Skip to content

Instantly share code, notes, and snippets.

@Zolomon
Created August 31, 2013 18:33
Show Gist options
  • Select an option

  • Save Zolomon/6399866 to your computer and use it in GitHub Desktop.

Select an option

Save Zolomon/6399866 to your computer and use it in GitHub Desktop.
It works! \o/ Thank you #rust@irc.freenode.net <3
fn main() {
struct GoalExplore {
name: ~str
}
struct GoalRest {
name: ~str
}
trait Goal {
fn new() -> Self;
fn process(&self, depth: int) { }
fn to_str(&self) -> ~str;
}
impl Goal for GoalExplore {
fn new() -> GoalExplore {
GoalExplore { name: ~"GoalExplore" }
}
fn process(&self, depth: int) {
println(fmt!("Exploring"));
}
fn to_str(&self) -> ~str {
self.name.clone()
}
}
impl Goal for GoalRest {
fn new() -> GoalRest {
GoalRest { name: ~"GoalRest"}
}
fn process(&self, depth: int) {
println(fmt!("Resting"))
}
fn to_str(&self) -> ~str {
self.name.clone()
}
}
enum Tree {
Leaf(@Goal),
Node(~[Tree]),
}
//fn foo<G: Goal>(goal: &G) { .. }
impl Tree {
fn push(&mut self, node: Tree) {
match *self {
Node(ref mut children) => children.push(node),
Leaf(_) => fail!(~"Cannot add node to leaf node")
}
}
fn process(&mut self, depth: int) {
match *self {
Node(ref mut children) => for x in children.mut_iter() { x.process(depth+1) },
Leaf(l) => {
for x in range(0, depth) { print(fmt!(" "))};
println(fmt!("Leaf: %s", l.to_str()));
}
}
}
}
let mut t: @mut GoalExplore = @mut Goal::new();
let mut rest: @mut GoalRest = @mut Goal::new();
let mut tree: Tree = Node(~[]);
tree.push(
Node(~[
Leaf(t as @Goal),
Leaf(rest as @Goal),
Node(~[
Leaf(t as @Goal),
Leaf(t as @Goal),
Leaf(t as @Goal),
Node(~[
Leaf(t as @Goal),
Leaf(t as @Goal),
Leaf(t as @Goal)])])]));
tree.process(0);
println!("{:?}", tree);
}
➜ roguelike git:(master) ✗ rust run composite.rs
composite.rs:72:9: 72:10 warning: variable does not need to be mutable [-W unused-mut (default)]
composite.rs:72 let mut t: @mut GoalExplore = @mut Goal::new();
^
composite.rs:73:9: 73:13 warning: variable does not need to be mutable [-W unused-mut (default)]
composite.rs:73 let mut rest: @mut GoalRest = @mut Goal::new();
^~~~
composite.rs:13:20: 13:25 warning: unused variable: `depth` [-W unused-variable (default)]
composite.rs:13 fn process(&self, depth: int) { }
^~~~~
composite.rs:22:20: 22:25 warning: unused variable: `depth` [-W unused-variable (default)]
composite.rs:22 fn process(&self, depth: int) {
^~~~~
composite.rs:36:20: 36:25 warning: unused variable: `depth` [-W unused-variable (default)]
composite.rs:36 fn process(&self, depth: int) {
^~~~~
composite.rs:64:9: 64:10 warning: unused variable: `x` [-W unused-variable (default)]
composite.rs:64 for x in range(0, depth) { print(fmt!(" "))};
^
Leaf: GoalExplore
Leaf: GoalRest
Leaf: GoalExplore
Leaf: GoalExplore
Leaf: GoalExplore
Leaf: GoalExplore
Leaf: GoalExplore
Leaf: GoalExplore
Node(~[Node(~[Leaf(), Leaf(), Node(~[Leaf(), Leaf(), Leaf(), Node(~[Leaf(), Leaf(), Leaf()])])])])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment