Skip to content
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

example in chapter Ownership seems not correct #87

Closed
Gnosnay opened this issue Dec 29, 2022 · 4 comments
Closed

example in chapter Ownership seems not correct #87

Gnosnay opened this issue Dec 29, 2022 · 4 comments

Comments

@Gnosnay
Copy link

Gnosnay commented Dec 29, 2022

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.

@Gnosnay
Copy link
Author

Gnosnay commented Dec 29, 2022

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

@mgeisler
Copy link
Collaborator

mgeisler commented Jan 2, 2023

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.

@Gnosnay
Copy link
Author

Gnosnay commented Jan 5, 2023

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.

@mgeisler
Copy link
Collaborator

mgeisler commented Jan 5, 2023

Thanks, I'll close it for now.

@mgeisler mgeisler closed this as not planned Won't fix, can't repro, duplicate, stale Jan 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants