2018-12-16 21:21:47 -06:00
|
|
|
enum Ast<'a> {
|
|
|
|
Num(usize),
|
|
|
|
Add(&'a Ast<'a>, &'a Ast<'a>)
|
2012-04-18 23:26:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn build() {
|
2018-12-16 21:21:47 -06:00
|
|
|
let x = Ast::Num(3);
|
|
|
|
let y = Ast::Num(4);
|
|
|
|
let z = Ast::Add(&x, &y);
|
2012-04-18 23:26:25 -05:00
|
|
|
compute(&z);
|
|
|
|
}
|
|
|
|
|
2018-12-16 21:21:47 -06:00
|
|
|
fn compute(x: &Ast) -> usize {
|
2012-08-06 14:34:08 -05:00
|
|
|
match *x {
|
2018-12-16 21:21:47 -06:00
|
|
|
Ast::Num(x) => { x }
|
|
|
|
Ast::Add(x, y) => { compute(x) + compute(y) }
|
2012-04-18 23:26:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-16 21:21:47 -06:00
|
|
|
fn map_nums<'a,'b, F>(x: &Ast, f: &mut F) -> &'a Ast<'b> where F: FnMut(usize) -> usize {
|
2012-08-06 14:34:08 -05:00
|
|
|
match *x {
|
2018-12-16 21:21:47 -06:00
|
|
|
Ast::Num(x) => {
|
2019-04-22 02:40:08 -05:00
|
|
|
return &Ast::Num((*f)(x)); //~ ERROR cannot return reference to temporary value
|
2012-04-18 23:26:25 -05:00
|
|
|
}
|
2018-12-16 21:21:47 -06:00
|
|
|
Ast::Add(x, y) => {
|
2015-01-03 09:45:00 -06:00
|
|
|
let m_x = map_nums(x, f);
|
|
|
|
let m_y = map_nums(y, f);
|
2019-04-22 02:40:08 -05:00
|
|
|
return &Ast::Add(m_x, m_y); //~ ERROR cannot return reference to temporary value
|
2012-04-18 23:26:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-18 17:52:21 -05:00
|
|
|
fn main() {}
|