Skip to content

Instantly share code, notes, and snippets.

@cengiz-io
Created August 18, 2014 12:46
Show Gist options
  • Save cengiz-io/e0cf83ed7659d44bbaf8 to your computer and use it in GitHub Desktop.
Save cengiz-io/e0cf83ed7659d44bbaf8 to your computer and use it in GitHub Desktop.
Playing with Rust
#![feature(struct_variant)]
extern crate debug;
enum Direction {
Up, Down, Left, Right
}
/*
use std::fmt;
impl Direction {
fn string(&self) -> &str {
match *self {
Up => "Up",
Down => "Down",
Left => "Left",
Right => "Right"
}
}
}
impl std::fmt::Show for Direction {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.string())
}
}
*/
struct Point {
x: f64, y: f64
}
enum Shape {
Circle { center: Point, radius: f64 },
Arrow { center: Point, direction: Direction },
Trapezoid
}
fn write_shape(s: Shape) {
match s {
Circle { center, radius } => println!(
"Circle x={}, y={}, r={}",
center.x,
center.y,
radius
),
Arrow { center, direction } => println!(
"Arrow x={}, y={}, d={:?}",
center.x,
center.y,
direction
),
_ => println!("Unknown shape")
}
}
fn main() {
write_shape(Circle { center: Point{ x: 1.0, y: 1.0 }, radius: 1.0 });
write_shape(Arrow { center: Point{ x: 0.5, y: 0.5 }, direction: Up });
write_shape(Trapezoid);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment