- Rust 教程
- Rust - 首页
- Rust - 简介
- Rust - 环境设置
- Rust - HelloWorld 示例
- Rust - 数据类型
- Rust - 变量
- Rust - 常量
- Rust - 字符串
- Rust - 运算符
- Rust - 决策
- Rust - 循环
- Rust - 函数
- Rust - 元组
- Rust - 数组
- Rust - 所有权
- Rust - 借用
- Rust - 片段
- Rust - 结构
- Rust - 枚举
- Rust - 模块
- Rust - 集合
- Rust - 错误处理
- Rust - 泛型类型
- Rust - 输入输出
- Rust - 文件输入/输出
- Rust - 包管理器
- Rust - 迭代器和闭包
- Rust - 智能指针
- Rust - 并发
- Rust 有用资源
- Rust - 快速指南
- Rust - 有用资源
- Rust - 讨论
Rust - 算术运算符
假设变量 a 和 b 中的值分别是 10 和 5。
| 序号 | 运算符 | 描述 | 示例 |
|---|---|---|---|
| 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
rust_operators.htm
广告