Skip to content

Commit 658bf6d

Browse files
committed
---
yaml --- r: 24542 b: refs/heads/try2 c: a76ed88 h: refs/heads/master v: v3
1 parent 4bdcacc commit 658bf6d

File tree

2 files changed

+61
-4
lines changed

2 files changed

+61
-4
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: cd6f24f9d14ac90d167386a56e7a6ac1f0318195
55
refs/heads/try: ffbe0e0e00374358b789b0037bcb3a577cd218be
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: 41bca84dd95e0ef9ceb9f0a304eca0d62602198b
8+
refs/heads/try2: a76ed88f54f6e7a3f9a85936a7b4576e2e3f3e78
99
refs/heads/incoming: 05543fd04dfb3f63b453a331e239ceb1a9a219f9
1010
refs/heads/dist-snap: 2f32a1581f522e524009138b33b1c7049ced668d
1111
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596

branches/try2/doc/tutorial.md

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ As a curly-brace language in the tradition of C, C++, and JavaScript,
1717
Rust looks a lot like other languages you may be familiar with.
1818

1919
~~~~
20-
fn fac(n: int) -> int {
20+
fn boring_old_factorial(n: int) -> int {
2121
let mut result = 1, i = 1;
2222
while i <= n {
2323
result *= i;
@@ -36,8 +36,65 @@ tendency towards aggressive abbreviation in the keywords—`fn` for
3636
function, `ret` for return.
3737

3838
You should, however, not conclude that Rust is simply an evolution of
39-
C. As will become clear in the rest of this tutorial, it goes in
40-
quite a different direction.
39+
C. As will become clear in the rest of this tutorial, it goes in quite
40+
a different direction, with efficient, strongly-typed and memory-safe
41+
support for many high-level idioms.
42+
43+
Here's a parallel game of rock, paper, scissors to whet your appetite.
44+
45+
~~~~
46+
use std;
47+
48+
import comm::{listen, methods};
49+
import task::spawn;
50+
import iter::repeat;
51+
import std::rand::{seeded_rng, seed};
52+
import uint::range;
53+
import io::println;
54+
55+
fn main() {
56+
// Open a channel for receiving game results
57+
do listen |result_from_game| {
58+
59+
let times = 10;
60+
let player1 = "graydon";
61+
let player2 = "patrick";
62+
63+
for repeat(times) {
64+
// Start another task to play the game
65+
do spawn |copy player1, copy player2| {
66+
let outcome = play_game(player1, player2);
67+
result_from_game.send(outcome);
68+
}
69+
}
70+
71+
// Report the results as the games complete
72+
for range(0, times) |round| {
73+
let winner = result_from_game.recv();
74+
println(#fmt("%s wins round #%u", winner, round));
75+
}
76+
77+
fn play_game(player1: str, player2: str) -> str {
78+
79+
// Our rock/paper/scissors types
80+
enum gesture {
81+
rock, paper, scissors
82+
}
83+
84+
let rng = seeded_rng(seed());
85+
// A small inline function for picking an RPS gesture
86+
let pick = || [rock, paper, scissors][rng.gen_uint() % 3];
87+
88+
// Pick two gestures and decide the result
89+
alt (pick(), pick()) {
90+
(rock, scissors) | (paper, rock) | (scissors, paper) { copy player1 }
91+
(scissors, rock) | (rock, paper) | (paper, scissors) { copy player2 }
92+
_ { "tie" }
93+
}
94+
}
95+
}
96+
}
97+
~~~~
4198

4299
## Conventions
43100

0 commit comments

Comments
 (0)