Rust errors / Ownership and borrowing
Rust compiler error · checked with rustc 1.99.0
Rust error E0195: lifetime parameters do not match the trait definition
What it meansThe lifetime parameters of the method do not match the trait declaration.
What rustc prints
error[E0195]: lifetime parameters do not match the trait definition --> main.rs:8:12 | 8 | fn bar<'a,'b>(x: &'a str, y: &'b str) { | ^^ ^^ | = note: lifetime parameters differ in whether they are early- or late-bound note: `'a` differs between the trait and impl --> main.rs:2:12 | 1 | trait Trait { | ----------- in this trait... 2 | fn bar<'a,'b:'a>(x: &'a str, y: &'b str); | ^^ -- this lifetime bound makes `'a` early-bound | | | `'a` is early-bound ... 7 | impl Trait for Foo { | ------------------ in this impl... 8 | fn bar<'a,'b>(x: &'a str, y: &'b str) { | ^^ `'a` is late-bound note: `'b` differs between the trait and impl --> main.rs:2:15 | 1 | trait Trait { | ----------- in this trait... 2 | fn bar<'a,'b:'a>(x: &'a str, y: &'b str); | ^^ -- this lifetime bound makes `'b` early-bound | | | `'b` is early-bound ... 7 | impl Trait for Foo { | ------------------ in this impl... 8 | fn bar<'a,'b>(x: &'a str, y: &'b str) { | ^^ `'b` is late-bound
Real output of rustc 1.99.0 compiling the official example below (2021 edition). Your names and line numbers will differ; the first line is the part to match.
The compiler team's explanation
The lifetime parameters of the method do not match the trait declaration.
Erroneous code example:
trait Trait {
fn bar<'a,'b:'a>(x: &'a str, y: &'b str);
}
struct Foo;
impl Trait for Foo {
fn bar<'a,'b>(x: &'a str, y: &'b str) {
// error: lifetime parameters or bounds on method `bar`
// do not match the trait declaration
}
}
The lifetime constraint 'b for bar() implementation does not match the trait declaration. Ensure lifetime declarations match exactly in both trait declaration and implementation. Example:
trait Trait {
fn t<'a,'b:'a>(x: &'a str, y: &'b str);
}
struct Foo;
impl Trait for Foo {
fn t<'a,'b:'a>(x: &'a str, y: &'b str) { // ok!
}
}From rustc --explain E0195 (1.99.0), part of the Rust project, MIT or Apache-2.0. Official page.
Related errors
- E0382borrow of moved value
- E0499cannot borrow as mutable more than once at a time
- E0502cannot borrow as mutable because it is also borrowed as immutable
- E0106missing lifetime specifier
- E0161cannot move a value of type `dyn Bar`
- E0207the type parameter `T` is not constrained by the impl trait, self type, or predicates
- E0226only a single explicit lifetime bound is permitted
Up next
Often hit nextE0382: borrow of moved valueA related error from the same family.Read it next →Did this answer your question?