Skip to content
This repository was archived by the owner on Jan 24, 2023. It is now read-only.

Commit 9ef91ab

Browse files
committed
Cleanup now that rust-lang/rust#3511 is closed
1 parent 5a5c65c commit 9ef91ab

File tree

6 files changed

+37
-107
lines changed

6 files changed

+37
-107
lines changed

Makefile

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@ tutorial/%.html: drafts/%.md
3636
.PHONY: clean
3737
clean:
3838
rm -f tutorial/*
39-
clean-tex:
39+
tclean:
4040
rm -f tutorial/*.aux tutorial/*.log tutorial/*.out
41+
dclean:
42+
rm -f $(DRAFTS)
4143

42-
drafts: $(DRAFTS)
4344
docs: $(WEB)
44-
docs-tex: $(TEX) clean-tex
45-
docs-all: docs docs-tex
45+
tex: $(TEX) tclean
46+
all: docs tex
47+
drafts: $(DRAFTS)

code/05.rs

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -65,58 +65,37 @@ fn stdout_test() {
6565

6666
#[test]
6767
fn number_input() {
68-
use std::num;
69-
70-
let mut ret;
71-
7268
loop {
73-
match from_str::<int>("123\n".trim_right_chars(&'\n')) {
74-
Some(x) => {
75-
ret = num::abs(x).to_uint().unwrap();
76-
break;
77-
}
78-
None => {
79-
println!("Yea, well... better with a number.");
80-
}
69+
match from_str::<uint>("123\n".trim_right_chars(&'\n')) {
70+
Some(x) => assert_eq!(x, 123); break,
71+
None => println!("I'd rather have a number.")
8172
}
8273
}
83-
84-
assert_eq!(ret, 123);
8574
}
8675

8776
#[test]
8877
fn test_solution() {
8978
use std::io::BufferedReader;
9079
use std::io::stdin;
91-
use std::num;
9280

9381
fn input_line() -> ~str {
9482
let mut _reader = BufferedReader::new(stdin());
9583
loop {
9684
match Some(~"50\n") {
9785
Some(~"\n") => println!("\nUhm, please type something..."),
98-
Some(thing) => { return thing },
86+
Some(thing) => return thing,
9987
None => continue
10088
}
10189
}
10290
}
10391

10492
fn input_number() -> uint {
105-
let mut ret;
106-
10793
loop {
108-
let input = input_line();
109-
match from_str::<int>(input.trim_right_chars(&'\n')) {
110-
Some(x) => {
111-
ret = num::abs(x).to_uint().unwrap();
112-
break;
113-
}
114-
None => {
115-
println!("Yea, well... better with a number.");
116-
}
94+
match from_str::<uint>(input_line().trim_right_chars(&'\n')) {
95+
Some(x) => return x,
96+
None => println!("I'd rather have a number.")
11797
}
11898
}
119-
ret
12099
}
121100

122101
let nbr = 50;

source/ch-05.md

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ fn main() {
2828
let mut cur;
2929
for _ in range(0, 11) {
3030
// nb: range upper bound is exclusive
31-
cur = task_rng().gen_range(1, 101);
31+
cur = task_rng().gen_range(1u, 101);
3232
println!("{}", cur);
3333
}
3434
}
@@ -134,22 +134,15 @@ Here is an example that uses a `loop`:
134134

135135
~~~~rust
136136
use std::io::stdin;
137-
use std::num;
138137

139138
fn number_input() {
140-
let mut ret;
141139
println!("Please type a number: ");
142140

143141
loop {
144142
// We need to trim the EOL `\n` char to be able to convert
145-
match from_str::<int>(stdin().read_line().trim_right_chars(&'\n')) {
146-
Some(x) => {
147-
ret = num::abs(x).to_uint().unwrap();
148-
break;
149-
}
150-
None => {
151-
println!("Yea, well... better with a number.");
152-
}
143+
match from_str::<uint>(stdin().read_line().trim_right_chars(&'\n')) {
144+
Some(x) => return x,
145+
None => println!("I'd rather have a number.")
153146
}
154147
}
155148
}
@@ -169,38 +162,28 @@ So here it is:
169162
~~~~rust
170163
use std::io::BufferedReader;
171164
use std::io::stdin;
172-
use std::num;
173165
use std::rand::{task_rng, Rng};
174166

175167
fn input_line() -> ~str {
176168
let mut reader = BufferedReader::new(stdin());
177169
loop {
178170
match reader.read_line() {
179171
Some(~"\n") => println!("\nUhm, please type something..."),
180-
Some(thing) => { return thing },
172+
Some(thing) => return thing,
181173
None => continue
182174
}
183175
}
184176
}
185177

186178
fn input_number() -> uint {
187-
let mut ret;
188179
println!("Please type a number: ");
189180

190181
loop {
191-
// it is important to allocate here for lifetime; cf. following chapter!
192-
let input = input_line();
193-
match from_str::<int>(input.trim_right_chars(&'\n')) {
194-
Some(x) => {
195-
ret = num::abs(x).to_uint().unwrap();
196-
break;
197-
}
198-
None => {
199-
println!("Yea, well... better with a number.");
200-
}
182+
match from_str::<uint>(input_line().trim_right_chars(&'\n')) {
183+
Some(x) => return x,
184+
None => println!("I'd rather have a number.")
201185
}
202186
}
203-
ret
204187
}
205188

206189
fn main() {

tutorial/ch-05.html

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ <h2>Getting some RNG</h2>
6565
<span class="kw">let</span> <span class="kw">mut</span> cur;
6666
<span class="kw">for</span> _ in range(<span class="dv">0</span>, <span class="dv">11</span>) {
6767
<span class="co">// nb: range upper bound is exclusive</span>
68-
cur = task_rng().gen_range(<span class="dv">1</span>, <span class="dv">101</span>);
68+
cur = task_rng().gen_range(<span class="dv">1u</span>, <span class="dv">101</span>);
6969
println!(<span class="st">&quot;{}&quot;</span>, cur);
7070
}
7171
}</code></pre>
@@ -123,22 +123,15 @@ <h2>Calling the user… err?</h2>
123123
println!(reader.read_line().unwrap());</code></pre>
124124
<p>Now we want to get an int out of <code>stdin</code>, but looking at the empty case, the program could behave strangely depending on what we are doing with it. So instead we could ask the user back for input (by a recursive call to our input function, for example) or stop with a specific error message.<br />Here is an example that uses a <code>loop</code>:</p>
125125
<pre class="sourceCode rust"><code class="sourceCode rust"><span class="kw">use</span> std::io::stdin;
126-
<span class="kw">use</span> std::num;
127126

128127
<span class="kw">fn</span> number_input() {
129-
<span class="kw">let</span> <span class="kw">mut</span> ret;
130128
println!(<span class="st">&quot;Please type a number: &quot;</span>);
131129

132130
<span class="kw">loop</span> {
133131
<span class="co">// We need to trim the EOL `\n` char to be able to convert</span>
134-
<span class="kw">match</span> from_str::&lt;<span class="kw">int</span>&gt;(stdin().read_line().trim_right_chars(&amp;<span class="ch">&#39;\n&#39;</span>)) {
135-
<span class="kw">Some</span>(x) =&gt; {
136-
ret = num::abs(x).to_uint().unwrap();
137-
<span class="kw">break</span>;
138-
}
139-
<span class="kw">None</span> =&gt; {
140-
println!(<span class="st">&quot;Yea, well... better with a number.&quot;</span>);
141-
}
132+
<span class="kw">match</span> from_str::&lt;<span class="kw">uint</span>&gt;(stdin().read_line().trim_right_chars(&amp;<span class="ch">&#39;\n&#39;</span>)) {
133+
<span class="kw">Some</span>(x) =&gt; <span class="kw">return</span> x,
134+
<span class="kw">None</span> =&gt; println!(<span class="st">&quot;I&#39;d rather have a number.&quot;</span>)
142135
}
143136
}
144137
}</code></pre>
@@ -150,38 +143,28 @@ <h2>The Solution (well, an implementation of it)</h2>
150143
<p>So here it is:</p>
151144
<pre class="sourceCode rust"><code class="sourceCode rust"><span class="kw">use</span> std::io::BufferedReader;
152145
<span class="kw">use</span> std::io::stdin;
153-
<span class="kw">use</span> std::num;
154146
<span class="kw">use</span> std::rand::{task_rng, Rng};
155147

156148
<span class="kw">fn</span> input_line() -&gt; ~<span class="kw">str</span> {
157149
<span class="kw">let</span> <span class="kw">mut</span> reader = BufferedReader::new(stdin());
158150
<span class="kw">loop</span> {
159151
<span class="kw">match</span> reader.read_line() {
160152
<span class="kw">Some</span>(~<span class="st">&quot;</span><span class="ch">\n</span><span class="st">&quot;</span>) =&gt; println!(<span class="st">&quot;</span><span class="ch">\n</span><span class="st">Uhm, please type something...&quot;</span>),
161-
<span class="kw">Some</span>(thing) =&gt; { <span class="kw">return</span> thing },
153+
<span class="kw">Some</span>(thing) =&gt; <span class="kw">return</span> thing,
162154
<span class="kw">None</span> =&gt; continue
163155
}
164156
}
165157
}
166158

167159
<span class="kw">fn</span> input_number() -&gt; <span class="kw">uint</span> {
168-
<span class="kw">let</span> <span class="kw">mut</span> ret;
169160
println!(<span class="st">&quot;Please type a number: &quot;</span>);
170161

171162
<span class="kw">loop</span> {
172-
<span class="co">// it is important to allocate here for lifetime; cf. following chapter!</span>
173-
<span class="kw">let</span> input = input_line();
174-
<span class="kw">match</span> from_str::&lt;<span class="kw">int</span>&gt;(input.trim_right_chars(&amp;<span class="ch">&#39;\n&#39;</span>)) {
175-
<span class="kw">Some</span>(x) =&gt; {
176-
ret = num::abs(x).to_uint().unwrap();
177-
<span class="kw">break</span>;
178-
}
179-
<span class="kw">None</span> =&gt; {
180-
println!(<span class="st">&quot;Yea, well... better with a number.&quot;</span>);
181-
}
163+
<span class="kw">match</span> from_str::&lt;<span class="kw">uint</span>&gt;(input_line().trim_right_chars(&amp;<span class="ch">&#39;\n&#39;</span>)) {
164+
<span class="kw">Some</span>(x) =&gt; <span class="kw">return</span> x,
165+
<span class="kw">None</span> =&gt; println!(<span class="st">&quot;I&#39;d rather have a number.&quot;</span>)
182166
}
183167
}
184-
ret
185168
}
186169

187170
<span class="kw">fn</span> main() {

tutorial/tutorial.html

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ <h2>Getting some RNG</h2>
475475
<span class="kw">let</span> <span class="kw">mut</span> cur;
476476
<span class="kw">for</span> _ in range(<span class="dv">0</span>, <span class="dv">11</span>) {
477477
<span class="co">// nb: range upper bound is exclusive</span>
478-
cur = task_rng().gen_range(<span class="dv">1</span>, <span class="dv">101</span>);
478+
cur = task_rng().gen_range(<span class="dv">1u</span>, <span class="dv">101</span>);
479479
println!(<span class="st">&quot;{}&quot;</span>, cur);
480480
}
481481
}</code></pre>
@@ -533,22 +533,15 @@ <h2>Calling the user… err?</h2>
533533
println!(reader.read_line().unwrap());</code></pre>
534534
<p>Now we want to get an int out of <code>stdin</code>, but looking at the empty case, the program could behave strangely depending on what we are doing with it. So instead we could ask the user back for input (by a recursive call to our input function, for example) or stop with a specific error message.<br />Here is an example that uses a <code>loop</code>:</p>
535535
<pre class="sourceCode rust"><code class="sourceCode rust"><span class="kw">use</span> std::io::stdin;
536-
<span class="kw">use</span> std::num;
537536

538537
<span class="kw">fn</span> number_input() {
539-
<span class="kw">let</span> <span class="kw">mut</span> ret;
540538
println!(<span class="st">&quot;Please type a number: &quot;</span>);
541539

542540
<span class="kw">loop</span> {
543541
<span class="co">// We need to trim the EOL `\n` char to be able to convert</span>
544-
<span class="kw">match</span> from_str::&lt;<span class="kw">int</span>&gt;(stdin().read_line().trim_right_chars(&amp;<span class="ch">&#39;\n&#39;</span>)) {
545-
<span class="kw">Some</span>(x) =&gt; {
546-
ret = num::abs(x).to_uint().unwrap();
547-
<span class="kw">break</span>;
548-
}
549-
<span class="kw">None</span> =&gt; {
550-
println!(<span class="st">&quot;Yea, well... better with a number.&quot;</span>);
551-
}
542+
<span class="kw">match</span> from_str::&lt;<span class="kw">uint</span>&gt;(stdin().read_line().trim_right_chars(&amp;<span class="ch">&#39;\n&#39;</span>)) {
543+
<span class="kw">Some</span>(x) =&gt; <span class="kw">return</span> x,
544+
<span class="kw">None</span> =&gt; println!(<span class="st">&quot;I&#39;d rather have a number.&quot;</span>)
552545
}
553546
}
554547
}</code></pre>
@@ -560,38 +553,28 @@ <h2>The Solution (well, an implementation of it)</h2>
560553
<p>So here it is:</p>
561554
<pre class="sourceCode rust"><code class="sourceCode rust"><span class="kw">use</span> std::io::BufferedReader;
562555
<span class="kw">use</span> std::io::stdin;
563-
<span class="kw">use</span> std::num;
564556
<span class="kw">use</span> std::rand::{task_rng, Rng};
565557

566558
<span class="kw">fn</span> input_line() -&gt; ~<span class="kw">str</span> {
567559
<span class="kw">let</span> <span class="kw">mut</span> reader = BufferedReader::new(stdin());
568560
<span class="kw">loop</span> {
569561
<span class="kw">match</span> reader.read_line() {
570562
<span class="kw">Some</span>(~<span class="st">&quot;</span><span class="ch">\n</span><span class="st">&quot;</span>) =&gt; println!(<span class="st">&quot;</span><span class="ch">\n</span><span class="st">Uhm, please type something...&quot;</span>),
571-
<span class="kw">Some</span>(thing) =&gt; { <span class="kw">return</span> thing },
563+
<span class="kw">Some</span>(thing) =&gt; <span class="kw">return</span> thing,
572564
<span class="kw">None</span> =&gt; continue
573565
}
574566
}
575567
}
576568

577569
<span class="kw">fn</span> input_number() -&gt; <span class="kw">uint</span> {
578-
<span class="kw">let</span> <span class="kw">mut</span> ret;
579570
println!(<span class="st">&quot;Please type a number: &quot;</span>);
580571

581572
<span class="kw">loop</span> {
582-
<span class="co">// it is important to allocate here for lifetime; cf. following chapter!</span>
583-
<span class="kw">let</span> input = input_line();
584-
<span class="kw">match</span> from_str::&lt;<span class="kw">int</span>&gt;(input.trim_right_chars(&amp;<span class="ch">&#39;\n&#39;</span>)) {
585-
<span class="kw">Some</span>(x) =&gt; {
586-
ret = num::abs(x).to_uint().unwrap();
587-
<span class="kw">break</span>;
588-
}
589-
<span class="kw">None</span> =&gt; {
590-
println!(<span class="st">&quot;Yea, well... better with a number.&quot;</span>);
591-
}
573+
<span class="kw">match</span> from_str::&lt;<span class="kw">uint</span>&gt;(input_line().trim_right_chars(&amp;<span class="ch">&#39;\n&#39;</span>)) {
574+
<span class="kw">Some</span>(x) =&gt; <span class="kw">return</span> x,
575+
<span class="kw">None</span> =&gt; println!(<span class="st">&quot;I&#39;d rather have a number.&quot;</span>)
592576
}
593577
}
594-
ret
595578
}
596579

597580
<span class="kw">fn</span> main() {

tutorial/tutorial.pdf

-861 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)