Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么rust中函数末尾的Ok(())后面没有分号?_Rust_Semicolon Inference - Fatal编程技术网

为什么rust中函数末尾的Ok(())后面没有分号?

为什么rust中函数末尾的Ok(())后面没有分号?,rust,semicolon-inference,Rust,Semicolon Inference,代码段取自以下文档: 使用std::env; fn main()->std::io::Result{ 让path=env::current_dir()?; println!(“当前目录为{}”,path.display()); 好(()) } 我注意到,只有在Ok(())之后添加分号,程序才会出现以下错误: 错误[E0308]:类型不匹配 应为枚举'std::result::result',找到了'()` 这是为什么?Rust返回最后一个表达式的值。当您在Ok(())之后添加分号时,最终表达

代码段取自以下文档:

使用std::env;
fn main()->std::io::Result{
让path=env::current_dir()?;
println!(“当前目录为{}”,path.display());
好(())
}
我注意到,只有在
Ok(())
之后添加分号,程序才会出现以下错误:

错误[E0308]:类型不匹配
应为枚举'std::result::result',找到了'()`

这是为什么?

Rust返回最后一个表达式的值。当您在
Ok(())
之后添加分号时,最终表达式将成为一条语句,因此它将返回语句的“值”,即缺少值,也称为
unit
(称为“()”)

这一问题也在这里提出和回答:

关于文件的表述: