RUST 中的分支跳转-条件判断不需要加括号
rust
fn picky_eater(food: &str) -> &str {
if food == "strawberry" {
"Yummy!"
} else if food == "potato" {
"I guess I can eat that."
} else {
"No thanks!"
}
}
RUST 作为一种表达式导向的语言,if-else 块作为一个表达式,可以直接赋值给变量:
rust
fn animal_habitat(animal: &str) -> &str {
// TODO: Fix the compiler error in the statement below.
let identifier = if animal == "crab" {
1
} else if animal == "gopher" {
2
} else if animal == "snake" {
3
} else {
4
};
// Don't change the expression below!
if identifier == 1 {
"Beach"
} else if identifier == 2 {
"Burrow"
} else if identifier == 3 {
"Desert"
} else {
"Unknown"
}
}