TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Exploring Dynamic Dispatch in Rust (2017)

40 pointsby ibobevabout 2 months ago

6 comments

kd5bjoabout 2 months ago
It&#x27;s worth noting that this post is 8 years old, and some things have moved on since then.<p>In particular, trait object upcasting is on beta now and will be promoted to stable in a couple of weeks. I know that there has also been active development towards enabling multiple-bound trait objects, but I don&#x27;t know what the status of that effort is.<p>The article also makes the (quite understandable) mistake of conflating a fat pointer to a trait object with the trait object itself. In Rust, trait objects are &quot;unsized&quot;, which means they can only exist behind some kind of pointer. And any pointer in Rust that targets an unsized type grows an additional metadata field (for trait objects, a vtable pointer). This is a slightly weird behavior with a few unexpected consequences that often catches people off-guard, but understanding it is key to understanding why you can&#x27;t make a trait object for something like `Clone`, which returns an instance of itself-- Because unsized types can&#x27;t appear on their own, the return type of the `clone()` method would be invalid.<p>For a more thorough take, I recommend &quot;A tour of `dyn Trait`&quot; ( <a href="https:&#x2F;&#x2F;quinedot.github.io&#x2F;rust-learning&#x2F;dyn-trait-overview.html" rel="nofollow">https:&#x2F;&#x2F;quinedot.github.io&#x2F;rust-learning&#x2F;dyn-trait-overview....</a> )
评论 #43493450 未加载
Ericson2314about 2 months ago
Looking forward to when Rust gets<p>1. erased types (have to use behind pointer, so not shallow dynamic sizing) 2. Existential types<p>So we can build vtables by hand.<p>Type safe API to any OOP ABI + tons of stuff OOP people never imagined....let&#x27;s go!
评论 #43491244 未加载
评论 #43492703 未加载
评论 #43491012 未加载
评论 #43491246 未加载
shmerlabout 2 months ago
If I recall correctly, Rust decisively wanted to avoid multiple inheritance concept, in order not to deal with the diamond problem.<p>Composition over inheritance is the suggested principle.
评论 #43495783 未加载
tyiloabout 2 months ago
Trait upcasting will land in Rust 1.86.0 which will be released in one week: <a href="https:&#x2F;&#x2F;github.com&#x2F;rust-lang&#x2F;rust&#x2F;pull&#x2F;134367" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;rust-lang&#x2F;rust&#x2F;pull&#x2F;134367</a>
tyiloabout 2 months ago
(2017)<p>The examples don&#x27;t compile without using Rust edition 2018 or older.
manmtstreamabout 2 months ago
The memory layouts don’t work like this anymore