gpt4 book ai didi

rust - 派生特征会导致意外的编译器错误,但手动实现有效

转载 作者:行者123 更新时间:2023-11-29 08:09:41 24 4
gpt4 key购买 nike

这段代码(playground):

#[derive(Clone)]
struct Foo<'a, T: 'a> {
t: &'a T,
}

fn bar<'a, T>(foo: Foo<'a, T>) {
foo.clone();
}

...不编译:

error[E0599]: no method named `clone` found for struct `Foo<'a, T>` in the current scope
--> src/main.rs:16:9
|
3 | struct Foo<'a, T: 'a> {
| ---------------------
| |
| method `clone` not found for this
| doesn't satisfy `Foo<'_, T>: std::clone::Clone`
...
16 | foo.clone();
| ^^^^^ method not found in `Foo<'a, T>`
|
= note: the method `clone` exists but the following trait bounds were not satisfied:
`T: std::clone::Clone`
which is required by `Foo<'_, T>: std::clone::Clone`
help: consider restricting the type parameter to satisfy the trait bound
|
3 | struct Foo<'a, T: 'a> where T: std::clone::Clone {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^

添加 use std::clone::Clone; 不会改变任何东西,因为它已经在前奏中了。

当我删除 #[derive(Clone)] 并为 Foo 手动实现 Clone 时,它按预期编译!

impl<'a, T> Clone for Foo<'a, T> {
fn clone(&self) -> Self {
Foo {
t: self.t,
}
}
}

这是怎么回事?

  • #[derive()]-impls 和手动实现有区别吗?
  • 这是编译器错误吗?
  • 还有什么我没有想到的?

最佳答案

答案隐藏在错误信息中:

    = note: the method `clone` exists but the following trait bounds were not satisfied:
`T: std::clone::Clone`
which is required by `Foo<'_, T>: std::clone::Clone`

当您派生 Clone(以及许多其他自动派生的类型)时,它会添加一个绑定(bind)到所有 泛型类型的 Clone。使用 rustc -Z unstable-options --pretty=expanded,我们可以看到它变成了什么:

impl <'a, T: ::std::clone::Clone + 'a> ::std::clone::Clone for Foo<'a, T> {
#[inline]
fn clone(&self) -> Foo<'a, T> {
match *self {
Foo { t: ref __self_0_0 } =>
Foo{t: ::std::clone::Clone::clone(&(*__self_0_0)),},
}
}
}

这种情况下,不需要绑定(bind),因为泛型类型在引用后面。

现在,您需要自己实现CloneThere's a Rust issue for this ,但这是一个相对罕见的解决方法。

关于rust - 派生特征会导致意外的编译器错误,但手动实现有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40096841/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com