在match中,经常会碰到ref,此时的ref是对某值的借用,而不是匹配。如果按纯匹配的角度看,不容易理解。
fn main() {
// 情景1:匹配
let t_1 = (42, "i love rust language".to_string());
let (n, s) = t_1;
// t_1 has been moved. It is No More
// n: 42, s:"i love rust language".to_string()
// 情景2:借用
let t_2 = (42, "i love rust language".to_string());
// nn and ss are borrowed from t_2. It still lives!
//nn: &42, ss:&"i love rust language"
// 情景3:借用
let ot = Some((42, "hello world".to_string()));
if let Some((_, ref s)) = ot {
assert_eq!(s, "hello world");
}
// s: 此时是表示对“hello world”.to_string()的借用,即“hello world”
// 并不是说要让ref s ="hello world.to_string()"进行模式匹配;
thread::sleep_ms(500000);
}
另外,如果我们来看看这种情况:
fn match_tuple_0(t: (i32, String)) {
let text = match t {
(0, s) => format!("zero {}", s),
(1, ref s) if s == "hello" => format!("hello one!"),
tt => format!("no match {:?}", tt),
// or say _ => format!("no match") if you're not interested in the value
};
println!("{}", text);
}
显然是可以编译的。 运行match_tuple_0(1,“hello”.to_string()), 输出“hello one”;
关于ref mut
let mut robot_name = Some(String::from("Bors"));
match robot_name {
Some(ref mut name) => *name = String::from("Another name"),
None => (),
}
println!("robot_name is: {:?}", robot_name);
但是:
fn match_tuple_1(t: (i32, String)) {
let text = match t {
(0, s) => format!("zero {}", s),
(0, ref s) if s == "hello" => format!("hello one!"),
tt => format!("no match {:?}", tt),
// or say _ => format!("no match") if you're not interested in the value
};
println!("{}", text);
}
也是可以。运行match_tuple_1(0,“hello”.to_string()), 输出什么?
结果是:“zero hello”, 为什么不会输出“hello one”? 因为被拦截了。