您可以使用return
命令在到达函数的最后一条语句之前从函数中返回:
fn example() -> i32 {
if true {
return 1;
}
0 // this line is never reached
}
能否使用块表达式实现类似的功能呢?
let foo = {
if true {
// somehow "return" so that foo is 1
}
0 // this line is never reached
};
在这个例子中,我本可以使用 if
-else
,但我想要询问一般情况下的块表达式。