Hi,
i'm trying to use a library, which has a function like do_something
:
struct A;
impl A {
fn do_something<'a, 'b:'a>(&'b self) -> &'a i32{
&1
}
}
struct B<'a> {
a: A,
i: &'a i32,
}
impl B<'_> {
fn new<'a>() -> B<'a> {
let a = A;
let i = a.do_something();
B {a, i}
}
}
fn main() {
let b = B::new();
println!("{:?}", b.i);
}
As you can see I want to store A
and the return value of do_something
in a struct (B
here).
However this does not seem possible:
error[E0515]: cannot return value referencing local variable `a`
--> src/main.rs:18:9
|
17 | let i = a.do_something();
| ---------------- `a` is borrowed here
18 | B {a, i}
| ^^^^^^^^ returns a value referencing data owned by the current function
I am quite confused by this error, as this should be possible as far as I understand. I get that, the borrow of a
in line 17 needs to outlive i
according to the signature of do_something
. However outliving is not enforced strictly (ref), and living equally as long is valid too. And this should be the case since a
and i
are moved into B
.
Why doesn't this work? What am I missing? Can I do something about this?