How do I access the option types stored in Vec?

I'm having trouble with this simple construct:

fn main() { 
    let mut stack: Vec<Option<&str>> = Vec::new();
    stack.push(None);
    let item: Option<&str> = stack.pop();
}

      

I am getting compiler error:

src/main.rs:4:30: 4:41 error: mismatched types:
 expected `core::option::Option<&str>`,
    found `core::option::Option<core::option::Option<&str>>`
(expected &-ptr,
    found enum `core::option::Option`) [E0308]
src/main.rs:4     let item: Option<&str> = stack.pop();
                                           ^~~~~~~~~~~
src/main.rs:4:30: 4:41 help: run `rustc --explain E0308` to see a detailed explanation

      

How do I store Option

types in Vec

and access individual elements that are still wrapped in Option

?

+3


source to share


1 answer


I just realized what was going on. Vec

returns Option<T>

.



+1


source







All Articles