0

So this is my situation:

let map: HashMap<String, String> = HashMap::new();
let key = String::from("key");
let v = map.get(&key).unwrap_or(&String::from("key not found"));
println!("{}", v);

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=1cab10de5528149c5d4ac37d9e0964f0

And this is my error:

temporary value is freed at the end of this statement creates a temporary which is freed while still in use

Being quite new to Rust I'm not sure if I'm using String properly or if str or &str would be more appropriate, but in any case I'm not exactly sure how I could use unwrap_or in this case to return a reference to a String. I know I could use pattern matching but I would prefer this pattern.

heapOverflow
  • 1,195
  • 2
  • 15
  • 28
  • Why do you want to return a reference? `String` is an owned value and goes out of scope immediately, so the reference would be invalid. Just return a `String`. – Herohtar May 27 '22 at 20:40
  • A string literal (`"I am a string literal" `) is a `&'static str`, similar to how `&*String::new()` is a `&'a str`, but the static lifetime can live for the entire runtime of the program. Does replacing your `&String::from(...)` with a string literal help with the lifetimes? – MeetTitan May 27 '22 at 20:40
  • You can create a temporary variable as the full error message states later: `"note: consider using a \`let\` binding to create a longer lived value"`. – Dogbert May 27 '22 at 20:50
  • @ChayimFriedman It certainly helps! – heapOverflow May 29 '22 at 07:51

1 Answers1

-1

How do I use unwrap_or to return a string reference?

Short answer: you do not unless is a &'static str

For your case, you are creating an String already, so return a String and then use it whenever you need the &str. Also as nitpick, you would probably should use unwrap_or_else to avoid allocating the String in case the item is found:

let map: HashMap<String, String> = HashMap::new();
let v = map.get(&key).unwrap_or_else(|| String::from("key not found"));
println!("{}", v);
Netwave
  • 40,134
  • 6
  • 50
  • 93
  • This code seems to just generate the error `expected '&String', found struct 'String' help: consider borrowing here: "&String::from("key not found")"`. If I try to create a reference to the return string I'd still get an error `temporary value created here returns a reference to data owned by the current function`. – heapOverflow May 29 '22 at 07:45
  • @heapOverflow, you have your types wrong. From your comment, you have this in a function, which you didn't include in your question. Also, you shouldn't return a String as an error, better to use `Result`. One more thing, you may take a look at `Cow`, is probably another thing you could use as result. – Netwave May 29 '22 at 08:10
  • I've fixed my code to make clearer what the problem is – heapOverflow May 29 '22 at 15:42
  • @heapOverflow, please read the answer, I clearly explained you can not use str unless they are &’static, nor you added any useful information to your question – Netwave May 29 '22 at 16:09