exercises/03_if
if1.rs - 基础 if 条件判断
问题代码
fn bigger(a: i32, b: i32) -> i32 {
}
fn main() {
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn ten_is_bigger_than_eight() {
assert_eq!(10, bigger(10, 8));
}
#[test]
fn fortytwo_is_bigger_than_thirtytwo() {
assert_eq!(42, bigger(32, 42));
}
#[test]
fn equal_numbers() {
assert_eq!(42, bigger(42, 42));
}
}
解题要点
- 错误原因: 函数体为空,需要实现比较两个数大小的逻辑
- 解决方法: 使用 if 表达式比较两个参数,返回较大值
正确写法
fn bigger(a: i32, b: i32) -> i32 {
if a > b {
a
} else {
b
}
}
fn main() {
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn ten_is_bigger_than_eight() {
assert_eq!(10, bigger(10, 8));
}
#[test]
fn fortytwo_is_bigger_than_thirtytwo() {
assert_eq!(42, bigger(32, 42));
}
#[test]
fn equal_numbers() {
assert_eq!(42, bigger(42, 42));
}
}
if2.rs - if 表达式类型一致性
问题代码
fn picky_eater(food: &str) -> &str {
if food == "strawberry" {
"Yummy!"
} else {
1
}
}
fn main() {
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn yummy_food() {
assert_eq!(picky_eater("strawberry"), "Yummy!");
}
#[test]
fn neutral_food() {
assert_eq!(picky_eater("potato"), "I guess I can eat that.");
}
#[test]
fn default_disliked_food() {
assert_eq!(picky_eater("broccoli"), "No thanks!");
assert_eq!(picky_eater("gummy bears"), "No thanks!");
assert_eq!(picky_eater("literally anything"), "No thanks!");
}
}
解题要点
- 错误原因: if 表达式的两个分支返回不同类型(字符串 vs 整数),Rust 要求类型一致
- 解决方法: 根据测试用例,所有分支都应返回字符串类型
正确写法
fn picky_eater(food: &str) -> &str {
if food == "strawberry" {
"Yummy!"
} else if food == "potato" {
"I guess I can eat that."
} else {
"No thanks!"
}
}
fn main() {
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn yummy_food() {
assert_eq!(picky_eater("strawberry"), "Yummy!");
}
#[test]
fn neutral_food() {
assert_eq!(picky_eater("potato"), "I guess I can eat that.");
}
#[test]
fn default_disliked_food() {
assert_eq!(picky_eater("broccoli"), "No thanks!");
assert_eq!(picky_eater("gummy bears"), "No thanks!");
assert_eq!(picky_eater("literally anything"), "No thanks!");
}
}
if3.rs - if 表达式与类型推断
问题代码
fn animal_habitat(animal: &str) -> &str {
let identifier = if animal == "crab" {
1
} else if animal == "gopher" {
2.0
} else if animal == "snake" {
3
} else {
"Unknown"
};
if identifier == 1 {
"Beach"
} else if identifier == 2 {
"Burrow"
} else if identifier == 3 {
"Desert"
} else {
"Unknown"
}
}
fn main() {
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn gopher_lives_in_burrow() {
assert_eq!(animal_habitat("gopher"), "Burrow")
}
#[test]
fn snake_lives_in_desert() {
assert_eq!(animal_habitat("snake"), "Desert")
}
#[test]
fn crab_lives_on_beach() {
assert_eq!(animal_habitat("crab"), "Beach")
}
#[test]
fn unknown_animal() {
assert_eq!(animal_habitat("dinosaur"), "Unknown")
}
}
解题要点
- 错误原因: if 表达式的分支返回不同类型(整数、浮点数、字符串),无法推断统一类型
- 解决方法: 所有分支返回相同类型,根据后续使用场景选择整数类型
正确写法
fn animal_habitat(animal: &str) -> &str {
let identifier = if animal == "crab" {
1
} else if animal == "gopher" {
2
} else if animal == "snake" {
3
} else {
0
};
if identifier == 1 {
"Beach"
} else if identifier == 2 {
"Burrow"
} else if identifier == 3 {
"Desert"
} else {
"Unknown"
}
}
fn main() {
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn gopher_lives_in_burrow() {
assert_eq!(animal_habitat("gopher"), "Burrow")
}
#[test]
fn snake_lives_in_desert() {
assert_eq!(animal_habitat("snake"), "Desert")
}
#[test]
fn crab_lives_on_beach() {
assert_eq!(animal_habitat("crab"), "Beach")
}
#[test]
fn unknown_animal() {
assert_eq!(animal_habitat("dinosaur"), "Unknown")
}
}
知识点汇总
if 基础语法
- if 条件判断: 使用
if condition { } else { } 进行条件分支
- if 表达式: if 可以作为表达式使用,返回值
- else if 链: 使用
else if 处理多个条件分支
if 表达式特性
- 类型一致性: if 表达式的所有分支必须返回相同类型
- 返回值: 最后一个表达式的值作为整个 if 表达式的返回值
- 类型推断: 编译器根据所有分支推断统一类型
实际应用
- 比较操作: 使用
>, <, == 等比较运算符
- 字符串比较: 使用
== 比较字符串字面量
- 条件赋值: 使用 if 表达式进行条件赋值操作