1 You tried to use a type which doesn't implement some trait in a place which
4 Erroneous code example:
7 // here we declare the Foo trait with a bar method
12 // we now declare a function which takes an object implementing the Foo trait
13 fn some_func<T: Foo>(foo: T) {
18 // we now call the method with the i32 type, which doesn't implement
20 some_func(5i32); // error: the trait bound `i32 : Foo` is not satisfied
24 In order to fix this error, verify that the type you're using does implement
32 fn some_func<T: Foo>(foo: T) {
33 foo.bar(); // we can now use this method since i32 implements the
37 // we implement the trait on the i32 type
43 some_func(5i32); // ok!
47 Or in a generic context, an erroneous code example would look like:
50 fn some_func<T>(foo: T) {
51 println!("{:?}", foo); // error: the trait `core::fmt::Debug` is not
52 // implemented for the type `T`
56 // We now call the method with the i32 type,
57 // which *does* implement the Debug trait.
62 Note that the error here is in the definition of the generic function: Although
63 we only call it with a parameter that does implement `Debug`, the compiler
64 still rejects the function: It must work with all possible input types. In
65 order to make this example compile, we need to restrict the generic type we're
71 // Restrict the input type to types that implement Debug.
72 fn some_func<T: fmt::Debug>(foo: T) {
73 println!("{:?}", foo);
77 // Calling the method is still fine, as i32 implements Debug.
80 // This would fail to compile now:
81 // struct WithoutDebug;
82 // some_func(WithoutDebug);
86 Rust only looks at the signature of the called function, as such it must
87 already specify all requirements that will be used for every type parameter.