История изменений
Исправление KivApple, (текущая версия) :
struct A;
struct B<'a> {
a: &'a A
}
impl A {
fn make_b(&self) -> B<'_> {
B { a: self }
}
}
struct C<'a> {
a: A,
b: B<'a>
}
impl<'a> C<'a> {
fn new() -> Self {
let a = A {};
let b = a.make_b();
Self {
a,
b
}
}
}
pub fn main() {
let _c = C::new();
}
Ошибки компиляции:
Compiling playground v0.0.1 (/playground)
error[E0515]: cannot return value referencing local variable `a`
--> src/main.rs:22:9
|
21 | let b = a.make_b();
| ---------- `a` is borrowed here
22 | / Self {
23 | | a,
24 | | b
25 | | }
| |_________^ returns a value referencing data owned by the current function
error[E0505]: cannot move out of `a` because it is borrowed
--> src/main.rs:23:13
|
18 | impl<'a> C<'a> {
| -- lifetime `'a` defined here
19 | fn new() -> Self {
20 | let a = A {};
| - binding `a` declared here
21 | let b = a.make_b();
| ---------- borrow of `a` occurs here
22 | / Self {
23 | | a,
| | ^ move out of `a` occurs here
24 | | b
25 | | }
| |_________- returning this value requires that `a` is borrowed for `'a`
Some errors have detailed explanations: E0505, E0515.
For more information about an error, try `rustc --explain E0505`.
error: could not compile `playground` (bin "playground") due to 2 previous errors
Тип то так описать можно, а вот конструктор для него написать нет.
Исходная версия KivApple, :
struct A;
struct B<'a> {
a: &'a A
}
impl A {
fn make_b(&self) -> B<'_> {
B { a: self }
}
}
struct C<'a> {
a: A,
b: B<'a>
}
impl<'a> C<'a> {
fn new() -> Self {
let a = A {};
let b = a.make_b();
Self {
a,
b
}
}
}
pub fn main() {
let _c = C::new();
}
Ошибки компиляции:
Compiling playground v0.0.1 (/playground)
error[E0515]: cannot return value referencing local variable `a`
--> src/main.rs:22:9
|
21 | let b = a.make_b();
| ---------- `a` is borrowed here
22 | / Self {
23 | | a,
24 | | b
25 | | }
| |_________^ returns a value referencing data owned by the current function
error[E0505]: cannot move out of `a` because it is borrowed
--> src/main.rs:23:13
|
18 | impl<'a> C<'a> {
| -- lifetime `'a` defined here
19 | fn new() -> Self {
20 | let a = A {};
| - binding `a` declared here
21 | let b = a.make_b();
| ---------- borrow of `a` occurs here
22 | / Self {
23 | | a,
| | ^ move out of `a` occurs here
24 | | b
25 | | }
| |_________- returning this value requires that `a` is borrowed for `'a`
Some errors have detailed explanations: E0505, E0515.
For more information about an error, try `rustc --explain E0505`.
error: could not compile `playground` (bin "playground") due to 2 previous errors