I've discovered some behavior I cannot explain. I've written this function:
fn main() {
let word_one = "ONE";
let x;
{
let word_two = "THREE";
x = get_ref(&word_one, &word_two);
}
println!("{}", x);
}
fn get_ref<'a>(a: &'a str, b: &'a str) -> &'a str {
if a.chars().count() >= b.chars().count() {
return a;
}
return b;
}
This function is possible, even if the lifetime of word_two
isn't long enough to build a reference. Otherwise this example isn't possible:
fn main() {
let word_one = "ONE";
let x;
{
let word_two = "THREE";
x = &word_two;
}
println!("{}", x);
}
Unfortunately I've discovered nothing in the docs which would explain this behavior.