Przeglądaj źródła

add old rust tour.

e.g. starting with the tutorial for 0.5.
Lucas Stadler 11 lat temu
rodzic
commit
9c35d2cb98
1 zmienionych plików z 121 dodań i 0 usunięć
  1. 121 0
      rust/hello.rs

+ 121 - 0
rust/hello.rs

@ -0,0 +1,121 @@
1
/*
2
 * playing with rust, it's quite interesting so far.
3
 *
4
 * http://static.rust-lang.org/doc/0.5/tutorial.html
5
 */
6
7
/** a pointy structure */
8
struct Point { x: float, y: float }
9
/** shapey things */
10
enum Shape {
11
	Circle(Point, float),
12
	Rectangle(Point, Point)
13
}
14
15
impl Shape {
16
	/** calculate the area of a shape */
17
	fn area(&self) -> float {
18
		area(*self)
19
	}
20
}
21
22
impl Shape {
23
	fn weird(&self) -> int {
24
		42
25
	}
26
}
27
28
fn area(shape: Shape) -> float {
29
	match shape {
30
		Circle(_, size) => float::consts::pi * size * size,
31
		// Rectangle(p1, p2) => (p2.x - p1.x) * (p2.y - p1.y)
32
		Rectangle(Point {x: x1, y: y1}, Point {x: x2, y:y2}) =>
33
			(x2 - x1) * (y2 - y1)
34
	}
35
}
36
37
fn main() {
38
	io::println("Hello, Rust!?");
39
	
40
	let p1 = Point {x: 10f, y: 5f};
41
	let p2 = Point {x: 50f, y: 10f};
42
	let r1 = Rectangle(p1, p2);
43
	io::println(fmt!("area: %f", area(r1)));
44
	
45
	let x = @10;
46
	let y = ~10;
47
	let z: ~int = y; // y is 'moved' (i.e. can't be used afterwards)
48
	let z_copy: ~int = copy z;
49
	io::println(fmt!("%? %? %?", *x, *z, *z_copy));
50
	io::println(fmt!("%? %? %?", **&x, **&z, **&z_copy));
51
	//assert x != z; // yields a type error (i.e. == is a sane operator)
52
	assert z == z_copy;
53
	
54
	let countables = ~[1, 2, 3];
55
	let not_countables = ~[42];
56
	let stuff = countables + not_countables;
57
	io::println(fmt!("%? %?", stuff, countables + not_countables));
58
	assert stuff == countables + not_countables;
59
	
60
	let mut cs = ~[1, 2, 3, 4];
61
	cs += [3];
62
	io::println(fmt!("%?", [cs[0], cs[2]]));
63
	
64
	for cs.each |c| {
65
		io::println(fmt!("%?", *c));
66
	}
67
	
68
	fn each(v: &[int], op: fn(v: &int)) {
69
		let mut n = 0;
70
		while n < v.len() {
71
			op(&v[n]);
72
			n += 1;
73
		}
74
	}
75
	
76
	do each(cs) |c| { // must really by of type fn(v:&int) -> ()
77
		*c + 3;
78
	}
79
	
80
	fn faked_each(v: &[int], op: fn(v: &int) -> bool) {
81
		if v.len() < 3 {
82
			return;
83
		}
84
		
85
		if !op(&v[0]) { return; }
86
		if !op(&v[1]) { return; }
87
		if !op(&v[2]) { return; }
88
	}
89
	
90
	for faked_each([1, 2, 3, 4, 5, 6]) |&n| {
91
		io::println(fmt!("%d", n));
92
		if n % 2 == 0 {
93
			break;
94
		}
95
	}
96
97
	fn first_even(v: &[int]) -> int {
98
		for faked_each(v) |&n| {
99
			if n % 2 == 0 {
100
				return n;
101
			}
102
		}
103
		return -1;
104
	}
105
	let x = first_even([1, 2, 3, 4, 5, 6, 7]); io::println(fmt!("%?", x));
106
	
107
	io::println(fmt!("%? %?", r1.area(), r1.weird()));
108
	
109
	fn map<T, U>(vector: &[T], function: fn(v: T) -> U) -> ~[U] {
110
		let mut accum = ~[];
111
		for vector.each |&v| {
112
			accum.push(function(v));
113
		}
114
		return accum;
115
	}
116
	
117
	let cs2 = do map(cs) |c| {
118
		c + 1
119
	};
120
	io::println(fmt!("%?", cs2));
121
}