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

Mistake about pointer conversion in borrowed pointers chapter #150

Open
xphade opened this issue Mar 22, 2024 · 0 comments
Open

Mistake about pointer conversion in borrowed pointers chapter #150

xphade opened this issue Mar 22, 2024 · 0 comments

Comments

@xphade
Copy link

xphade commented Mar 22, 2024

I'm a rust noob, so please tell me if I'm misunderstanding anything here, but:

In the "Borrowed pointers" chapter, when you talk about automatic referencing, you give the following example and explain that pointer types are automatically converted to references:

fn foo(x: &i32) { ... }

fn bar(x: i32, y: Box<i32>) {
    foo(&x);
    // foo(x);   // Error - expected &i32, found i32
    foo(y);      // Ok
    foo(&*y);    // Also ok, and more explicit, but not good style
}

However, when trying this, the foo(y) line produces the following error:

error[E0308]: mismatched types
   --> src/main.rs:132:9
    |
132 |     foo(y); // Ok
    |     --- ^ expected `&i32`, found `Box<i32>`
    |     |
    |     arguments to this function are incorrect
    |
    = note: expected reference `&i32`
                  found struct `Box<i32>`
note: function defined here
   --> src/main.rs:127:4
    |
127 | fn foo(x: &i32) {}
    |    ^^^ -------
help: consider borrowing here
    |
132 |     foo(&y); // Ok
    |         +

For more information about this error, try `rustc --explain E0308`.

So is this statement and the example wrong?

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

1 participant