Lifetimes magically coerced to a common one?
⚓ Rust 📅 2025-11-23 👤 surdeus 👁️ 11I'm confused that this compiles:
fn either_string<'a>(b: bool, str1: &'a str, str2: &'a str) -> &'a str {
if b { str1 } else { str2 }
}
fn test_subtyping<'a, 'b>(b: bool, str1: &'a str, str2: &'b str) -> String {
let x = either_string(b, str1, str2);
String::from(x)
}
fn main() {
println!("{}", test_subtyping(true, "foo", "bar"));
}
What's the magic that makes either_string(b, str1, str2) compile even though str1 and str2 have different lifetimes without a bound between them? There is no type annotation I could put on x that would keep it compiling, right? Is there some kind of heuristic that turns this into fn test_subtyping<'c, 'a: 'c, 'b: 'c> { let x : &'c str = ... }?
4 posts - 4 participants
🏷️ Rust_feed