2012-12-10 19:32:48 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2013-12-10 01:16:18 -06:00
|
|
|
enum ast<'a> {
|
2015-01-08 05:02:42 -06:00
|
|
|
num(usize),
|
2013-12-10 01:16:18 -06:00
|
|
|
add(&'a ast<'a>, &'a ast<'a>)
|
2012-04-18 23:26:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn build() {
|
2015-03-03 02:42:26 -06:00
|
|
|
let x = ast::num(3);
|
|
|
|
let y = ast::num(4);
|
2014-11-06 02:05:53 -06:00
|
|
|
let z = ast::add(&x, &y);
|
2012-04-18 23:26:25 -05:00
|
|
|
compute(&z);
|
|
|
|
}
|
|
|
|
|
2015-01-08 05:02:42 -06:00
|
|
|
fn compute(x: &ast) -> usize {
|
2012-08-06 14:34:08 -05:00
|
|
|
match *x {
|
2014-11-06 02:05:53 -06:00
|
|
|
ast::num(x) => { x }
|
|
|
|
ast::add(x, y) => { compute(x) + compute(y) }
|
2012-04-18 23:26:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-08 05:02:42 -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 {
|
2014-11-06 02:05:53 -06:00
|
|
|
ast::num(x) => {
|
2015-01-03 09:45:00 -06:00
|
|
|
return &ast::num((*f)(x)); //~ ERROR borrowed value does not live long enough
|
2012-04-18 23:26:25 -05:00
|
|
|
}
|
2014-11-06 02:05:53 -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);
|
2014-11-06 02:05:53 -06:00
|
|
|
return &ast::add(m_x, m_y); //~ ERROR borrowed value does not live long enough
|
2012-04-18 23:26:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-18 17:52:21 -05:00
|
|
|
fn main() {}
|