为什么在 main 中使用 Result 时,match 臂中需要 return ?
我正在阅读《Rust by Examples》一书。在此示例 删除 Err(e) => 中的
导致错误:return
return Err(e)预期为“i32”,但发现枚举“Result”
。这是为什么?
Err(e) => 和有什么区别?返回 Err(e)
和 Err(e) =>错误(e)?
这是示例中的代码:
use std::num::ParseIntError;
fn main() -> Result<(), ParseIntError> {
let number_str = "10";
let number = match number_str.parse::<i32>() {
Ok(number) => number,
Err(e) => return Err(e),
};
println!("{}", number);
Ok(())
}
I am reading Rust by Example book. In this example removing return
in Err(e) => return Err(e)
causes an error: expected `i32`, found enum `Result``
. Why is that?
What is the difference between Err(e) => return Err(e)
and Err(e) => Err(e)
?
Here is the code from the example:
use std::num::ParseIntError;
fn main() -> Result<(), ParseIntError> {
let number_str = "10";
let number = match number_str.parse::<i32>() {
Ok(number) => number,
Err(e) => return Err(e),
};
println!("{}", number);
Ok(())
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这表示如果
number_str.parse::()
返回Ok
将number
设置为number
,i32。如果它返回Err
,则立即从main
返回Err(e)
,这是一个Result
,<代码>数字未设置。这很好,因为
number
只能是i32
并且main
确实返回Result
。这表示如果
number_str.parse::()
返回Ok
将number
设置为number
,i32,和以前一样。如果它返回一个Err
,则将number
设置为Err(e)
,这是一个Result
。number
不能同时是i32
和Result
,因此您会收到编译器错误。This says if
number_str.parse::<i32>()
returns anOk
to setnumber
tonumber
, an i32. If it returns anErr
to immediately returnErr(e)
, which is aResult
, frommain
,number
is not set.That's fine because
number
can only be ani32
andmain
does return aResult
.This says if
number_str.parse::<i32>()
returns anOk
to setnumber
tonumber
, an i32, same as before. If it returns anErr
to setnumber
toErr(e)
, which is aResult
.number
cannot be both ani32
and aResult
, so you get a compiler error.比赛
的所有手臂都必须具有“兼容”类型。这要么意味着它们都具有相同的类型:要么某些分支需要以某种方式将控制流从匹配中引出。
要记住的主要规则是 Rust 中的每个变量都需要一个单一类型(忽略生命周期的差异)。就您而言,如果没有
return
关键字,number
的类型是什么?如果解析成功,
number
将是i32
。如果失败,则会出现Err(ParseIntError)
。这是不允许的,因此您会收到编译错误。但是,如果您
返回 Err(e)
,则number
将始终为i32
(因为return
会阻止其余部分)运行的函数,因此number
不需要类型)。类似的事情也适用于其他控制流关键字(例如
continue
、break
等),以及保证不返回的内容,例如panic!()
(查找Never
/!
类型以获取更多信息)All arms of a
match
have to have "compatible" types. This either mean they all have the same type:or some of the branches need to direct control flow away from the match in some way.
The main rule to keep in your head is that every variable in Rust needs a single type (ignoring variance w.r.t. lifetimes). In your case, if you didn't have the
return
keyword, what would be the type ofnumber
?If the parse succeeds,
number
would be ani32
. If it fails, it would be anErr(ParseIntError)
. This isn't allowed, so you get a compile error.However, if you
return Err(e)
,number
will always be ani32
(sincereturn
prevents the rest of the function running, sonumber
doesn't need a type).A similar thing applies to other control-flow keywords (e.g.
continue
,break
, ...), as well as things that are guaranteed to not return, likepanic!()
(look up theNever
/!
type for more info)