为什么 Rust 的示例猜谜游戏允许具有不同 return 类型的匹配语句?
Why does Rust's example guessing game allow a match statement with varying return types?
查看 the intro book 中的猜谜游戏示例,特别是您使用 match
语句执行错误处理的部分:
let guess: u32 = match guess.trim().parse() {
Ok(num) => num,
Err(_) => continue,
};
为什么它不抱怨 match
语句的不同部分有不同的 return 类型?一个 return 是 u32
,另一个执行 continue
语句并且不 return 任何东西。我认为要么 match
语句的所有分支都必须执行代码,要么所有分支必须 return 彼此类型相同的东西。
continue
具有类型 !
(AKA "never"), which can coerce into any other type,因为它的值不存在。
查看 the intro book 中的猜谜游戏示例,特别是您使用 match
语句执行错误处理的部分:
let guess: u32 = match guess.trim().parse() {
Ok(num) => num,
Err(_) => continue,
};
为什么它不抱怨 match
语句的不同部分有不同的 return 类型?一个 return 是 u32
,另一个执行 continue
语句并且不 return 任何东西。我认为要么 match
语句的所有分支都必须执行代码,要么所有分支必须 return 彼此类型相同的东西。
continue
具有类型 !
(AKA "never"), which can coerce into any other type,因为它的值不存在。