Constrained genericity: Difference between revisions

Added Rust language
(Added Rust language)
Line 1,253:
p Foodbox.new(Apple.new, "string can't eat")
# => test1.rb:3:in `initialize': food must be eadible (ArgumentError)
</lang>
 
=={{header|Rust}}==
<lang rust>
// This declares the "Eatable" constraint. It could contain no function.
trait Eatable {
fn eat();
}
 
// This declares the generic "FoodBox" type,
// whose parameter must "Eatable" constraint.
// The objects of this type contain a vector of eatable objects.
struct FoodBox<T: Eatable> {
_data: Vec<T>,
}
 
// This implements the functions associated to the "FoodBox" type.
// It is not required, but here it is used
// to declare a handy "new" constructor.
impl<T: Eatable> FoodBox<T> {
fn new() -> FoodBox<T> {
FoodBox::<T> { _data: Vec::<T>::new() }
}
}
 
// This declares a simple type.
struct Banana {}
 
// This makes the "Banana" type satisfy the "Eatable" constraint.
// For that, every declaration inside the declaration of "Eatable"
// must be implemented here.
impl Eatable for Banana {
fn eat() {}
}
 
// This makes the primitive "char" type satisfy the "Eatable" constraint.
impl Eatable for char {
fn eat() {}
}
 
fn main() {
// This instantiate a "FoodBox" parameterized by the "Banana" type.
// It is allowed as "Banana" implements "Eatable".
let _fb1 = FoodBox::<Banana>::new();
 
// This instantiate a "FoodBox" parameterized by the "char" type.
// It is allowed as "char" implements "Eatable".
let _fb2 = FoodBox::<char>::new();
 
// This instantiate a "FoodBox" parameterized by the "bool" type.
// It is NOT allowed as "bool" does not implement "Eatable".
//let _fb3 = FoodBox::<bool>::new();
}
</lang>