[rust-dev] tutorial on interfaces: bounded type parameters

Niko Matsakis niko at alum.mit.edu
Thu Jan 12 13:59:03 PST 2012

On 1/12/12 1:17 PM, Marijn Haverbeke wrote:
>> Note the phrase "aren't known at compile time." That reads as if bounded types
>> offer runtime polymorphism. (They don't, do they?)
> They do.

They do and they don't.  I think what Graham means is that if I write a 

    inter X { ... }
    fn foo<T:X>(x: [T]) { ... }

The function foo() is compiled without knowledge of what T is, but it 
can only be applied to one T at a time.  In other words, the list is 
always a list of values of one type (i.e., T).  This is polymorphic but 
in a somewhat more limited sense than a C++ object.

However, we also allow values to be *cast* to the interface type, so you 
can write:

     fn foo(x: [X]) { ... }

and now the function foo() accepts a list whose contents may be of many 
different types, all of which support the interface X.


More information about the Rust-dev mailing list