comprehensive-rust
comprehensive-rust copied to clipboard
example in chapter `Ownership` seems not correct
In Ownership chapter we give one example to indicate that variable will be dropped at the end of scope.
struct Point(i32, i32);
fn main() {
{
let p = Point(3, 4);
println!("x: {}", p.0);
}
println!("y: {}", p.1);
}
But actually the rustc’s complaining is
Blocking waiting for file lock on package cache
Compiling exercise v0.1.0 (/home/coder/project/exercise)
error[E0425]: cannot find value `p` in this scope
--> src/main.rs:8:23
|
8 | println!("y: {}", p.1);
| ^ not found in this scope
For more information about this error, try `rustc --explain E0425`.
error: could not compile `exercise` due to previous error
I believe this example may cause confusion.
I believe here we need to introduce what the end of scope is. E.g. the end of one code block, one pointer transfer to another function e.t.c
Hi @Yansongsongsong,
You are completely correct that the error message is about scopes and that the instructor will have to explain what a scope is at this point. That is normally super easy, though, since the course participants already know how to program. To my knowledge, a scope in Rust is the same as a scope in C++ (please correct me if I'm wrong?) and so I normally tell the students that.
To me, ownership is mostly a matter of who should deallocate this value when it goes out of scope? So that is why I start the discussion of borrowing and ownership with scopes.
We should eventually write something about this into speaker notes #53.
Hi @Yansongsongsong,
You are completely correct that the error message is about scopes and that the instructor will have to explain what a scope is at this point. That is normally super easy, though, since the course participants already know how to program. To my knowledge, a scope in Rust is the same as a scope in C++ (please correct me if I'm wrong?) and so I normally tell the students that.
To me, ownership is mostly a matter of who should deallocate this value when it goes out of scope? So that is why I start the discussion of borrowing and ownership with scopes.
We should eventually write something about this into speaker notes #53.
hi @mgeisler , Thank you for your reply. That sounds great. Looking forward to new version with spears notes!
Feel free to close this issue at any time.
Thanks, I'll close it for now.