Assigning unwrapped option to a variable

what's wrong with the below code ? Here I'm trying to assign k with 100 if op is Some else with 200

#![allow(unused)]
fn main() {
    let op:Option<i32> = Some(100);
    let k = if let Some(z) = op{
        100
    }
    else {
    200
    }
}

compilation error:

error: expected `;`, found `}`
  --> src/main.rs:9:6
   |
9  |     }
   |      ^ help: add `;` here
...
12 | }
   | - unexpected token

Doesn't the error message tell you? Let statements always have to end in a semicolon.

Most statements do, only ifs, matches and loops can IF they are used as statements. You are using an if-let expression

2 Likes

Yes, add the semicolon.

let k = if let Some(z) = op{
        100
    }
else {
    200
};
1 Like

thanks ! such a silly mistake!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.