1 You tried to use a type which doesn't implement some trait in a place which
2 expected that trait. Erroneous code example:
5 // here we declare the Foo trait with a bar method
10 // we now declare a function which takes an object implementing the Foo trait
11 fn some_func<T: Foo>(foo: T) {
16 // we now call the method with the i32 type, which doesn't implement
18 some_func(5i32); // error: the trait bound `i32 : Foo` is not satisfied
22 In order to fix this error, verify that the type you're using does implement
30 fn some_func<T: Foo>(foo: T) {
31 foo.bar(); // we can now use this method since i32 implements the
35 // we implement the trait on the i32 type
41 some_func(5i32); // ok!
45 Or in a generic context, an erroneous code example would look like:
48 fn some_func<T>(foo: T) {
49 println!("{:?}", foo); // error: the trait `core::fmt::Debug` is not
50 // implemented for the type `T`
54 // We now call the method with the i32 type,
55 // which *does* implement the Debug trait.
60 Note that the error here is in the definition of the generic function: Although
61 we only call it with a parameter that does implement `Debug`, the compiler
62 still rejects the function: It must work with all possible input types. In
63 order to make this example compile, we need to restrict the generic type we're
69 // Restrict the input type to types that implement Debug.
70 fn some_func<T: fmt::Debug>(foo: T) {
71 println!("{:?}", foo);
75 // Calling the method is still fine, as i32 implements Debug.
78 // This would fail to compile now:
79 // struct WithoutDebug;
80 // some_func(WithoutDebug);
84 Rust only looks at the signature of the called function, as such it must
85 already specify all requirements that will be used for every type parameter.