Rust type mismatch in numerical operation
- i32
- i64
fn main() {
let x: i32 = 3;
let y: i64 = 7;
let z = x + 1;
assert_eq!(z, 4);
println!("{z}");
let z = y + 1;
assert_eq!(z, 8);
println!("{z}");
//let z = x + y;
//println!("{z}");
}
- If we remove the
i32then this works even though the default isi32. - That’s because Rust will infere the type of the first variable from the type of the second variable and the operation.