借用的值不够长 - 将字符串切片到 HashMap
Borrowed value does not live long enough - string slice into HashMap
我无法编译我的函数。它总是抱怨借来的价值活得不够长。我已经用字符串切片切换了 HashMap 的顺序,因为我认为销毁顺序会影响 HashMap 的寿命比放入的字符串切片长一步。但即使在更改之后它仍然不起作用:
代码
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();
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
字符串需要所有者:
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);
}
我无法编译我的函数。它总是抱怨借来的价值活得不够长。我已经用字符串切片切换了 HashMap 的顺序,因为我认为销毁顺序会影响 HashMap 的寿命比放入的字符串切片长一步。但即使在更改之后它仍然不起作用:
代码
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();
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
字符串需要所有者:
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);
}