为什么迭代HashMap<& str,& str>产生&& str?

时间:2016-06-21 05:12:47

标签: string hashmap pattern-matching rust

我正在迭代HashMap并通过模式匹配将值放入其中。

Delegater

fn lyrics_no_bottles(song_template:&mut String){
    let mut template_partials = HashMap::new();

    template_partials.insert("start", "No more bottles");
    template_partials.insert("repeat", "no more bottles");
    template_partials.insert("remaining", "99 bottles");
    template_partials.insert("message", "Go to the store and buy some more");

    resolve_template(song_template, template_partials);
}

调用

fn resolve_template(song_template:&mut String, partials: HashMap<&str, &str>){
    let start:&str;
    let repeat:&str;
    let remaining:&str;
    let message:&str;

    for key in partials.keys(){
        match key {
            "start" => start = partials.get(key),
            "repeat" => repeat = partials.get(key),
            "remaining" => remaining = partials.get(key),
            "message" => message = partials.get(key)
        }
    }

    *song_template = song_template.replace("%1", start);
    *song_template = song_template.replace("%2", repeat);
    *song_template = song_template.replace("%3", message);
    *song_template = song_template.replace("%4", remaining);
}

错误输出

lib.rs:51:5: 58:6 error: type mismatch resolving `<std::collections::hash::map::Keys<'_, &str, &str> as core::iter::Iterator>::Item == &str`:
 expected &-ptr,
    found str [E0271]
lib.rs:51     for key in partials.keys(){
lib.rs:52         match key {
lib.rs:53             "start" => start = partials.get(key),
lib.rs:54             "repeat" => repeat = partials.get(key),
lib.rs:55             "remaining" => remaining = partials.get(key),
lib.rs:56             "message" => message = partials.get(key)
          ...
lib.rs:51:5: 58:6 help: run `rustc --explain E0271` to see a detailed explanation
lib.rs:53:32: 53:49 error: mismatched types:
 expected `&str`,
    found `core::option::Option<&&str>`
(expected &-ptr,
    found enum `core::option::Option`) [E0308]
lib.rs:53             "start" => start = partials.get(key),

当我将参数声明为&&str时,我不明白为什么编译器认为存在HashMap<&str,&str>

1 个答案:

答案 0 :(得分:3)

因为that particular method returns a reference wrapped by an Option

基本上,如果您在String存储get并使用Option<&String> ..因为调用get,您将获得&str。由于调用Option<&&str>,存储get会向您返回get

值示例以及在您致电String -> Option<&String> &str -> Option<&&str> u32 -> Option<&u32> 时它们将如何显示:

&str

get作为值插入时,use std::collections::HashMap; fn main() { let mut map: HashMap<&str, &str> = HashMap::new(); map.insert("start", "value"); let mut start: &str = "Unknown"; let mut repeat: &str; let mut remaining: &str; let mut message: &str; for key in map.keys() { match &key[..] { "start" => start = map.get(key).unwrap(), "repeat" => repeat = map.get(key).unwrap(), "remaining" => remaining = map.get(key).unwrap(), "message" => message = map.get(key).unwrap(), _ => unreachable!() } } println!("Result: {}", start); } 的所有权将被转移到散列映射中。因此,hashmap使用$item[] = [ 'content' => [ Html::tag('div', 'begin my div here' . Html::tag('div', 'center'), ['class' => 'background_white'] ) ] ]; ...或mutable references using get_mut返回对它们的引用。

游乐场目前已被打破(无法与其分享任何内容)...所以这里有一段工作......细分的代码示例:

beginTag