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