我有以下结构:
struct Age(u8);
struct User {
age: Age,
}我想将User类型的借来的值强制转换为&Age,如下所示:
let s = String::from("abcd");
let z: &str = &s; // This works
let b = Box::new(Age(8));
let a: &Age = &b; // This also worksBorrow提到,A类型可以作为其他类型的B借用,如果A实现了Borrow<B>,所以我尝试为User实现Borrow<Age>。
use std::borrow::Borrow;
impl Borrow<Age> for User {
fn borrow(&self) -> &Age {
&self.age
}
}
fn main() {
let u = User { age: Age(8) };
let a: &Age = u.borrow(); // Works
let a: &Age = &u; // Error
}这是我得到的错误消息:
error[E0308]: mismatched types
--> src/main.rs:23:19
|
23 | let a: &Age = &u; // Error: expected struct `Age`, found struct `User`
| ^^ expected struct `example::Age`, found struct `example::User`
|
= note: expected type `&example::Age`
found type `&example::User`我是不是遗漏了什么?我如何强迫&u进入&Age?
发布于 2019-01-31 18:07:46
使用Box和String的两个示例之所以起作用不是因为Borrow特性,而是因为Deref特性。示例中的框&b可以强制进入&Age,因为Box<Age>实现了Deref<Target = Age>。类似地,String有一个Deref实现,因此可以将字符串引用&s强制转换为&str。它将以完全相同的方式为您的User工作:
use std::ops::Deref;
impl Deref for User { // That's bad, don't do that.
type Target = Age;
fn deref(&self) -> &Age {
&self.age
}
}在此范围内,现在可以编译以下内容:
let u = User { age: Age(8) };
let a: &Age = &u;请注意,这个示例有些人为,而不是惯用的:没有人会期望User的行为像指向Age的指针类型。同样,没有人会期望user.borrow()借用Age而不是User,这不是Borrow特性的目的。
https://stackoverflow.com/questions/54466319
复制相似问题