Match statements and the keyword 'some'

From this page

fn plus_one(x: Option<i32>) -> Option<i32> {
    match x {
        None => None,
        Some(i) => Some(i + 1),

let five = Some(5);
let six = plus_one(five);
let none = plus_one(None);

There is something I don't understand. How come the variable five doesn't call the plus_one function. How come it directly calls Some(5)?

That's because the "keywords" (None and Some) as you're calling them are actually tuple variants for Option which are imported by default. So really saying Some(x) Is Option::Some(x).

But like in this example the person didn't even call the plus_one() function so how Option::Some(x) still end up calling the plus_one() function?

You are calling the plus_one function here let six = plus_one(five);

1 Like

What I am trying to say is that in let five = Some(5); doesn't this call the plus_one function, cause it goes through the match case which is inside the plus_one function?

No, this line individually has nothing to do with plus_one, but the value that five holds is moved into plus_one so that it can be matched on.


No, this just makes an Option<i32> and that's all. It's kinda like

struct Person {
    name: String

fn main() {
    let person = Person { name: "Bob".to_string() };

Person { name: "Bob".to_string() } just makes a value of type Person and does nothing else.

Like that


Just makes a value of type Option<i32> and nothing else


This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.