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

match arms differ in mutability #18657

Closed
bfops opened this issue Nov 5, 2014 · 2 comments
Closed

match arms differ in mutability #18657

bfops opened this issue Nov 5, 2014 · 2 comments

Comments

@bfops
Copy link
Contributor

bfops commented Nov 5, 2014

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?

@steveklabnik
Copy link
Member

Rust does not do this kind of implicit casting, no. It could be done, but that would require an RFC to change. Please pursue one if you'd like to add this to Rust, thanks!

@bfops
Copy link
Contributor Author

bfops commented Jan 27, 2015

Ah turns out this has been fixed. I'm not sure which RFC touched this behavior, but the first version works now.

lnicola pushed a commit to lnicola/rust that referenced this issue Dec 11, 2024
…iant

minor: Migrate `generate_enum_variant` to `SyntaxEditor`
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