I want move one struct into another and get references on parts of first struct as parts of other without cloning or copying, if it is possible. How to do it in right way?
fn main() {
let foo = Foo::new();
let bar = Bar::new(foo);
println!("{:?}", bar);
}
#[derive(Debug)]
struct Foo {
v: String,
}
impl Foo {
pub fn new() -> Self {
Foo {
v: String::from("a|b"),
}
}
pub fn get_a(&self) -> &str {
&self.v[0..1]
}
pub fn get_b(&self) -> &str {
&self.v[2..3]
}
}
#[derive(Debug)]
struct Bar<'a> {
foo: Foo,
a: &'a str,
b: &'a str,
}
impl<'a> Bar<'a> {
pub fn new(f: Foo) -> Self {
Bar::parse(f)
}
fn parse(f: Foo) -> Self {
let a = f.get_a();
let b = f.get_b();
Bar { foo: f, a, b }
}
}
I got an error:
error[E0515]: cannot return value referencing function parameter `f`
--> src/main.rs:44:9
|
41 | let a = f.get_a();
| - `f` is borrowed here
...
44 | Bar { foo: f, a, b }
| ^^^^^^^^^^^^^^^^^^^^ returns a value referencing data owned by the current function
error[E0515]: cannot return value referencing function parameter `f`
--> src/main.rs:44:9
|
42 | let b = f.get_b();
| - `f` is borrowed here
43 |
44 | Bar { foo: f, a, b }
| ^^^^^^^^^^^^^^^^^^^^ returns a value referencing data owned by the current function
error[E0505]: cannot move out of `f` because it is borrowed
--> src/main.rs:44:20
|
35 | impl<'a> Bar<'a> {
| -- lifetime `'a` defined here
...
41 | let a = f.get_a();
| - borrow of `f` occurs here
...
44 | Bar { foo: f, a, b }
| -----------^--------
| | |
| | move out of `f` occurs here
| returning this value requires that `f` is borrowed for `'a`