struct Person {
name: String,
}
pub struct Message<T> (pub T);
impl<T> Message<T> {
/// 测试1
pub fn ok() -> Self {
// 报错
Self::new(Person {
name: "Sam".to_owned(),
})
}
pub fn new(data: T) -> Self {
Self(data)
}
}
/// 测试2
pub fn ok() {
// 正常
Message::new(Person {
name: "Sam".to_owned(),
});
}
测试2代码块正常,测试1代码块会报错,报错内容如下:
mismatched types
expected type parameter, found struct `Person`
note: expected type `T`
found type `Person`rustc(E0308)
message.rs(14, 19): expected type parameter, found struct `Person`
RUST版本1.38.0
怎样修改才能使测试1代码块不报错?求指教,感谢!
1
共 3 条评论, 1 页
评论区
写评论感谢@yinyanlv,原来是返回类型的问题,明白了
卡住了,发了两遍-_-!