ʰ
Size: a a a
ʰ
ʰ
ʰ
use std::collections::HashMap;
fn add_value(s: String, i: i32, h: &mut HashMap<&str, i32>) {
h.insert(s.as_str(), i);
}
fn main() {
let x = HashMap::new();
add_value("a".into(), 12, &mut x);
}
ʰ
error[E0597]:
s
does not live long enough
--> hashmap.rs:4:14
|
3 | fn add_value(s: String, i: i32, h: &mut HashMap<&str, i32>) {
| - let's call the lifetime of this reference '1
4 | h.insert(s.as_str(), i);
| ---------^-------------
| | |
| | borrowed value does not live long enough
| argument requires that s is borrowed for '1
5 | }
| - s dropped here while still borrowed
error: aborting due to previous error
For more information about this error, try rustc --explain E0597.
ʰ
ʰ
ʰ
ʰ
S[
error[E0597]:
s
does not live long enough
--> hashmap.rs:4:14
|
3 | fn add_value(s: String, i: i32, h: &mut HashMap<&str, i32>) {
| - let's call the lifetime of this reference '1
4 | h.insert(s.as_str(), i);
| ---------^-------------
| | |
| | borrowed value does not live long enough
| argument requires that s is borrowed for '1
5 | }
| - s dropped here while still borrowed
error: aborting due to previous error
For more information about this error, try rustc --explain E0597.
s.as_str()
живёт до конца add_value
, а ты его мытаешься положил в хешмапу с владеющим лайфтаймомʰ
s.as_str()
живёт до конца add_value
, а ты его мытаешься положил в хешмапу с владеющим лайфтаймомS[
h.insert(s, i);
ʰ
h.insert(s, i);
S[
S[
ʰ
ʰ
use std::collections::HashMap;
fn add_value(s: String, i: i32, h: &mut HashMap<String, i32>) {
h.insert(s, i);
}
fn main() {
let mut x = HashMap::new();
add_value("a".into(), 12, &mut x);
}
ʰ
pub (crate) enum Expr {
Const (i32),
Var (String),
Add (Box<Expr>, Box<Expr>),
Mul (Box<Expr>, Box<Expr>)
}
MT
ʰ