借用的值不够长 - 字符串切片到HashMap

时间:2016-06-23 13:01:41

标签: rust lifetime borrow-checker

我无法编译我的函数。它总是抱怨借来的价值不够长。我已经用字符串切换切换了HashMap的顺序,因为我认为破坏顺序会影响HashMap比输入的字符串切片长一步。但即使在更改之后它仍然不起作用:

CODE

fn lyrics_more_bottles(song_template:&mut String, number:i32){
    let mut start_bottles:&str = format!("{} bottles", number).as_str();
    let mut remaining_num:&str = format!("{} bottles", number).as_str();
    let mut template_partials:HashMap<&str, &str> = HashMap::new();

    template_partials.insert("start", start_bottles);
    template_partials.insert("repeat", start_bottles);
    template_partials.insert("remaining", remaining_num);
    template_partials.insert("message", "Take one down and pass it around");

    resolve_template(song_template, template_partials);
}

ERROR_MSG:

lib.rs:45:34: 45:63 error: borrowed value does not live long enough
lib.rs:45     let mut start_bottles:&str = format!("{} bottles", number).as_str();
                                           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
lib.rs:45:34: 45:63 note: in this expansion of format! (defined in <std macros>)
lib.rs:45:73: 55:2 note: reference must be valid for the block suffix following statement 0 at 45:72...
lib.rs:45     let mut start_bottles:&str = format!("{} bottles", number).as_str();
lib.rs:46     let mut remaining_num:&str = format!("{} bottles", number).as_str();
lib.rs:47     let mut template_partials:HashMap<&str, &str> = HashMap::new();
lib.rs:48 
lib.rs:49     template_partials.insert("start", start_bottles);
lib.rs:50     template_partials.insert("repeat", start_bottles);
          ...
lib.rs:45:5: 45:73 note: ...but borrowed value is only valid for the statement at 45:4
lib.rs:45     let mut start_bottles:&str = format!("{} bottles", number).as_str();
              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
lib.rs:45:5: 45:73 help: consider using a `let` binding to increase its lifetime
lib.rs:45     let mut start_bottles:&str = format!("{} bottles", number).as_str();
              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
lib.rs:46:34: 46:63 error: borrowed value does not live long enough
lib.rs:46     let mut remaining_num:&str = format!("{} bottles", number).as_str();
                                           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
lib.rs:46:34: 46:63 note: in this expansion of format! (defined in <std macros>)
lib.rs:46:73: 55:2 note: reference must be valid for the block suffix following statement 1 at 46:72...
lib.rs:46     let mut remaining_num:&str = format!("{} bottles", number).as_str();
lib.rs:47     let mut template_partials:HashMap<&str, &str> = HashMap::new();
lib.rs:48 
lib.rs:49     template_partials.insert("start", start_bottles);
lib.rs:50     template_partials.insert("repeat", start_bottles);
lib.rs:51     template_partials.insert("remaining", remaining_num);
          ...
lib.rs:46:5: 46:73 note: ...but borrowed value is only valid for the statement at 46:4
lib.rs:46     let mut remaining_num:&str = format!("{} bottles", number).as_str();
              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
lib.rs:46:5: 46:73 help: consider using a `let` binding to increase its lifetime
lib.rs:46     let mut remaining_num:&str = format!("{} bottles", number).as_str();
              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

1 个答案:

答案 0 :(得分:1)

字符串需要拥有者:

fn lyrics_more_bottles(song_template:&mut String, number:i32){
    let mut start_bottles = format!("{} bottles", number); // own them as `String`
    let mut remaining_num = format!("{} bottles", number);
    let mut template_partials:HashMap<&str, &str> = HashMap::new();

    template_partials.insert("start", &start_bottles); // &String -> &str is implicit
    template_partials.insert("repeat", &start_bottles);
    template_partials.insert("remaining", &remaining_num);
    template_partials.insert("message", "Take one down and pass it around");

    resolve_template(song_template, template_partials);
}