Skip to content

Can turn immutable into mut with ref mut #52240

Closed
@Esption

Description

@Esption

This lets you get a mutable reference to an immutable value under a specific case.

Code:

enum Foo {
	Bar(i32),
}

fn main() {
	let arr = vec!(Foo::Bar(0));
	if let (Some(Foo::Bar(ref mut val)), _) = (&arr.get(0), 0) {
		*val = 9001;
	}
	match arr[0] {
		Foo::Bar(ref s) => println!("{}", s)
	}
}

From what I can tell, the tuple and at least two depths of Enums are necessary, along with an & on the right side. There's probably other variations, but that seems to work well enough.

Expected output would be a compile error, instead output is 9001

This works on both stable rustc 1.27.1 (5f2b325f6 2018-07-07) and nightly rustc 1.29.0-nightly (e5f6498d3 2018-07-10)

Metadata

Metadata

Assignees

No one assigned

    Labels

    E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions