How to implement a builde like trait for a lifetime bounded type

trait Builder<T> {
    fn new(x: &u32) -> T;

struct A<'a>(&'a u32);

struct B;

impl<'a> Builder<A<'a>> for B {
    fn new(x: &u32) -> A<'a> {

fn main() {


This example does not compile. Is it possible to implement Builder<A> trait for B without changing Builder definition?


No, it is not possible to do without changing builder. The fn new(&u32) method is specified as having an interface that only lends the argument (reference to u32) for the duration of the function call, with no allowance for the returned T to borrow the input argument (which is what your impl for B needs to do).

The signature that you need is fn new(x: &'a u32) -> A<'a> which makes sense right – a &'foo u32 produces an A<'foo>.