Last active
August 29, 2015 14:23
-
-
Save arkadijs/933c70751629f352e4b0 to your computer and use it in GitHub Desktop.
Beauty of Rust
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
#![allow(dead_code, unused_variables)] | |
#[derive(Debug)] | |
struct Node<'l, 'r> { | |
val: u32, | |
left: Option<&'l Node<'l, 'l>>, | |
right: Option<&'r Node<'r, 'r>> | |
} | |
impl<'l, 'r> Node<'l, 'r> { | |
fn leaf(val: u32) -> Node<'l, 'r> { | |
Node { val: val, left: None, right: None } | |
} | |
fn new(val: u32, left: &'l Node<'l, 'l>, right: &'r Node<'r, 'r>) -> Node<'l, 'r> { | |
Node { val: val, left: Some(left), right: Some(right) } | |
} | |
} | |
enum Dir { | |
Left, Right | |
} | |
struct Choice { | |
choice: Dir | |
} | |
fn choose<'n>(node: &'n Node<'n, 'n>, what: &Choice) -> Option<&'n Node<'n, 'n>> { | |
match what.choice { | |
Dir::Left => node.left, | |
Dir::Right => node.right | |
} | |
} | |
fn main() { | |
let left = Node::leaf(1); | |
let right = Node::leaf(2); | |
let root = Node::new(3, &left, &right); | |
let result = choose(&root, &Choice { choice: Dir::Right }); | |
println!("\nresult: {:?}", result); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment