I'm trying to parse a series to tokentrees, but when I try to implement my parsing trait I get an error related to reference lifetimes. I thought creating a boxed version would move around any issues with reference counts or lifetimes. The code is as follows.
impl Parse for TokenTree {
fn parse(&mut self) -> Tree {
match self.clone() {
TtDelimited(_, y) => {
let mut y2 = box (*y).clone();
match y2.delim {
token::DelimToken::Paren => y2.parse(),
_ => panic!("not done yet"),
}
}
TtToken(_, t) => E(t),
_ => panic!("not done yet"),
}
}
}
the errors I get make the issue clear, but I can't find any information on solving this particular issue.
35:51 error: `*y2` does not live long enough
token::DelimToken::Paren => y2.parse(),
^~
42:6 note: reference must be valid for the anonymous lifetime #1 defined on the block at 30:31...
fn parse(&mut self) -> Tree{
match self.clone(){
TtDelimited(_, y) => {
let mut y2 = box () (*y).clone();
match y2.delim{
token::DelimToken::Paren => y2.parse(),
...
38:14 note: ...but borrowed value is only valid for the block at 32:33
TtDelimited(_, y) => {
let mut y2 = box () (*y).clone();
match y2.delim{
token::DelimToken::Paren => y2.parse(),
_ => panic!("not done yet"),
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…