3 ms·
This is important for soundness; it's shadowing, not replacing let s = String::from("hello"); let s = &s[..]; Here, the second s refers to the first, and
by steveklabnik 10y ago
This is important for soundness; it's shadowing, not replacing
let s = String::from("hello");
let s = &s[..];
Here, the second s refers to the first, and if it were go to out of scope right away, would be dangling.
let s = String::from("hello");
{
let s = &s[..3];
// here, s is "hell"
}
// here, it's the full string again
This falls out of the usual scoping rules, basically. You can think of any variable introducing a new scope where it's valid; if you don't close it yourself, it's closed at the end of the enclosing scope.