23

This is the code I am trying to execute:

fn my_fn(arg1: &Option<Box<i32>>) -> i32 {
    if arg1.is_none() {
        return 0;
    }
    let integer = arg1.unwrap();
    *integer
}

fn main() {
    let integer = 42;
    my_fn(&Some(Box::new(integer)));
}

(on the Rust playground)

I get the following error in previous versions of Rust:

error[E0507]: cannot move out of borrowed content
 --> src/main.rs:5:19
  |
5 |     let integer = arg1.unwrap();
  |                   ^^^^ cannot move out of borrowed content

And in more modern versions:

error[E0507]: cannot move out of `*arg1` which is behind a shared reference
 --> src/main.rs:5:19
  |
5 |     let integer = arg1.unwrap();
  |                   ^^^^
  |                   |
  |                   move occurs because `*arg1` has type `std::option::Option<std::boxed::Box<i32>>`, which does not implement the `Copy` trait
  |                   help: consider borrowing the `Option`'s content: `arg1.as_ref()`

I see there is already a lot of documentation about borrow checker issues, but after reading it, I still can't figure out the problem.

Why is this an error and how do I solve it?

user4815162342
  • 141,790
  • 18
  • 296
  • 355
Moebius
  • 6,242
  • 7
  • 42
  • 54

2 Answers2

23

Option::unwrap() consumes the option, that is, it accepts the option by value. However, you don't have a value, you only have a reference to it. That's what the error is about.

Your code should idiomatically be written like this:

fn my_fn(arg1: &Option<Box<i32>>) -> i32 {
    match arg1 {
        Some(b) => **b,
        None => 0,
    }
}

fn main() {
    let integer = 42;
    my_fn(&Some(Box::new(integer)));
}

(on the Rust playground)

Or you can use Option combinators like Option::as_ref or Option::as_mut paired with Option::map_or, as Shepmaster has suggested:

fn my_fn(arg1: &Option<Box<i32>>) -> i32 {
    arg1.as_ref().map_or(0, |n| **n)
}

This code uses the fact that i32 is automatically copyable. If the type inside the Box weren't Copy, then you wouldn't be able to obtain the inner value by value at all - you would only be able to clone it or to return a reference, for example, like here:

fn my_fn2(arg1: &Option<Box<i32>>) -> &i32 {
    arg1.as_ref().map_or(&0, |n| n)
}

Since you only have an immutable reference to the option, you can only return an immutable reference to its contents. Rust is smart enough to promote the literal 0 into a static value to keep in order to be able to return it in case of absence of the input value.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Vladimir Matveev
  • 120,085
  • 34
  • 287
  • 296
  • And what if I want to get a reference to the i32 inside the box (let's say I want to modify it and have another part of the program to see the changed value) ? – Moebius Sep 01 '15 at 19:07
  • 1
    FWIW, I'd write it as `arg1.as_ref().map(|x| **x).unwrap_or(0)` – Shepmaster Sep 01 '15 at 19:12
  • 1
    @Moebius, I've updated my answer with an example with references, however, I didn't exactly understand what do you mean about modifying values. An example would be helpful, but it probably deserves another question anyway. – Vladimir Matveev Sep 01 '15 at 19:41
  • @VladimirMatveev no special use case, I was just asking out of curiosity. Thanks for the solution ! – Moebius Sep 01 '15 at 21:16
1

Since Rust 1.40 there is Option::as_deref, so now you can do:

fn my_fn(arg1: &Option<Box<i32>>) -> i32 {
    *arg1.as_deref().unwrap_or(&0)
}
Daniel
  • 1,358
  • 11
  • 15