-
Notifications
You must be signed in to change notification settings - Fork 10.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
move_semantics3: Why can I pass to a function an argument that is immutable and have it be received as a value that is mutable? #631
Comments
It is not clear to me neither. |
To my point of view, passing a variable without |
But I think the point is that |
Well, if we pass a reference, things will like what you say. But for ownership, things are different. let mut var = val; // create a mutable object let var = val;
let mut var = var; // change make variable mutable |
I see, thank you very much! But I still don't get how can we move
since |
Can someone explain this? This seems insane to me. From what I understand, the underlying memory in use to store values of variables is never immutable. Every memory address can technically be written to. Immutability is an artificial constraint. When I say:
I'm saying that I want a variable in memory with the value 5, and I don't want it changed by any code later on. If I try to change this variable at some point, give me an error. This is me defining the constraint. If you can later just do:
make it mutable, and change it, this seems to defeat the entire purpose of immutable variables. At that point, you might as well just make all variables mutable, because immutability is not guaranteed. At least with constants, there's a guarantee of immutability. The purpose of a regular immutable variable is unclear. |
For those curious, I posted the same question here to reach a wider audience. |
Closing as stale. |
Hi, you could help me learn Rust through Rustlings solving this question on mutability and moving.
So here's the code in question. Notice vec0 is immutable and I'm passing it to fill_vec, which by definition receives mutable vectors.
So v0 changes ownership. It goes from main as an immutable to fill_vec as a mutable. How does this work?
Given the Rustlings hint for this question ("You can [...] add
mut
in one place that will change an existing binding to be a mutable binding instead of an immutable one"), bindings can vary. Fair enough, but what are they binding to? Is it a bind from a symbol to a pointer?I'm not sure if I missed something in the Klabnik Nichols book on how to think about this, but if my interpretation is correct, thanks, Rustlings!
The text was updated successfully, but these errors were encountered: