Skip to content

match arms differ in mutability #18657

Closed
Closed
@bfops

Description

@bfops

This doesn't compile (error: match arms have incompatible types: expected &int, found &mut int (values differ in mutability))

fn main() {
    let mut x: int = 1;
    let opt: Option<int> = None;
    let rx: &int =
      match opt {
        Some(_) => {
          &x
        },
        None => {
          &mut x
        },
      };
    println!("{}", *rx);
}

But this works:

fn main() {
  let mut x: int = 1;
    let opt: Option<int> = None;
    let rx: &int =
      match opt {
        Some(_) => {
          &x
        },
        None => {
          let y: &int = &mut x;
          y
        },
      };
    println!("{}", *rx);
}

Should this "cast" be able to happen implicitly?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions