TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Rust Any part 3: we have upcasts

174 点作者 jmillikin大约 2 个月前

7 条评论

kibwen大约 1 个月前
<i>&gt; Even though DebugAny inherits from Any</i><p>I&#x27;m going to push back against this terminology for the sake of people who don&#x27;t know Rust and are coming from traditional OO languages.<p>Rust traits don&#x27;t &quot;inherit&quot; from other traits, they &quot;require&quot; other traits.<p>So if I have two traits, where one requires the other:<p><pre><code> trait Foo {} trait Bar: Foo {} </code></pre> That doesn&#x27;t add Foo&#x27;s methods to Bar&#x27;s list of methods, like you might expect from the term &quot;inheritance&quot;.<p>Instead, it just means that it&#x27;s not possible to implement Bar for a type unless Foo is also separately implemented for that type.<p>Despite this it&#x27;s still accurate to say that this enables a supertype&#x2F;subtype relationship in certain contexts, but be careful because Rust isn&#x27;t a traditionally OO language, so that may not always be the most useful framing.
评论 #43526624 未加载
评论 #43525513 未加载
评论 #43525670 未加载
评论 #43525261 未加载
评论 #43528909 未加载
mmastrac大约 2 个月前
The important change here appears to be that the internal representation of a vtable is now guaranteed to have its supertraits laid out in some predictable prefix form to its own methods.<p>EDIT: or if this is not possible, a pointer to the appropriate vtable is included. I assume this must be for diamond trait inheritance.
评论 #43524132 未加载
评论 #43527701 未加载
trashface大约 1 个月前
This example shows how it works for one trait, Debug, but what if you have a type that (might) implement multiple traits A, B, and&#x2F;or C? It isn&#x27;t clear to me if that is possible, unless the type implements all of those traits. What I&#x27;d like to do is have some base trait object and query it to see if it supports other interfaces, but also not have to have stub &quot;I don&#x27;t actually implement this&quot; trait impls for the unsupported ones. A bit like how I might use dynamic_cast in c++.<p>(I believe I understand that in rust this has not historically been possible because rust doesn&#x27;t have inheritance, so, there can be only one vtable for a type, rather than an chain like you might have in c++.)
评论 #43526830 未加载
评论 #43525232 未加载
评论 #43525234 未加载
评论 #43525659 未加载
fcantournet大约 2 个月前
Any are you Debug ? Are you DebugAny ?
评论 #43524144 未加载
aerzen大约 1 个月前
I like rust <i>because</i> Any is inconvenient. It pushes you to implement things with static typing.
评论 #43525154 未加载
the__alchemist大约 1 个月前
Rust continues to blow my mind. It&#x27;s been my most-used language for ~5 years, and I have no idea what this article is describing!
jtrueb大约 2 个月前
Getting closer and closer to OOP
评论 #43523746 未加载
评论 #43524663 未加载
评论 #43525096 未加载
评论 #43524605 未加载
评论 #43523650 未加载
评论 #43525222 未加载
评论 #43525214 未加载
评论 #43524049 未加载