Rust 取消引用强制
例子
给定两种类型TandU,&T将强制(隐式转换)为&U当且仅当T实现Deref
这允许我们做这样的事情:
fn foo(a: &[i32]) {
//代码
}
fn bar(s: &str) {
//代码
}
let v = vec![1, 2, 3];
foo(&v); // &Vec coerces into &[i32] because Vec impls Deref
let s = "Hello world".to_string();
let rc = Rc::new(s);
// This works because Rc impls Deref ∴ &Rc coerces into
// &String which coerces into &str. This happens as much as needed at compile time.
bar(&rc);
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短