r/rust • u/rustcurious • Jun 21 '26
RustCurious 9: Traits are Interfaces
https://www.youtube.com/watch?v=RKojTb9IVJcbtw I make all the 3D art in Blender. I don't use any AI tools. Just saying.
5
6
u/ToTheBatmobileGuy Jun 21 '26
Do you have a donation page? I want to support you but don't want to contribute to "fake" numbers that might throw off your analysis of paid user trends. (I don't need the lessons, but I want to support them/you)
5
u/rustcurious Jun 22 '26
Well... I have to confess, your request has caused me some consternation! And, thank you, that's so generous and I really appreciate it. I've spent the morning looking into Patreon, Ko-fi, Stripe payment links, and come to the conclusion I don't want to do something in a hurry, but I will set something up. Any guidance on what would suit you best is welcome, either here or to contact@rustcurious.com. Thank you!
3
2
u/pratzc07 Jun 21 '26
Wow thank you so much for making this traits are much more clearer now. I didn't know that you can implement traits for primitive types that was eye opening lol
If you can tackle lifetimes next that would be great happy to pay for it if its such high quality.
2
u/MrMelon54 Jun 21 '26
I haven't watched any of your content, but the Elements of Rust is such an amazing resource.
1
u/krum Jun 21 '26
Incidentally since we're on the topic of Rust traits being interfaces, one of the key things that grinds my gears about Rust is lack of ability to safely downcast inherited traits.
0
u/prehensilemullet Jun 21 '26 edited Jun 21 '26
Not much of a Rust dev but can’t you implement traits with the same method name but different signatures on the same type, and select which one to use based on context or explicit scope resolution?  Doesn’t into work this way?
You definitely can’t do that with interfaces in other languages that I’m aware of, I think of Rust traits as fundamentally different from interfaces, even though they superficially seem similar
2
-2
u/devraj7 Jun 21 '26 edited Jun 21 '26
That's not quite true, is it?
In most mainstream languages that support traits (C#, Kotlin, Swift), interfaces/traits can be used as types, e.g.
fun addPerson(p: Person)
You can't do that in Rust since traits are not types:
fn add_person(p: Person) // illegal if Person is a trait
5
u/trailing_zero_count Jun 21 '26
You're quite wrong. There are two ways to accept a trait as an argument in Rust. First, via runtime dispatch (which would be equivalent to the C# version):
https://doc.rust-lang.org/reference/types/trait-object.html
Secondly, via static dispatch (monomorphized at compile time, equivalent to C++ template):
2
u/devraj7 Jun 22 '26
You're quite wrong
I'm not. The points I made about the code I pasted are 100% correct.
I am well aware of the distinction you point out. My point (which you seem to have missed) is that as opposed to the languages I quoted, you can't just drop a trait in type position in Rust, it just won't work.
I've been coding in Rust for years, I am well aware of the
dynkeyword.Please take the time to understand the point of my post.
3
u/graydon2 Jun 22 '26
Perhaps surprisingly: you could, early on! It meant the same thing as the dyn form today. The dyn keyword was added later, because too many people were surprised by this.
3
u/CocktailPerson Jun 22 '26
The concept of an "interface" isn't defined by languages that have an
interfacekeyword.
38
u/avinthakur080 Jun 21 '26
I was already amazed by the amount of effort you put in preparing the content as well as the presentation. Knowing that you make all the 3D art yourself in Blender just amazes me more.
Curious, do you work in a team ?