如何修正代码以避免错误:
'a', 'b' -- is borrowed here
returns a value referencing data owned by the current function
?
use std::collections::HashMap;
#[derive(Debug)]
struct Error;
fn f1(s: &str) -> Result<&str, Error> {
// something for test:
if s.len() > 5 {
Ok(s)
}else{
Err(Error)
}
}
fn f2(s: &str) -> Result<&str, Error> {
todo!()
}
fn r(raw: HashMap<String, String>) -> Result<HashMap<&'static str, &'static str>, Error> {
raw
.into_iter()
.map(|(a, b)| {
// println!("{:?} - {:?}", a, b);
Ok((
f1(&a)?,
f2(&b)?,
))
})
.collect()
}
fn main() {
let raw: HashMap<String, String> = HashMap::from([
("Mercury".to_string(), "0.4".to_string()),
("Venus".to_string(), "0.7".to_string()),
("Earth".to_string(), "1.0".to_string()),
("Mars".to_string(), "1.5".to_string()),
]);
println!("{:?}", r(raw));
}