Back to questions
JuniorTheoryOccasional
What happens when this program is built and run?
fn add(a, b) -> i32 {
a + b
}
fn main() {
println!("{}", add(2, 3));
}It does not compile
- Expecting
5because the calladd(2, 3)shows the types: inference stops at the signature, so each parameter is written asa: i32. - Expecting a shared type such as
a, b: i32: Rust has no such shorthand, and every parameter carries its ownname: Type.
- Why does Rust require parameter types when
let x = 5;needs none? - How would you write
addso that it accepts bothi32andf64?