Created
September 11, 2018 19:03
-
-
Save rust-play/5f87c55848af9a56f6ec73cc245ee6c7 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![feature(nll)] | |
#[derive(Debug)] | |
struct Node { | |
kind: Kind<Box<Node>>, | |
loc: i32, | |
} | |
#[derive(Debug)] | |
enum Kind<T> { | |
Mod(Vec<T>), | |
Term(i32), | |
} | |
#[derive(Debug)] | |
enum Node2 { | |
Unsolved(Box<Node>), | |
Solved { | |
kind: Kind<Node2>, | |
loc: i32, | |
env: String, | |
}, | |
} | |
fn main() { | |
let n = Box::new(Node { | |
kind: Kind::Mod(vec![ | |
Box::new(Node { | |
kind: Kind::Term(0), | |
loc: 20, | |
}), | |
Box::new(Node { | |
kind: Kind::Term(1), | |
loc: 30, | |
}), | |
]), | |
loc: 10, | |
}); | |
println!("{:?}", n); | |
let m = foo(n); | |
println!("{:?}", m); | |
} | |
fn foo(n: Box<Node>) -> Node2 { | |
match *n { | |
Node { | |
kind: Kind::Mod(nodes), | |
loc, | |
} => { | |
let nodes = nodes.into_iter().map(foo).collect(); | |
Node2::Solved { | |
kind: Kind::Mod(nodes), | |
loc, | |
env: "mod".into(), | |
} | |
} | |
Node { | |
kind: Kind::Term(0), | |
loc, | |
} => Node2::Solved { | |
kind: Kind::Term(0), | |
loc, | |
env: "term(0)".into(), | |
}, | |
Node { | |
kind: Kind::Term(ref N), | |
loc, | |
} => Node2::Unsolved(n), | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment