![](/img/trans.png)
[英]Why does Rust "From" trait implementation give lifetime error, but custom trait does not?
[英]Why does Rust assignment of const from trait to trait not work?
我正在尝试将一个特征中定义的关联常量分配给另一个无法按预期工作的特征。 这是一个包含 3 个特征和一个结构的最小示例:
trait A {
const X: i32 = 1;
}
struct S;
impl A for S {}
trait B {
const Y: i32 = A::X;
}
trait C {
const Y: i32 = S::X;
}
fn main() {}
对应的编译错误是:
error[E0283]: type annotations required: cannot resolve `_: A`
--> src/main.rs:10:20
|
10 | const Y: i32 = A::X;
| ^^^^
|
note: required by `A::X`
--> src/main.rs:2:5
|
2 | const X: i32 = 1;
| ^^^^^^^^^^^^^^^^^
E0283
的解释告诉我代码所揭示的内容:我可以从具体类型进行分配,但不能从特征本身进行分配。 但是在E0283
示例使用未定义函数的地方,我有一个已经定义的值。 为什么会这样,如何绕过?
问题是任何实现A
struct
都可以为X
定义自己的值。 因此,在trait B
的上下文中简单地声明A::X
并不能为编译器提供足够的信息,说明应该选择A
哪个impl
。
如果您希望impl
B
东西也impl
A
,您可以尝试以下操作(我手头没有编译器,但想法应该很清楚):
trait B : A {
const Y: i32 = <Self as A>::X;
}
Traits 应该由一个具体的类型来实现,而不应该自己定义一个不能在实现者中改变的常量。 您指定的是默认值,而不是所有实现者必须遵守的值。 如果所有类型都必须具有相同的X
值,则不需要特征。
因此A::X
不是一个明确定义的值。
这是一个说明原因的示例:
trait A {
const X: i32 = 1;
}
struct S;
impl A for S {}
struct R;
impl A for R {
const X: i32 = 42;
}
fn main() {
println!("S: {}", S::X);
println!("R: {}", R::X);
println!("S: {}", <S as A>::X); // A::X alone is ambiguous
println!("R: {}", <R as A>::X);
}
您正在做的类似于尝试在特征上调用默认函数,这里是错误 E0283:
trait A {
fn get_x() -> i32 {
1
}
}
struct S;
impl A for S {}
struct R;
impl A for R {
fn get_x() -> i32 {
42
}
}
fn main() {
// A::get_x() is ambiguous but there are not:
println!("S: {}", S::get_x());
println!("R: {}", R::get_x());
println!("S: {}", <S as A>::get_x());
println!("R: {}", <R as A>::get_x());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.