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

Commit

Permalink
Cleanup now that rust-lang/rust#3511 is closed
Browse files Browse the repository at this point in the history
  • Loading branch information
adrientetar committed Jan 26, 2014
1 parent 5a5c65c commit 9ef91ab
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 107 deletions.
10 changes: 6 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ tutorial/%.html: drafts/%.md
.PHONY: clean
clean:
rm -f tutorial/*
clean-tex:
tclean:
rm -f tutorial/*.aux tutorial/*.log tutorial/*.out
dclean:
rm -f $(DRAFTS)

drafts: $(DRAFTS)
docs: $(WEB)
docs-tex: $(TEX) clean-tex
docs-all: docs docs-tex
tex: $(TEX) tclean
all: docs tex
drafts: $(DRAFTS)
35 changes: 7 additions & 28 deletions code/05.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,58 +65,37 @@ fn stdout_test() {

#[test]
fn number_input() {
use std::num;

let mut ret;

loop {
match from_str::<int>("123\n".trim_right_chars(&'\n')) {
Some(x) => {
ret = num::abs(x).to_uint().unwrap();
break;
}
None => {
println!("Yea, well... better with a number.");
}
match from_str::<uint>("123\n".trim_right_chars(&'\n')) {
Some(x) => assert_eq!(x, 123); break,
None => println!("I'd rather have a number.")
}
}

assert_eq!(ret, 123);
}

#[test]
fn test_solution() {
use std::io::BufferedReader;
use std::io::stdin;
use std::num;

fn input_line() -> ~str {
let mut _reader = BufferedReader::new(stdin());
loop {
match Some(~"50\n") {
Some(~"\n") => println!("\nUhm, please type something..."),
Some(thing) => { return thing },
Some(thing) => return thing,
None => continue
}
}
}

fn input_number() -> uint {
let mut ret;

loop {
let input = input_line();
match from_str::<int>(input.trim_right_chars(&'\n')) {
Some(x) => {
ret = num::abs(x).to_uint().unwrap();
break;
}
None => {
println!("Yea, well... better with a number.");
}
match from_str::<uint>(input_line().trim_right_chars(&'\n')) {
Some(x) => return x,
None => println!("I'd rather have a number.")
}
}
ret
}

let nbr = 50;
Expand Down
33 changes: 8 additions & 25 deletions source/ch-05.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn main() {
let mut cur;
for _ in range(0, 11) {
// nb: range upper bound is exclusive
cur = task_rng().gen_range(1, 101);
cur = task_rng().gen_range(1u, 101);
println!("{}", cur);
}
}
Expand Down Expand Up @@ -134,22 +134,15 @@ Here is an example that uses a `loop`:

~~~~rust
use std::io::stdin;
use std::num;

fn number_input() {
let mut ret;
println!("Please type a number: ");

loop {
// We need to trim the EOL `\n` char to be able to convert
match from_str::<int>(stdin().read_line().trim_right_chars(&'\n')) {
Some(x) => {
ret = num::abs(x).to_uint().unwrap();
break;
}
None => {
println!("Yea, well... better with a number.");
}
match from_str::<uint>(stdin().read_line().trim_right_chars(&'\n')) {
Some(x) => return x,
None => println!("I'd rather have a number.")
}
}
}
Expand All @@ -169,38 +162,28 @@ So here it is:
~~~~rust
use std::io::BufferedReader;
use std::io::stdin;
use std::num;
use std::rand::{task_rng, Rng};

fn input_line() -> ~str {
let mut reader = BufferedReader::new(stdin());
loop {
match reader.read_line() {
Some(~"\n") => println!("\nUhm, please type something..."),
Some(thing) => { return thing },
Some(thing) => return thing,
None => continue
}
}
}

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

loop {
// it is important to allocate here for lifetime; cf. following chapter!
let input = input_line();
match from_str::<int>(input.trim_right_chars(&'\n')) {
Some(x) => {
ret = num::abs(x).to_uint().unwrap();
break;
}
None => {
println!("Yea, well... better with a number.");
}
match from_str::<uint>(input_line().trim_right_chars(&'\n')) {
Some(x) => return x,
None => println!("I'd rather have a number.")
}
}
ret
}

fn main() {
Expand Down
33 changes: 8 additions & 25 deletions tutorial/ch-05.html
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ <h2>Getting some RNG</h2>
<span class="kw">let</span> <span class="kw">mut</span> cur;
<span class="kw">for</span> _ in range(<span class="dv">0</span>, <span class="dv">11</span>) {
<span class="co">// nb: range upper bound is exclusive</span>
cur = task_rng().gen_range(<span class="dv">1</span>, <span class="dv">101</span>);
cur = task_rng().gen_range(<span class="dv">1u</span>, <span class="dv">101</span>);
println!(<span class="st">&quot;{}&quot;</span>, cur);
}
}</code></pre>
Expand Down Expand Up @@ -123,22 +123,15 @@ <h2>Calling the user… err?</h2>
println!(reader.read_line().unwrap());</code></pre>
<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>
<pre class="sourceCode rust"><code class="sourceCode rust"><span class="kw">use</span> std::io::stdin;
<span class="kw">use</span> std::num;

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

<span class="kw">loop</span> {
<span class="co">// We need to trim the EOL `\n` char to be able to convert</span>
<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>)) {
<span class="kw">Some</span>(x) =&gt; {
ret = num::abs(x).to_uint().unwrap();
<span class="kw">break</span>;
}
<span class="kw">None</span> =&gt; {
println!(<span class="st">&quot;Yea, well... better with a number.&quot;</span>);
}
<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>)) {
<span class="kw">Some</span>(x) =&gt; <span class="kw">return</span> x,
<span class="kw">None</span> =&gt; println!(<span class="st">&quot;I&#39;d rather have a number.&quot;</span>)
}
}
}</code></pre>
Expand All @@ -150,38 +143,28 @@ <h2>The Solution (well, an implementation of it)</h2>
<p>So here it is:</p>
<pre class="sourceCode rust"><code class="sourceCode rust"><span class="kw">use</span> std::io::BufferedReader;
<span class="kw">use</span> std::io::stdin;
<span class="kw">use</span> std::num;
<span class="kw">use</span> std::rand::{task_rng, Rng};

<span class="kw">fn</span> input_line() -&gt; ~<span class="kw">str</span> {
<span class="kw">let</span> <span class="kw">mut</span> reader = BufferedReader::new(stdin());
<span class="kw">loop</span> {
<span class="kw">match</span> reader.read_line() {
<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>),
<span class="kw">Some</span>(thing) =&gt; { <span class="kw">return</span> thing },
<span class="kw">Some</span>(thing) =&gt; <span class="kw">return</span> thing,
<span class="kw">None</span> =&gt; continue
}
}
}

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

<span class="kw">loop</span> {
<span class="co">// it is important to allocate here for lifetime; cf. following chapter!</span>
<span class="kw">let</span> input = input_line();
<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>)) {
<span class="kw">Some</span>(x) =&gt; {
ret = num::abs(x).to_uint().unwrap();
<span class="kw">break</span>;
}
<span class="kw">None</span> =&gt; {
println!(<span class="st">&quot;Yea, well... better with a number.&quot;</span>);
}
<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>)) {
<span class="kw">Some</span>(x) =&gt; <span class="kw">return</span> x,
<span class="kw">None</span> =&gt; println!(<span class="st">&quot;I&#39;d rather have a number.&quot;</span>)
}
}
ret
}

<span class="kw">fn</span> main() {
Expand Down
33 changes: 8 additions & 25 deletions tutorial/tutorial.html
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ <h2>Getting some RNG</h2>
<span class="kw">let</span> <span class="kw">mut</span> cur;
<span class="kw">for</span> _ in range(<span class="dv">0</span>, <span class="dv">11</span>) {
<span class="co">// nb: range upper bound is exclusive</span>
cur = task_rng().gen_range(<span class="dv">1</span>, <span class="dv">101</span>);
cur = task_rng().gen_range(<span class="dv">1u</span>, <span class="dv">101</span>);
println!(<span class="st">&quot;{}&quot;</span>, cur);
}
}</code></pre>
Expand Down Expand Up @@ -533,22 +533,15 @@ <h2>Calling the user… err?</h2>
println!(reader.read_line().unwrap());</code></pre>
<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>
<pre class="sourceCode rust"><code class="sourceCode rust"><span class="kw">use</span> std::io::stdin;
<span class="kw">use</span> std::num;

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

<span class="kw">loop</span> {
<span class="co">// We need to trim the EOL `\n` char to be able to convert</span>
<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>)) {
<span class="kw">Some</span>(x) =&gt; {
ret = num::abs(x).to_uint().unwrap();
<span class="kw">break</span>;
}
<span class="kw">None</span> =&gt; {
println!(<span class="st">&quot;Yea, well... better with a number.&quot;</span>);
}
<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>)) {
<span class="kw">Some</span>(x) =&gt; <span class="kw">return</span> x,
<span class="kw">None</span> =&gt; println!(<span class="st">&quot;I&#39;d rather have a number.&quot;</span>)
}
}
}</code></pre>
Expand All @@ -560,38 +553,28 @@ <h2>The Solution (well, an implementation of it)</h2>
<p>So here it is:</p>
<pre class="sourceCode rust"><code class="sourceCode rust"><span class="kw">use</span> std::io::BufferedReader;
<span class="kw">use</span> std::io::stdin;
<span class="kw">use</span> std::num;
<span class="kw">use</span> std::rand::{task_rng, Rng};

<span class="kw">fn</span> input_line() -&gt; ~<span class="kw">str</span> {
<span class="kw">let</span> <span class="kw">mut</span> reader = BufferedReader::new(stdin());
<span class="kw">loop</span> {
<span class="kw">match</span> reader.read_line() {
<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>),
<span class="kw">Some</span>(thing) =&gt; { <span class="kw">return</span> thing },
<span class="kw">Some</span>(thing) =&gt; <span class="kw">return</span> thing,
<span class="kw">None</span> =&gt; continue
}
}
}

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

<span class="kw">loop</span> {
<span class="co">// it is important to allocate here for lifetime; cf. following chapter!</span>
<span class="kw">let</span> input = input_line();
<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>)) {
<span class="kw">Some</span>(x) =&gt; {
ret = num::abs(x).to_uint().unwrap();
<span class="kw">break</span>;
}
<span class="kw">None</span> =&gt; {
println!(<span class="st">&quot;Yea, well... better with a number.&quot;</span>);
}
<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>)) {
<span class="kw">Some</span>(x) =&gt; <span class="kw">return</span> x,
<span class="kw">None</span> =&gt; println!(<span class="st">&quot;I&#39;d rather have a number.&quot;</span>)
}
}
ret
}

<span class="kw">fn</span> main() {
Expand Down
Binary file modified tutorial/tutorial.pdf
Binary file not shown.

0 comments on commit 9ef91ab

Please sign in to comment.