Skip to content

Commit 2d4330f

Browse files
committed
---
yaml --- r: 194942 b: refs/heads/auto c: 8fe7f1f h: refs/heads/master v: v3
1 parent 09de218 commit 2d4330f

File tree

28 files changed

+543
-314
lines changed

28 files changed

+543
-314
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503
1010
refs/tags/release-0.3.1: 495bae036dfe5ec6ceafd3312b4dca48741e845b
1111
refs/tags/release-0.4: e828ea2080499553b97dfe33b3f4d472b4562ad7
1212
refs/tags/release-0.5: 7e3bcfbf21278251ee936ad53e92e9b719702d73
13-
refs/heads/auto: b27ba527c5cee06f43967daf3a0dd01a2258a0fa
13+
refs/heads/auto: 8fe7f1fa97884c233eb4b8128197431a9b1506ed
1414
refs/heads/servo: af82457af293e2a842ba6b7759b70288da276167
1515
refs/tags/release-0.6: b4ebcfa1812664df5e142f0134a5faea3918544c
1616
refs/tags/0.1: b19db808c2793fe2976759b85a355c3ad8c8b336

branches/auto/AUTHORS.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ Peter Schuller <[email protected]>
606606
Peter Williams <[email protected]>
607607
Peter Zotov <[email protected]>
608608
Petter Remen <[email protected]>
609-
Phil Dawes <[email protected]>
609+
Phil Dawes <[email protected]>
610610
Phil Ruffwind <[email protected]>
611611
Philip Munksgaard <[email protected]>
612612
Philipp Brüschweiler <[email protected]>

branches/auto/src/doc/trpl/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* [Looping](looping.md)
1414
* [Strings](strings.md)
1515
* [Arrays, Vectors, and Slices](arrays-vectors-and-slices.md)
16+
* [Standard Input](standard-input.md)
1617
* [Intermediate Rust](intermediate.md)
1718
* [Crates and Modules](crates-and-modules.md)
1819
* [Testing](testing.md)

branches/auto/src/doc/trpl/documentation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ can be useful when changing some options, or when writing a macro.
517517

518518
### Re-exports
519519

520-
`rustdoc` will show the documentation for a public re-export in both places:
520+
`rustdoc` will show the documentation for a publc re-export in both places:
521521

