You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Seems like there's a lot going on, and as a beginner, although I've read the majority of the Rust book, I'm a bit confused.
First, a new immutable vector is declared - vec0.
Then on the next line, you pass ownership of vec0 to the fill_vec function. Simple enough.
Inside fill_vec, you somehow try to convert the original immutable vector into a mutable one? Sure, you have ownership, but is that even possible? Can you convert an immutable variable into a mutable one after it's been declared?
Or is it just declaring a completely new vec variable inside the fill_vec function that happens to have the same name (shadows) the original, and sets its value to the value of the original (a blank vector), thereby cloning it in the heap? I guess since the function now owns the original, as soon as the function returns, it gets dropped (or is it dropped as soon as it gets shadowed?), and the memory gets de-allocated, leaving only the new vector that is returned by the function?
Am I close?
The text was updated successfully, but these errors were encountered:
I had a similar question about the hint in this exercise. It states:
So vec0 is being moved into the function fill_vec when we call it on line 10, which means it gets dropped at the end of fill_vec, which means we can't use vec0 again on line 13
Does it really get dropped at the end of the function? I was under the impression that ownership is transferred on assignment, so isn't it transferred to the shadow variable on the first line of the body? And then at the end of the function, isn't ownership transferred out?
I'm confused by the hint stating it is dropped at the end of the function.
Seems like there's a lot going on, and as a beginner, although I've read the majority of the Rust book, I'm a bit confused.
First, a new immutable vector is declared - vec0.
Then on the next line, you pass ownership of vec0 to the fill_vec function. Simple enough.
Inside fill_vec, you somehow try to convert the original immutable vector into a mutable one? Sure, you have ownership, but is that even possible? Can you convert an immutable variable into a mutable one after it's been declared?
Or is it just declaring a completely new
vec
variable inside the fill_vec function that happens to have the same name (shadows) the original, and sets its value to the value of the original (a blank vector), thereby cloning it in the heap? I guess since the function now owns the original, as soon as the function returns, it gets dropped (or is it dropped as soon as it gets shadowed?), and the memory gets de-allocated, leaving only the new vector that is returned by the function?Am I close?
The text was updated successfully, but these errors were encountered: