
控制流是编程的核心,它决定了程序如何根据条件执行不同的代码分支。在Rust中,控制流的主要工具包括:
Rust的一个独特之处在于:if是一个表达式而不是语句,这意味着它可以返回值。这种设计让Rust代码更加简洁和函数式。
本文将深入探讨Rust的if表达式,包括:
通过本文的学习,你将能够:
Rust中的if表达式语法与其他语言类似,但更加强调其作为表达式的特性:
fn main() {
let number = 5;
if number < 10 {
println!("数字小于10");
}
println!("继续执行");
}关键特点:
bool 类型(不像C/C++可以用0/非0){}fn main() {
let number = 15;
if number < 10 {
println!("数字小于10");
} else {
println!("数字大于等于10");
}
}
fn main() {
let number = 7;
if number < 5 {
println!("数字小于5");
} else if number < 10 {
println!("数字在5到10之间");
} else {
println!("数字大于等于10");
}
}
这是Rust if最独特的特性:if表达式可以返回值!
fn main() {
let number = 5;
// if表达式返回一个值
let result = if number < 10 {
100 // 注意:没有分号,这是表达式
} else {
200 // 同样没有分号
};
println!("结果: {}", result); // 100
}
关键规则:
()这是Rust的常见模式,可以替代其他语言的三元运算符:
fn main() {
let condition = true;
// Rust中没有三元运算符 ? :,但可以使用if表达式
let number = if condition {
5
} else {
6
};
println!("数字: {}", number);
}
与C/C++/Java的对比:
// C/C++/Java中的三元运算符
// int number = condition ? 5 : 6;
// Rust中的等价写法
let number = if condition { 5 } else { 6 };fn main() {
let score = 85;
// 所有分支必须返回相同类型
let grade = if score >= 90 {
"优秀"
} else if score >= 80 {
"良好"
} else if score >= 60 {
"及格"
} else {
"不及格"
};
println!("分数 {} 的等级: {}", score, grade);
}
错误示例:
fn main() {
let condition = true;
// 错误!类型不匹配
// let result = if condition {
// 5 // i32类型
// } else {
// "six" // &str类型 - 编译错误!
// };
// 正确:返回相同类型
let result = if condition {
5
} else {
6
};
}在Rust中,if的条件必须是 bool 类型,不能使用数字或其他类型:
fn main() {
let number = 5;
// 错误!数字不能直接用作条件
// if number {
// println!("非零");
// }
// 正确:使用比较运算符
if number != 0 {
println!("非零");
}
// 或者显式转换为bool
if number > 0 {
println!("正数");
}
}
与其他语言的对比:
语言 | 条件类型 | 示例 |
|---|---|---|
C/C++ | 任何类型 | if (5) ✅ |
Python | 任何类型(真值测试) | if 5: ✅ |
JavaScript | 任何类型(真值测试) | if (5) ✅ |
Rust | 只能是bool | if 5 ❌,if true ✅ |
fn main() {
let number = 5;
// 使用比较运算符创建bool值
let is_positive = number > 0;
let is_even = number % 2 == 0;
if is_positive {
println!("正数");
}
if is_even {
println!("偶数");
} else {
println!("奇数");
}
// 直接使用表达式
if number > 0 && number < 10 {
println!("在0到10之间");
}
}fn main() {
let age = 25;
let has_license = true;
let has_car = false;
// 复杂的逻辑组合
if age >= 18 && has_license {
println!("可以开车");
}
if (age >= 18 && has_license) || has_car {
println!("可以出行");
}
// 多个条件
if age >= 18 && age <= 65 && has_license {
println!("符合开车条件");
}
if !has_car && (age < 18 || !has_license) {
println!("不能单独出行");
}
}
fn is_even(n: i32) -> bool {
n % 2 == 0
}
fn is_positive(n: i32) -> bool {
n > 0
}
fn main() {
let number = 8;
if is_even(number) && is_positive(number) {
println!("{} 是正偶数", number);
}
// 链式条件
if is_even(number) {
if is_positive(number) {
println!("{} 是正偶数", number);
}
}
}
fn main() {
let x = 5;
let y = 10;
if x > 0 {
if y > 0 {
println!("x 和 y 都是正数");
} else {
println!("x 是正数,但 y 不是");
}
} else {
println!("x 不是正数");
}
}
fn main() {
let x = 5;
let y = 10;
// 嵌套if表达式也可以返回值
let result = if x > 0 {
if y > 0 {
"都是正数"
} else {
"x是正数,y不是"
}
} else {
"x不是正数"
};
println!("结果: {}", result);
}fn main() {
let x = 5;
let y = 10;
// 方式1:嵌套(可能较复杂)
let result1 = if x > 0 {
if y > 0 {
"都是正数"
} else {
"x是正数,y不是"
}
} else {
"x不是正数"
};
// 方式2:使用else if(更清晰)
let result2 = if x > 0 && y > 0 {
"都是正数"
} else if x > 0 {
"x是正数,y不是"
} else {
"x不是正数"
};
println!("结果1: {}", result1);
println!("结果2: {}", result2);
}fn main() {
let age = 25;
// 使用if表达式返回值
let category = if age < 13 {
"儿童"
} else if age < 18 {
"青少年"
} else if age < 65 {
"成年人"
} else {
"老年人"
};
println!("年龄 {} 属于: {}", age, category);
// 多个条件的判断
let can_vote = age >= 18;
let can_drive = age >= 16;
let can_drink = age >= 21;
println!("可以投票: {}", can_vote);
println!("可以开车: {}", can_drive);
println!("可以饮酒: {}", can_drink);
}fn main() {
fn get_grade(score: f64) -> &'static str {
if score >= 90.0 {
"A"
} else if score >= 80.0 {
"B"
} else if score >= 70.0 {
"C"
} else if score >= 60.0 {
"D"
} else {
"F"
}
}
let scores = vec![95.0, 85.0, 75.0, 65.0, 55.0];
for score in scores {
let grade = get_grade(score);
println!("分数 {:.1} -> 等级 {}", score, grade);
}
}fn main() {
let environment = "production"; // 或 "development", "testing"
// 根据环境选择配置
let config = if environment == "production" {
("prod.example.com", 443, true)
} else if environment == "testing" {
("test.example.com", 8080, false)
} else {
("localhost", 3000, false)
};
let (host, port, use_ssl) = config;
println!("环境: {}", environment);
println!("主机: {}", host);
println!("端口: {}", port);
println!("使用SSL: {}", use_ssl);
}
fn main() {
fn max(a: i32, b: i32) -> i32 {
if a > b {
a
} else {
b
}
}
let x = 10;
let y = 20;
let z = 15;
// 嵌套if表达式找最大值
let maximum = max(max(x, y), z);
println!("{}, {}, {} 中的最大值: {}", x, y, z, maximum);
// 使用if表达式链
let max_val = if x > y {
if x > z { x } else { z }
} else {
if y > z { y } else { z }
};
println!("使用if表达式链的最大值: {}", max_val);
}
fn main() {
let condition = true;
// 错误!if和else返回类型不同
// let result = if condition {
// 5 // i32
// } else {
// "six" // &str - 类型不匹配!
// };
// 解决方案1:返回相同类型
let result1 = if condition {
5
} else {
6
};
// 解决方案2:返回Option
let result2 = if condition {
Some(5)
} else {
None
};
println!("result1: {}", result1);
println!("result2: {:?}", result2);
}fn main() {
let condition = true;
// 错误!加了分号,变成了语句
// let result = if condition {
// 5; // 这是语句,返回 ()
// } else {
// 6; // 这是语句,返回 ()
// };
// 正确:表达式不能有分号
let result = if condition {
5 // 没有分号
} else {
6 // 没有分号
};
println!("结果: {}", result);
}fn main() {
let number = 5;
// 错误!Rust不支持非bool条件
// if number {
// println!("非零");
// }
// 解决方案:使用比较或转换
if number != 0 {
println!("非零");
}
// 或者
if number > 0 {
println!("正数");
}
}当if作为表达式使用时,如果某个分支可能不返回值,会导致问题:
fn main() {
let condition = true;
// 如果condition为false,这个if没有返回值
// let result = if condition {
// 5
// }; // 错误!if表达式必须有else分支
// 解决方案1:添加else分支
let result1 = if condition {
5
} else {
0
};
// 解决方案2:使用Option
let result2 = if condition {
Some(5)
} else {
None
};
println!("result1: {}", result1);
println!("result2: {:?}", result2);
}// ✅ 推荐:简单的值选择
let max = if a > b { a } else { b };
// ✅ 推荐:简单的配置
let port = if is_production { 443 } else { 3000 };
// ❌ 不推荐:过于复杂的逻辑
let result = if condition1 {
if condition2 {
if condition3 {
complex_calculation1()
} else {
complex_calculation2()
}
} else {
complex_calculation3()
}
} else {
complex_calculation4()
};
// ✅ 推荐:复杂逻辑使用match或函数
let result = match (condition1, condition2, condition3) {
(true, true, true) => complex_calculation1(),
(true, true, false) => complex_calculation2(),
(true, false, _) => complex_calculation3(),
(false, _, _) => complex_calculation4(),
};// ✅ 好的做法:清晰的逻辑
let status = if score >= 90 {
"优秀"
} else if score >= 80 {
"良好"
} else {
"需努力"
};
// ✅ 好的做法:使用函数
fn get_status(score: i32) -> &'static str {
if score >= 90 { "优秀" }
else if score >= 80 { "良好" }
else { "需努力" }
}
// ❌ 避免:过于复杂的嵌套
// 如果嵌套超过2层,考虑重构fn main() {
let number = 5;
// 使用if-else if链
let result1 = if number < 0 {
"负数"
} else if number == 0 {
"零"
} else {
"正数"
};
// 使用match表达式(可能更清晰)
let result2 = match number.cmp(&0) {
std::cmp::Ordering::Less => "负数",
std::cmp::Ordering::Equal => "零",
std::cmp::Ordering::Greater => "正数",
};
println!("if表达式: {}", result1);
println!("match表达式: {}", result2);
}选择指南:
fn main() {
fn divide(numerator: f64, denominator: f64) -> Option<f64> {
if denominator != 0.0 {
Some(numerator / denominator)
} else {
None
}
}
match divide(10.0, 2.0) {
Some(result) => println!("结果: {}", result),
None => println!("除零错误"),
}
}fn main() {
fn safe_parse(s: &str) -> Result<i32, &'static str> {
if s.is_empty() {
Err("字符串为空")
} else if let Ok(num) = s.parse::<i32>() {
Ok(num)
} else {
Err("不是有效数字")
}
}
match safe_parse("42") {
Ok(num) => println!("解析成功: {}", num),
Err(e) => println!("解析失败: {}", e),
}
}fn main() {
// 根据条件初始化变量
let config_value = if std::env::var("DEBUG").is_ok() {
"debug"
} else {
"release"
};
println!("配置值: {}", config_value);
// 在复杂初始化中使用
let mut counter = if config_value == "debug" {
0 // 调试模式从0开始
} else {
100 // 生产模式从100开始
};
println!("计数器初始值: {}", counter);
}编写一个函数,根据摄氏度值返回温度描述(极冷、冷、适中、热、极热)。
使用if表达式判断一个年份是否为闰年(能被4整除但不能被100整除,或能被400整除)。
编写函数判断三条边能否构成三角形,以及三角形的类型(等边、等腰、普通)。
根据购买金额计算折扣后的价格:超过100元打9折,超过500元打8折,超过1000元打7折。
根据身高和体重计算BMI,并使用if表达式判断体重状况(过轻、正常、过重、肥胖)。
特性 | C/C++/Java | Python | Rust |
|---|---|---|---|
条件类型 | 任意 | 任意(真值测试) | 只能是bool |
返回值 | 语句 | 语句 | 表达式 |
三元运算符 | ? : | if-else表达式 | 使用if表达式 |
括号 | 必需 | 可选 | 不需要 |
掌握了if表达式后,下一步我们将学习: