如何访问存储在Vec中的选项类型?

时间:2015-08-01 21:57:18

标签: rust

我在使用这个简单的构造时遇到了麻烦:

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

我收到编译错误:

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

如何在Option中存储Vec种类型,并访问仍包含在Option中的个别元素?

1 个答案:

答案 0 :(得分:1)

刚刚意识到发生了什么。 Vec会返回Option<T>

相关问题