Rust - 算术运算符
假设变量 a 和 b 的值分别为 10 和 5。
| Sr.No | 运算符 | 描述 | 示例 |
|---|---|---|---|
| 1 | +(加法) | 返回操作数之和 | a+b 是 15 |
| 2 | -(减法) | 返回值的差异 | a-b 是 5 |
| 3 | *(乘法) | 返回值的乘积 | a*b 为 50 |
| 4 | /(除法) | 执行除法运算并返回商 | a / b 为 2 |
| 5 | %(模数) | 执行除法运算并返回余数 | a % b 为 0 |
注意 − Rust 不支持 ++ 和 -- 运算符。
示例
fn main() {
let num1 = 10 ;
let num2 = 2;
let mut res:i32;
res = num1 + num2;
println!("Sum: {} ",res);
res = num1 - num2;
println!("Difference: {} ",res) ;
res = num1*num2 ;
println!("Product: {} ",res) ;
res = num1/num2 ;
println!("Quotient: {} ",res);
res = num1%num2 ;
println!("Remainder: {} ",res);
}
输出
Sum: 12 Difference: 8 Product: 20 Quotient: 5 Remainder: 0

