Closed
Description
when matching &str in nightly or beta the following code always returns the first match case:
#[derive(Debug, PartialEq)]
enum Reg {
EAX,
EBX,
ECX,
EDX,
ESP,
EBP,
ISP,
}
fn string_to_reg(_s:&str) -> Reg {
match _s.as_ref() {
"EAX" => Reg::EAX,
"EBX" => Reg::EBX,
"ECX" => Reg::ECX,
"EDX" => Reg::EDX,
"EBP" => Reg::EBP,
"ESP" => Reg::ESP,
"ISP" => Reg::ISP,
&_ => panic!("bla bla bla"), //removing this "&" fixes it
}
}
fn main() {
let vec = vec!["EAX", "EBX", "ECX", "EDX", "ESP", "EBP", "ISP"];
let mut iter = vec.iter();
println!("{:?}", string_to_reg(""));
println!("{:?}", string_to_reg(iter.next().unwrap()));
println!("{:?}", string_to_reg(iter.next().unwrap()));
println!("{:?}", string_to_reg(iter.next().unwrap()));
println!("{:?}", string_to_reg(iter.next().unwrap()));
println!("{:?}", string_to_reg(iter.next().unwrap()));
println!("{:?}", string_to_reg(iter.next().unwrap()));
println!("{:?}", string_to_reg(iter.next().unwrap()));
}
Expected behaviour is returning the fitting case. This can be fixed by removing the "&" in front of the last case.
Meta
tested on my machine with: rustc 1.11.0-nightly (bb4a79b 2016-06-15)
tested on here with beta and nightly showing the bug and stable giving a runtime error.