Rust uses the single equals sign (`=`) as its assignment operator. Assignment binds a name to a value, but it does not produce a Boolean result for a conditional; its result is the unit value, `()`. section 2.2 section 1.6.2
Keep these two operations distinct:
- Use `=` to assign a value, such as `let a = 10`. section 2.2
- Use `==` to test equality. Writing `if a = 10` fails because `if` requires a Boolean but receives `()` from the assignment. section 1.6.2
- When the assigned value owns a non-copyable object, assignment can move ownership to the new variable. section 4.4
Fixing an accidental assignment
This condition is invalid: `if a = 10 { ... }`. Replace the assignment with an equality test: `if a == 10 { println!("a equals ten"); }`. For ownership, `let new_sat_a = sat_a;` transfers the `CubeSat` value to `new_sat_a`; a function can later return it to the original scope. section 1.6.2 section 4.4
1 fn main() {
2 let a = 10;
3
4 if a = 10 {
5 println!("a equals ten");
6 }
7 }
Inspect the invalid conditional that confuses assignment with equality testing. section 1.6.2






