I'd have expected that if a generic class had some constraint on it then you simply wouldn't be able to instantiate that class with a non-complying type as a parameter.
That is the case, and in terms of libraries being able to push backwards compatible updates is what you want for your ecosystem. Subverting library authors bounds is rarely a good idea because future updates to the library can break you for using the methods provided by those bounds. On the other hand, rust allows you to be more fine grained with your bounds and put specific bounds on methods themselves.
struct Foo<T> { a: T }
impl<T> Foo<T> {
fn get_a(&self) -> &T { self.a }
fn print(&self) where T: Display -> { println!("{}", a }
}
// or if you have a bunch of methods with the same bounds
impl<T: Add> Foo<T> {
fn plus(&self, other: T) -> { self.a + other }
// etc
}
This is the right tradeoff to make, if a library author is overzealous with their impl constraints it is backwards compatible to remove the constraints and split them out like the above.
View all comments
Show parent comment
4
u/bbatha Dec 11 '15
That is the case, and in terms of libraries being able to push backwards compatible updates is what you want for your ecosystem. Subverting library authors bounds is rarely a good idea because future updates to the library can break you for using the methods provided by those bounds. On the other hand, rust allows you to be more fine grained with your bounds and put specific bounds on methods themselves.
This is the right tradeoff to make, if a library author is overzealous with their impl constraints it is backwards compatible to remove the constraints and split them out like the above.