3 ms·
In the most basic sense you can view it like that. When combined with the typical behavior of traits it gets more interesting. Say you have some data: stru
by kibwen 9y ago
In the most basic sense you can view it like that. When combined with the typical behavior of traits it gets more interesting. Say you have some data:
struct Foo;
Then we can make a trait, that defines an interface that we can give to data later:
trait Bar {
const BAR_CONSTANT: i32;
fn some_function();
fn some_method(self);
}
Then we can actually implement that trait for our data:
impl Bar for Foo {
const BAR_CONSTANT: i32 = 42;
fn some_function() {
println!("foo's associated function, and the const is {}", Self::BAR_CONSTANT);
}
fn some_method(self) {
println!("foo's method, and the const is still {}", Self::BAR_CONSTANT);
}
}
Then we can use it like so:
Foo::some_function(); // foo's associated function, and the const is 42
let foo = Foo;
foo.some_method(); // foo's method, and the const is still 42
And now we can take it further. Imagine that you have another piece of data, `struct Qux`. Then you can do the same and `impl Bar for Qux`. And now you can write a generic function like so:
fn bar_taker<T: Bar>(something_that_impls_bar: T) {
T::some_function();
something_that_impls_bar.some_method();
// And of course we can refer to T::BAR_CONSTANT in here as well.
}
And call it like so:
bar_taker(foo);
bar_taker(qux);
AFAIK, the big deal with associated consts is specifically that it allows generic code like that to refer to different values on a per-type basis.
Here's all this code on the Rust interactive playground if you'd like to poke at it: https://play.rust-lang.org/?gist=60bd64e1b2f52bb91ed0b0cb428d7303&version=beta https://play.rust-lang.org/?gist=60bd64e1b2f52bb91ed0b0cb428...
- phkahler 9y agoThanks for the nice example. I've heard of traits but hadn't read enough to know what they are. Looks like a form of multiple inheritance. The example you gave shows polymorphism via traits. That was helpful to me.