Rust 取消引用强制
例子
给定两种类型Tand U,&T将强制(隐式转换)为&U当且仅当T实现Deref<Target=U>
这允许我们做这样的事情:
fn foo(a: &[i32]) {// 代码
}
fn bar(s: &str) {
// 代码
}
let v = vec![1, 2, 3];
foo(&v); // &Vec<i32> coerces into &[i32] because Vec<T> impls Deref<Target=[T]>
let s = "Hello world".to_string();
let rc = Rc::new(s);
// This works because Rc<T> impls Deref<Target=T> ∴ &Rc<String> coerces into
// &String which coerces into &str. This happens as much as needed at compile time.
bar(&rc);
以上是 Rust 取消引用强制 的全部内容, 来源链接: utcz.com/z/327483.html