This code doesn't compile:
let y = {
let x = "foo".to_owned();
&x
};
dbg!(y);
But this code does:
let y = {
let x = "foo".to_owned();
&x.clone()
};
dbg!(y);
I thought we were not able to return reference to local variables, but in this case we can. x.clone()
is created inside the block, but we can get a reference to it outside the block. Yet we are not able to do the same on x
. What is happening here?