Here is an alternative explanation, based on an example, which might be helpful in understanding the concept, which was already explained very accurately by Shepmaster and Matthieu.
Let's say we want to make a trait with a generic implementation like this:
pub trait MyTrait<T> {
fn say_hi(&self) -> String;
}
impl<T> MyTrait<T> for &T {
fn say_hi(&self) -> String {
return "Hi!".to_string();
}
}
That will allow us to call say_hi()
on references to various types like this:
let a = &74; // a reference to a SIZED integer
println!("a: {}", a.say_hi());
let b = &[1, 2, 3]; // a reference to a SIZED array
println!("b: {}", b.say_hi());
However, if we declare a function like this:
fn be_fancy(arr: &[u16]) {
println!("arr: {}", arr.say_hi());
}
, we will get a compiler error:
error[E0599]: the method `say_hi` exists for reference `&[u16]`, but its trait bounds were not satisfied
|
| println!("arr: {}", arr.say_hi());
| ^^^^^^ method cannot be called on `&[u16]` due to unsatisfied trait bounds
|
note: the following trait bounds were not satisfied because of the requirements of the implementation of `MyTrait<_>` for `_`:
`[u16]: Sized`
As can be seen, the problem is our trait is only implemented for references to Sized
types. Sized
is a special marker trait that is "on" by default. In most cases that's convenient, but sometimes we might want to turn that "restriction" off. ?Sized
basically says "the type may or may not be Sized" (which is not the same as "unsized").
Our function be_fancy
expects a reference to an array of unknown (at compile time) size. To fix the problem, we can simply replace T
(which is equivalent to T: Sized
) with T: ?Sized
like this:
pub trait MyTrait<T: ?Sized> {
fn say_hi(&self) -> String;
}
impl<T: ?Sized> MyTrait<T> for &T {
fn say_hi(&self) -> String {
return "Hi yo!".to_string();
}
}