Hello,
I'm trying to figure out how I can set a reference to another struct member's data during instantiation. This code compiles and has the correct outcome of referencing the first element of the member array:
struct Foo<'a> {
tokens:Vec<i32>,
current_tok:Option<&'a i32>,
}
impl<'a> Foo<'a> {
fn new(tokens:Vec<i32>) -> Foo<'a> {
Foo {
tokens,
current_tok: None,
}
}
}
fn main() {
let mut foo = Foo::new(vec![1,2,5]);
foo.current_tok = foo.tokens.first();
match foo.tokens.first() {
Some(x) => println!("{}",x),
_ => {},
}
}
See Playground.
I would like to be able to do this in the new function something like this:
struct Foo<'a> {
tokens:Vec<i32>,
current_tok:Option<&'a i32>,
}
impl<'a> Foo<'a> {
fn new(tokens:Vec<i32>) -> Foo<'a> {
Foo {
tokens,
current_tok: tokens.first(),
}
}
}
fn main() {
let foo = Foo::new(vec![1,2,5]);
// Not assigning here >>>
// foo.current_tok = foo.tokens.first();
match foo.tokens.first() {
Some(x) => println!("{}",x),
_ => {},
}
}
See Playground
I am aware enough of the borrowing rules to understand the problem but not well enough to come up w/ a solution :).
Thanks for any help.