Skip to content

Mistake about pointer conversion in borrowed pointers chapter #150

@xphade

Description

@xphade

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions