gpt4 book ai didi

What is this: `impl Trait for T {}`?(这是什么:`Impll特征for T{}`?)

转载 作者:bug小助手 更新时间:2023-10-27 21:01:32 25 4
gpt4 key购买 nike



Recently, I saw different places where this was given as example code, and writing it in my editor, it seems to be valid code.

最近,我看到了不同的地方,这是作为示例代码给出的,在我的编辑器中编写它,它似乎是有效的代码。


trait Trait {}

impl<T> Trait for T {}

I can't seem to find any documentation explaining this; I'm sure it exists, but I don't really know what to google.

我似乎找不到任何解释这一点的文档;我确信它存在,但我真的不知道要用谷歌搜索什么。


How would you instantiate T to and then call any methods within the impl block?

如何将T实例化到Impl块中,然后调用其中的任何方法?


I'm sure there is a good reason for this, but I can't seem to wrap my head around it, would anyone be able to link the relevant documentation for this feature in rust?

我相信这是有很好的理由的,但我似乎不能理解它,有谁能够链接到Ruust中这个特性的相关文档?




I realise from the answers that I may have not been clear; I had copied the example from what I’ve seen.

我从答案中意识到我可能没有说清楚;我从我所看到的例子中复制了这个例子。


Though from the first answer, I realised that the below code is also valid, and perhaps a more clear representation of what I’m trying to ask, which is, what is the use for the generics syntax that allows this code to be valid:

虽然从第一个答案中,我意识到下面的代码也是有效的,并且可能更清楚地表达了我试图询问的问题,即允许此代码有效的泛型语法有什么用处:


trait Trait {
fn test() -> String {
"test".to_string()
}
}

impl<T> Trait for T {}

Does this affect existing structs and enums in the file, or does it literally just do absolutely nothing, and can never be called or used?

这是否会影响文件中现有的结构和枚举,或者它实际上什么都不做,永远不能调用或使用?


更多回答

Not all legal code has a practical use; sometimes things are examples that need to be extended (in this case, by defining actual concrete types) before they have utility. Note, though, that all Stack Overflow questions are expected to be practical in nature, about a "specific problem you actually face". Certainly, a trait itself is not a thing that can be instantiated on its own. impl trait lets you define functions that called with anything implementing that trait, but you still need a concrete type for any particular such "anything" to be before instantiation can take place.

并不是所有的法律代码都有实际用途;有时,在它们具有实用性之前,需要对其进行扩展(在本例中,通过定义实际的具体类型)。不过,请注意,所有堆栈溢出问题本质上都是实用的,是关于“您实际面临的特定问题”的。当然,特征本身不是可以自己实例化的东西。Iml特征允许您定义用实现该特征任何对象调用的函数,但在进行实例化之前,您仍然需要为任何特定的此类“对象”指定一个具体类型。

The code in your edit isn't valid, so it isn't clear what you're asking.

您编辑的代码无效,因此不清楚您请求的内容。

@interjay Ah yes, you are right, rust-analyzer was being slow, i've updated the edit section, this code definitely does compile and run.

@interjay啊是的,你是对的,铁锈分析器速度很慢,我已经更新了编辑部分,这段代码确实可以编译和运行。

优秀答案推荐

This code is showing you two things: marker traits and blanket implementations.

这段代码向您展示了两件事:标记特征和全面实现。


trait Foo {} (with no trait members) declares a marker trait which serves only to tag certain types. The built-in traits Copy, Send, and Sync are good examples of marker traits.

特征foo{}(没有特征成员)声明了一个标记特征,该标记特征仅用于标记某些类型。内置特征Copy、Send和Sync就是标记特征的很好例子。


impl<T> Foo for T {} implements the trait Foo for all types, a so-called blanket implementation.

Iml Foo for T{}为所有类型实现了特征Foo,即所谓的一揽子实现。


Together, this is pretty much redundant, because there isn't a difference between "type that implements Foo" and "all types."

总而言之,这几乎是多余的,因为“实现foo的类型”和“所有类型”之间没有区别。


However, seemingly-useless blanket implementations can actually be useful in practice. In the Rust standard library, there is a blanket implementation impl<T> From<T> for T which tells Rust "you can convert any T to T." This is not useful on its own, but winds up being useful in generic code, such as fn foo(arg: impl Into<i32>) which lets you pass an argument of any type that can be infallibly converted to i32. Because of the blanket implementation, this includes i32 itself.

然而,看似毫无用处的全面实现实际上可能是有用的。在Rust标准库中,有一个全面的实现impl from for T,它告诉Rust“您可以将任何T转换为T”。这本身并不有用,但在泛型代码中很有用,比如fn foo(arg:impl into ),它允许您传递可以正确地转换为I32的任何类型的参数。由于全面实现,这包括I32本身。



How would you instantiate T to then call any methods within the impl block?



Well, the body of the impl block can't have anything in this case. The body of an impl ... for block is to implement trait members, but the trait being implemented has no members.

在这种情况下,Impl块的主体不能有任何东西。一辆汽车的车身...块是实现特征成员,但要实现的特征没有成员。


If we assume that the trait does have members, then the answer to your question would be that you would have to bound T in a way that lets you perform operations on it. For example, to create a T you could choose to add the bound T: Default which would let you call T::default() to create an instance of T with a (type-defined) default value.

如果我们假设特征确实有成员,那么您的问题的答案将是您必须以一种允许您对其执行操作的方式来绑定T。例如,要创建一个T,您可以选择添加绑定的T:Default,这将允许您调用T::Default()来创建具有(类型定义的)缺省值的T的实例。


Note that adding bounds on T would change the scope of the implementation to only apply to types that satisfy the bounds. This is very common in practice. For example, the implementation of Clone on Vec<T> is bounded on T: Clone. (How would you clone a vector if you can't clone its elements, after all?)

请注意,在T上添加边界会将实现的范围更改为仅应用于满足边界的类型。这在实践中是很常见的。例如,在VEC 上实现Clone是在T:Clone上绑定的。(如果你毕竟不能克隆载体的元素,你怎么克隆它呢?)




Further reading:

进一步阅读:



更多回答

Hi, this is a really good answer, but you made me realise I probably hadn't made my question clear enough, so i've added an edit, thanks!

嗨,这是一个非常好的答案,但你让我意识到我可能没有把我的问题说得足够清楚,所以我添加了一个编辑,谢谢!

@StefanKyriacou The answer to that question is different. The T: Default bound isn't necessary and doesn't really accomplish anything. Both Rust and English have a grammar, and you can construct useless but grammatically-correct sentences in both. The impl block applies to all types that implement Default, even though nothing ever uses the Default implementation.

@Stefan Kyriacou这个问题的答案是不同的。T:Default界限不是必需的,也不会真正完成任何事情。Rust和English都有语法,你可以在这两种语言中构建无用但语法正确的句子。Impl块适用于实现Default的所有类型,即使从来没有使用默认实现的类型。

Yes, I now realise that adding the Default constraint is different, the specific thing I was looking for was the term "blanket implementation", that's the part I was missing, and is the very last part of the link you provided, plus your answer was all round very helpful, so i'm going to mark it as the answer, thank you!

是的,我现在意识到添加默认约束是不同的,我正在寻找的具体内容是术语“全面实现”,这是我遗漏的部分,也是您提供的链接的最后一部分,加上您的答案非常有帮助,所以我将把它标记为答案,谢谢!

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