// Rust 比较运算let x =5;let y =10;println!("相等: {}", x == y);// falseprintln!("不等: {}", x != y);// trueprintln!("大于: {}", x > y);// falseprintln!("小于: {}", x < y);// true// 类型必须匹配// let result = 5 == 5.0; // 错误!不能比较 i32 和 f64
// Rust 赋值运算letmut x =10;
x +=5;// x = 15
x -=3;// x = 12
x *=2;// x = 24
x /=4;// x = 6
x %=4;// x = 2// 需要 mut 关键字// let y = 10;// y += 5; // 错误!y 不可变
# Python 赋值运算
x =10
x +=5# x = 15
x -=3# x = 12
x *=2# x = 24
x /=4# x = 6.0
x %=4# x = 2.0
x **=3# x = 8.0
x //=2# x = 4.0# 不需要特殊声明
y =10
y +=5# 可以直接修改
六、类型相关运算符
Rust 特有运算符
// 类型转换 aslet x:i32=10;let y:f64= x asf64;// 显式类型转换// 引用和解引用let value =42;let ref_value =&value;// 创建引用let deref_value =*ref_value;// 解引用// 范围运算符 ..for i in0..5{// 0, 1, 2, 3, 4println!("{}", i);}for i in0..=5{// 0, 1, 2, 3, 4, 5println!("{}", i);}
Python 特有运算符
# 身份运算符 is, is not
a =[1,2,3]
b =[1,2,3]
c = aprint(a is b)# False - 不同对象print(a is c)# True - 同一对象print(a == b)# True - 值相等# 成员运算符 in, not in
numbers =[1,2,3,4,5]print(3in numbers)# Trueprint(6notin numbers)# True# 海象运算符 := (Python 3.8+)if(n :=len(numbers))>3:print(f"列表有 {n} 个元素")