let s: String = String::from("some message");
let b: Box<&String> = Box::<&String>::new(&s);
drop(s);
let another_box = b; // error! b (which borrows s) is used later!
So, above code looks very simple.
b is clearly borrowing s, in the human sense.
But, I think this is quite magical: How can the compiler knows that without any lifetime statements?
According to the Rust's standard library,
Box's declaration is as follows:
pub struct Box<T, A = Global>(_, _)
where A: AllocRef, T: ?Sized;
And what I think what it really is is as follows:
pub struct Box<'a, T, A = Global>(_, _)
where A: AllocRef, T: ?Sized + 'a;
I don't think that the lifetime(or the borrowing) moves automatically for all the situations where a struct has a generic paramter type. Or is it??