61
Announcing Rust 1.86.0 (blog.rust-lang.org)
submitted 4 months ago by neme@lemm.ee to c/rust@programming.dev
top 6 comments
sorted by: hot top controversial new old
[-] livingcoder@programming.dev 14 points 4 months ago* (last edited 4 months ago)

Wow, that trait feature is great. I've been eagerly waiting for that one for a long time. Thank you to everyone who made that possible.

[-] INeedMana@lemmy.world 1 points 4 months ago

If a trait has a supertrait you can coerce a reference to said trait object to a reference to a trait object of the supertrait

As someone that just started learning Rust: wha?

[-] livingcoder@programming.dev 3 points 4 months ago* (last edited 4 months ago)

Basically, you can generalize your trait types into their parent (super) traits for situations when functionality is specific to those supertrait objects.

As an example, if you have a trait CanBark and it is a super trait for the trait IsDog, you can coerce your references of &dyn IsDog into a &dyn CanBark. You can then work with other trait types that share a super trait.

trait CanBark {
    fn bark(&self);
}
trait IsSeal: CanBark { }
trait IsDog: CanBark { }

fn bark_as_group(barkers: &Vec<&dyn CanBark>) {
    for barker in barkers {
        barker.bark();
    }
}

let spot: &dyn IsDog = get_spot();
let seal: &dyn IsSeal = get_seal();
let barkers: Vec<&dyn CanBark> = Vec::new();
barkers.push(spot);  // coerced
barkers.push(seal);  // coerced
bark_as_group(&barkers);

At least, I hope this is possible now. If it's purely "you can return a coerced type from a function", that is less useful.

[-] INeedMana@lemmy.world 2 points 4 months ago

Thank you

So basically, it's like inheritance but for traits?

[-] livingcoder@programming.dev 3 points 4 months ago

Exactly. The functions of the super trait are also required when implementing the child trait's functions, as you would expect from inheritance.

[-] sugar_in_your_tea@sh.itjust.works 2 points 3 months ago

HashMaps and slices now support indexing multiple elements mutably

This is kinda neat. I was hoping for something more generic, so hopefully this is a step toward that.

Allow safe functions to be marked with the #[target_feature] attribute.

This seems huge for library authors! That's not me as of yet, but maybe soon. ๐Ÿ˜€

Congrats on the release!

this post was submitted on 03 Apr 2025
61 points (100.0% liked)

Rust

7219 readers
94 users here now

Welcome to the Rust community! This is a place to discuss about the Rust programming language.

Wormhole

!performance@programming.dev

Credits

  • The icon is a modified version of the official rust logo (changing the colors to a gradient and black background)

founded 2 years ago
MODERATORS