rust Some(v)和Some(&v)有什么区别?

xqk2d5yq  于 2023-05-23  发布在  其他
关注(0)|答案(1)|浏览(209)

下面代码中的code block 1code block 2都可以工作,但我不知道为什么。
Some(&number)Some(number)有什么区别?

use std::collections::HashMap;

fn call(number: &str) -> &str {
    match number {
        "798-1364" => "We're sorry, please hang up and try again.",
        "645-7689" => "Hello, what can I get for you today?",
        _ => "Hi! Who is this again?"
    }
}

fn main() { 
    let mut contacts = HashMap::new();

    contacts.insert("Daniel", "798-1364");

    //====== code block 1
    match contacts.get(&"Daniel") {
        Some(&number) => println!("Calling Daniel: {}", call(&number)),
        _ => println!("Don't have Daniel's number."),
    }

    //====== code block 2, without '&'
    match contacts.get("Daniel") {
        Some(number) => println!("Calling Daniel: {}", call(number)),
        _ => println!("Don't have Daniel's number."),
    }

}
dba5bblo

dba5bblo1#

contacts.get("Daniel")中,参数的类型是&str
contacts.get(&"Daniel")中,参数的类型是&&str
HashMap的get()方法实现如下:

pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V>
where
    K: Borrow<Q>,
    Q: Hash + Eq,
{
    self.base.get(k)
}

当您传递&&str时,Rust编译器会自动将&&str解引用为&str。因此,您的两个版本在本质上是等同的。
在您的match * 手臂 *:

  • Some(&number)number的类型是&str
  • Some(number)number的类型是&&str

同样,由于Rust的自动解引用规则,两者本质上是相同的。
请注意,如果您编写了Some(&&number)number的类型将是str,并且不会编译为以下消息:

the size for values of type `str` cannot be known at compilation time

但是当你有&&T甚至&&&&&T时,就像你有&T一样。

相关问题