0

I am trying to implement method which generates some data (i32 in this case) and returns reference to this data. How can I make this code compilable:

  fn parent_func<'a>(x: i32) -> &'a i32 {
    let a = 5 + x;
    let res: &'a i32 = &a; // TODO - borrowed value does not live long enough
    res
  }

  fn child_func<'b>() -> &'b i32 {
    let x = 2;
    let res = parent_func(x);
    res
  }

  fn main() {
    let x = child_func();
    assert_eq!(*x, 7);
  }

I tried to put return type to Box<> but it did not helped. Thanks a lot.

0 Answers0