Back to questions
JuniorTheoryCommon
What happens when this program is built and run?
fn main() {
let x = Some(5);
println!("{:?}", x + 1);
}It does not compile
- Expecting
Some(6):+does not reach inside anOption, and Rust has no arithmetic onOption<i32>, sox + 1is error E0369 until the value is taken out ofSome. - Expecting
6, as ifSome(5)were simply 5:Option<i32>andi32are different types, and the compiler never uses one in place of the other.
- How would you take the value out of
xso that the program prints6? - What does
x.unwrap() + 1do whenxisNone?