I want to write the equivalent of this C++ in Rust:
struct Bar { int x; };
struct Foo { Bar* bar; };
void main() {
Bar bar { 42 };
Foo foo { &bar };
Bar bar2 { 50 };
foo.bar = &bar2;
printf("%d\n", foo.bar->x);
}
The C++ sets up a structure, then swaps out a Bar
object with another Bar
object. This is the code I've tried:
struct Bar(isize);
impl Bar {
fn new(i: isize) -> Bar { Bar(i) }
}
struct Foo<'a> {
bar: Option<&'a Bar>,
}
impl<'a> Foo<'a> {
fn new(bar: &Bar) -> Foo {
Foo { bar: Some(&bar) }
}
}
fn main() {
// Set up first state
let bar = Bar::new(42);
let mut foo = Foo::new(&bar);
// Replace bar object
let bar2 = Bar::new(50);
foo.bar = Some(&bar2);
if let Some(e) = foo.bar {
println!("{}", e.0);
}
}
This code complains:
error[E0597]: `bar2` does not live long enough
--> src/main.rs:22:21
|
22 | foo.bar = Some(&bar2);
| ^^^^ borrowed value does not live long enough
...
27 | }
| - `bar2` dropped here while still borrowed
|
= note: values in a scope are dropped in the opposite order they are created
Is it possible in Rust to have a structure that has a reference to a sub-structure whose lifetime can be shorter (i.e. you can replace the sub-structure instance with another instance)?
I looked at some other answers, and tried to split up the structure:
struct Bar(isize);
impl Bar {
fn new(i: isize) -> Bar {
Bar(i)
}
}
struct FooCore {
a: isize,
}
struct Foo<'c, 'a> {
core: &'c FooCore,
bar: &'a Bar,
}
impl<'c, 'a> Foo<'c, 'a> {
fn new(core: &'c FooCore, bar: &'a Bar) -> Foo<'c, 'a> {
Foo {
core: &core,
bar: &bar,
}
}
}
fn main() {
// Set up first state
let core = FooCore { a: 10 };
let bar = Bar::new(42);
let _foo = Foo::new(&core, &bar);
// Replace bar object
let bar2 = Bar::new(50);
let foo = Foo::new(&core, &bar2);
println!("{} {}", foo.core.a, foo.bar.0);
}
This compiles and works, however, as soon as I make the core and bar fields mutable, it falls apart. Below is code that uses mut
:
#![allow(unused_mut)]
struct Bar(isize);
impl Bar {
fn new(i: isize) -> Bar {
Bar(i)
}
}
struct FooCore {
a: isize,
}
struct Foo<'c, 'a> {
core: &'c mut FooCore,
bar: &'a mut Bar,
}
impl<'c, 'a> Foo<'c, 'a> {
fn new(core: &'c mut FooCore, bar: &'a mut Bar) -> Foo<'c, 'a> {
Foo {
core: &mut core,
bar: &mut bar,
}
}
}
fn main() {
// Set up first state
let mut core = FooCore { a: 10 };
let mut bar = Bar::new(42);
let mut _foo = Foo::new(&mut core, &mut bar);
// Replace bar object
let mut bar2 = Bar::new(50);
let mut foo = Foo::new(&mut core, &mut bar2);
println!("{} {}", foo.core.a, foo.bar.0);
}
This results in the errors:
error[E0597]: `core` does not live long enough
--> src/main.rs:21:24
|
21 | core: &mut core,
| ^^^^ borrowed value does not live long enough
...
24 | }
| - borrowed value only lives until here
|
note: borrowed value must be valid for the lifetime 'c as defined on the impl at 18:1...
--> src/main.rs:18:1
|
18 | impl<'c, 'a> Foo<'c, 'a> {
| ^^^^^^^^^^^^^^^^^^^^^^^^
error[E0597]: `bar` does not live long enough
--> src/main.rs:22:23
|
22 | bar: &mut bar,
| ^^^ borrowed value does not live long enough
23 | }
24 | }
| - borrowed value only lives until here
|
note: borrowed value must be valid for the lifetime 'a as defined on the impl at 18:1...
--> src/main.rs:18:1
|
18 | impl<'c, 'a> Foo<'c, 'a> {
| ^^^^^^^^^^^^^^^^^^^^^^^^
error[E0499]: cannot borrow `core` as mutable more than once at a time
--> src/main.rs:35:33
|
31 | let mut _foo = Foo::new(&mut core, &mut bar);
| ---- first mutable borrow occurs here
...
35 | let mut foo = Foo::new(&mut core, &mut bar2);
| ^^^^ second mutable borrow occurs here
...
38 | }
| - first borrow ends here
How do I structure my data to achieve what I want?