函数 returns &Option 而不是 Option

Function returns &Option and not Option

我是 Rust 的初学者,我不明白我遇到的行为。函数号 1 应该 return me 是一个 Option 类型,但 returns 是一个 &Option 类型。我已经通过执行功能 2 解决了这个问题,但我不明白为什么会这样。我的功能做错了什么?

函数 1:

pub fn manage_request(&self, request:String) -> Option<Some type> {
    let words : Vec<&str> = request.split_whitespace().collect();
    match words[0] {
        "attach" => &self.hashmap.get(words[1]),
        _ => None,
    }
}

函数 2:

pub fn manage_request(&self, request:String) -> Option<Some type> {
    let words : Vec<&str> = request.split_whitespace().collect();
    match words[0] {
        "attach" => match &self.hashmap.get(words[1]){
            None => None,
            Some(x) => Some(x)
        }
        _ => None,
    }
}

Here is a link to rust playground with the code

如果我错了请纠正我,但我认为你确实做得很好,但可能漏了一个错字。

在你的操场上,你的签名 通过说 fn manage_request(self) 消耗 自我。如果您将其更改为像 fn manage_request(&self) 这样的对 self 的引用,我相信它会起作用。

已更新 playground link 运行时没有编译器错误: