3 ms·
> Not sure of anything that uses traits without generics. Traits do a lot of things in Rust that have nothing to do with generics. :) Implementing a trait is h
by kibwen 9y ago
> Not sure of anything that uses traits without generics.
Traits do a lot of things in Rust that have nothing to do with generics. :) Implementing a trait is how one gives a type a destructor. Implementing various traits (the ones in std::ops) allows one to do operator overloading for user-defined types. Defining a trait allows one to give methods to types that are defined in other compilation units, including the standard library, e.g.:
trait Foo {
fn foo(self);
}
impl Foo for i32 {
fn foo(self) {
println!("Hello, traits!");
}
}
fn main() {
42.foo(); // Hello, traits!
}
- rkangel 9y agoThank you for that.