I suppose this is quite a basic question, but I am very confused -- how come this function compiles (foo_ref)?
Here is how I understand it:
-
foo_refcreates aFoostruct on the stack -
foo_refdoes not return a copy of the struct (since its signature demands a ref) - So
foo_refreturns a reference, but it is a reference to a variable on the stack -- how is this possible?
Is there some coercion/magic here?
struct Foo<'a> {
name: &'a str,
}
fn foo_ref<'a> () -> &'a Foo<'a> {
&Foo{
name: "Alice"
}
}
fn use_foo_ref() {
println!("{}", foo_ref().name);
}