522522
```ignore
523523
extern crate foo;
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
% Standard Input
2+
3+
Getting input from the keyboard is pretty easy, but uses some things
4+
we haven't seen before. Here's a simple program that reads some input,
5+
and then prints it back out:
6+
7+
```{rust,ignore}
8+
# #![feature(old_io)]
9+
fn main() {
10+
println!("Type something!");
11+
12+
let input = std::old_io::stdin().read_line().ok().expect("Failed to read line");
13+
14+
println!("{}", input);
15+
}
16+
```
17+
18+
Let's go over these chunks, one by one:
19+
20+
```{rust,ignore}
21+
std::old_io::stdin();
22+
```
23+
24+
This calls a function, `stdin()`, that lives inside the `std::old_io` module. As
25+
you can imagine, everything in `std` is provided by Rust, the 'standard
26+
library.' We'll talk more about the module system later.
27+
28+
Since writing the fully qualified name all the time is annoying, we can use
29+
the `use` statement to import it in:
30+
31+
```{rust}
32+
# #![feature(old_io)]
33+
use std::old_io::stdin;
34+
35+
stdin();
36+
```
37+
38+
However, it's considered better practice to not import individual functions, but
39+
to import the module, and only use one level of qualification:
40+
41+
```{rust}
42+
# #![feature(old_io)]
43+
use std::old_io;
44+
45+
old_io::stdin();
46+
```
47+
48+
Let's update our example to use this style:
49+
50+
```{rust,ignore}
51+
use std::old_io;
52+
53+
fn main() {
54+
println!("Type something!");
55+
56+
let input = old_io::stdin().read_line().ok().expect("Failed to read line");
57+
58+
println!("{}", input);
59+
}
60+
```
61+
62+
Next up:
63+
64+
```{rust,ignore}
65+
.read_line()
66+
```
67+
68+
The `read_line()` method can be called on the result of `stdin()` to return
69+
a full line of input. Nice and easy.
70+
71+
```{rust,ignore}
72+
.ok().expect("Failed to read line");
73+
```
74+
75+
Do you remember this code?
76+
77+
```{rust}
78+
enum OptionalInt {
79+
Value(i32),
80+
Missing,
81+
}
82+
83+
fn main() {
84+
let x = OptionalInt::Value(5);
85+
let y = OptionalInt::Missing;
86+
87+
match x {
88+
OptionalInt::Value(n) => println!("x is {}", n),
89+
OptionalInt::Missing => println!("x is missing!"),
90+
}
91+
92+
match y {
93+
OptionalInt::Value(n) => println!("y is {}", n),
94+
OptionalInt::Missing => println!("y is missing!"),
95+
}
96+
}
97+
```
98+
99+
We had to match each time to see if we had a value or not. In this case,
100+
though, we _know_ that `x` has a `Value`, but `match` forces us to handle
101+
the `missing` case. This is what we want 99% of the time, but sometimes, we
102+
know better than the compiler.
103+
104+
Likewise, `read_line()` does not return a line of input. It _might_ return a
105+
line of input, though it might also fail to do so. This could happen if our program
106+
isn't running in a terminal, but as part of a cron job, or some other context
107+
where there's no standard input. Because of this, `read_line` returns a type
108+
very similar to our `OptionalInt`: an `IoResult<T>`. We haven't talked about
109+
`IoResult<T>` yet because it is the *generic* form of our `OptionalInt`.
110+
Until then, you can think of it as being the same thing, just for any type –
111+
not just `i32`s.
112+
113+
Rust provides a method on these `IoResult<T>`s called `ok()`, which does the
114+
same thing as our `match` statement but assumes that we have a valid value.
115+
We then call `expect()` on the result, which will terminate our program if we
116+
don't have a valid value. In this case, if we can't get input, our program
117+
doesn't work, so we're okay with that. In most cases, we would want to handle
118+
the error case explicitly. `expect()` allows us to give an error message if
119+
this crash happens.
120+
121+
We will cover the exact details of how all of this works later in the Guide in
122+
[Error Handling]. For now, this gives you enough of a basic understanding to
123+
work with.
124+
125+
Back to the code we were working on! Here's a refresher:
126+
127+
```{rust,ignore}
128+
use std::old_io;
129+
130+
fn main() {
131+
println!("Type something!");
132+
133+
let input = old_io::stdin().read_line().ok().expect("Failed to read line");
134+
135+
println!("{}", input);
136+
}
137+
```
138+
139+
With long lines like this, Rust gives you some flexibility with the whitespace.
140+
We _could_ write the example like this:
141+
142+
```{rust,ignore}
143+
use std::old_io;
144+
145+
fn main() {
146+
println!("Type something!");
147+
148+
// here, we'll show the types at each step
149+
150+
let input = old_io::stdin() // std::old_io::stdio::StdinReader
151+
.read_line() // IoResult<String>
152+
.ok() // Option<String>
153+
.expect("Failed to read line"); // String
154+
155+
println!("{}", input);
156+
}
157+
```
158+
159+
Sometimes, this makes things more readable – sometimes, less. Use your judgement
160+
here.
161+
162+
That's all you need to get basic input from the standard input! It's not too
163+
complicated, but there are a number of small parts.
164+
165+
166+
[Error Handling]: ./error-handling.html

branches/auto/src/libcollections/btree/map.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -904,7 +904,14 @@ impl<K: Ord, V: Ord> Ord for BTreeMap<K, V> {
904904
#[stable(feature = "rust1", since = "1.0.0")]
905905
impl<K: Debug, V: Debug> Debug for BTreeMap<K, V> {
906906
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
907-
self.iter().fold(f.debug_map(), |b, (k, v)| b.entry(k, v)).finish()
907+
try!(write!(f, "{{"));
908+
909+
for (i, (k, v)) in self.iter().enumerate() {
910+
if i != 0 { try!(write!(f, ", ")); }
911+
try!(write!(f, "{:?}: {:?}", *k, *v));
912+
}
913+
914+
write!(f, "}}")
908915
}
909916
}
910917

branches/auto/src/libcollections/btree/set.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,14 @@ impl<'a, 'b, T: Ord + Clone> BitOr<&'b BTreeSet<T>> for &'a BTreeSet<T> {
628628
#[stable(feature = "rust1", since = "1.0.0")]
629629
impl<T: Debug> Debug for BTreeSet<T> {
630630
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
631-
self.iter().fold(f.debug_set(), |b, e| b.entry(e)).finish()
631+
try!(write!(f, "{{"));
632+
633+
for (i, x) in self.iter().enumerate() {
634+
if i != 0 { try!(write!(f, ", ")); }
635+
try!(write!(f, "{:?}", *x));
636+
}
637+
638+
write!(f, "}}")
632639
}
633640
}
634641

branches/auto/src/libcollections/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
#![feature(str_char)]
4141
#![feature(convert)]
4242
#![feature(slice_patterns)]
43-
#![feature(debug_builders)]
4443
#![cfg_attr(test, feature(rand, rustc_private, test, hash, collections))]
4544
#![cfg_attr(test, allow(deprecated))] // rand
4645

branches/auto/src/libcollections/linked_list.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,14 @@ impl<A: Clone> Clone for LinkedList<A> {
927927
#[stable(feature = "rust1", since = "1.0.0")]
928928
impl<A: fmt::Debug> fmt::Debug for LinkedList<A> {
929929
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
930-
self.iter().fold(f.debug_list(), |b, e| b.entry(e)).finish()
930+
try!(write!(f, "["));
931+
932+
for (i, e) in self.iter().enumerate() {
933+
if i != 0 { try!(write!(f, ", ")); }
934+
try!(write!(f, "{:?}", *e));
935+
}
936+
937+
write!(f, "]")
931938
}
932939
}
933940

branches/auto/src/libcollections/slice.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@
5050
//!
5151
//! ## Iteration
5252
//!
53-
//! The slices implement `IntoIterator`. The iterator yields references to the
54-
//! slice elements.
53+
//! The slices implement `IntoIterator`. The iterators of yield references
54+
//! to the slice elements.
5555
//!
5656
//! ```
5757
//! let numbers = &[0, 1, 2];

0 commit comments

Comments
 (0)