Skip to content

Rust claims that Ok(T) is the same as &T #54578

Closed
@sgrif

Description

@sgrif

Steps to reproduce:

  • Have a function that takes a reference
  • Call that function passing an expression using ?
  • Forget an &

Example code: https://play.rust-lang.org/?gist=c75cddbb4c5d8a1b63ec11dc3c5bca43&version=stable&mode=debug&edition=2015

Output:

error[E0308]: try expression alternatives have incompatible types
 --> src/main.rs:5:9
  |
5 |     foo(bar()?);
  |         ^^^^^^
  |         |
  |         expected &i32, found i32
  |         help: try wrapping with a success variant: `Ok(bar()?)`
  |
  = note: expected type `&i32`
             found type `i32`

Expected output:

error[E0308]: mismatched types
 --> src/main.rs:5:9
  |
5 |     foo(bar()?);
  |         ^^^^^^
  |         |
  |         expected &i32, found i32
  |         help: consider borrowing here: `&bar()?`
  |
  = note: expected type `&i32`
             found type `i32`

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions