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 家庭新春聚餐祝福语简短
18 当选团员祝福语简短精辟