3 ms·
Well, the std lib disagrees with you. In Rust, type definitions tend to be maximally generic and restricted with traits only in their implementation. Have a loo
by leshow 6y ago
Well, the std lib disagrees with you. In Rust, type definitions tend to be maximally generic and restricted with traits only in their implementation. Have a look at BTreeMap for example:
pub struct BTreeMap<K, V> {
root: Option<node::Root<K, V>>,
length: usize,
}
The two type parameters are unbounded, this gives flexibility in the implementation because it allows you to define separate `impl` blocks where the bounds are different and more specific to their use case.
Have a look, https://doc.rust-lang.org/src/alloc/collections/btree/map.rs.html#140 https://doc.rust-lang.org/src/alloc/collections/btree/map.rs...
Sometimes the bounds are just Clone, other times they are more complicated Ord bounds for functions like `get`.
This is the way Rust libraries are written, it's very flexible and it's the right choice IMO.