Skip to content

Commit df255fb

Browse files
steveklabnikalexcrichton
authored andcommitted
---
yaml --- r: 153598 b: refs/heads/try2 c: fca79e4 h: refs/heads/master v: v3
1 parent 1f98bef commit df255fb

File tree

3 files changed

+135
-62
lines changed

3 files changed

+135
-62
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: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: 9cc39a054d485e46f7b8292cea690407932232ce
8+
refs/heads/try2: fca79e472689bb7f580a947e9268f4cabc2d298d
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/src/doc/guide.md

Lines changed: 131 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,25 +1143,31 @@ can only be _one_ of `Less`, `Equal`, or `Greater` at any given time. Here's
11431143
an example:
11441144

11451145
```rust
1146-
let x = 5i;
1147-
let y = 10i;
1146+
fn cmp(a: int, b: int) -> Ordering {
1147+
if a < b { Less }
1148+
else if a > b { Greater }
1149+
else { Equal }
1150+
}
1151+
1152+
fn main() {
1153+
let x = 5i;
1154+
let y = 10i;
11481155

1149-
let ordering = x.cmp(&y);
1156+
let ordering = cmp(x, y);
11501157

1151-
if ordering == Less {
1152-
println!("less");
1153-
} else if ordering == Greater {
1154-
println!("greater");
1155-
} else if ordering == Equal {
1156-
println!("equal");
1158+
if ordering == Less {
1159+
println!("less");
1160+
} else if ordering == Greater {
1161+
println!("greater");
1162+
} else if ordering == Equal {
1163+
println!("equal");
1164+
}
11571165
}
11581166
```
11591167

1160-
`cmp` is a function that compares two things, and returns an `Ordering`. The
1161-
call looks a little bit strange: rather than `cmp(x, y)`, we say `x.cmp(&y)`.
1162-
We haven't covered methods and references yet, so it should look a little bit
1163-
foreign. Right now, just pretend it says `cmp(x, y)`, and we'll get to those
1164-
details soon.
1168+
`cmp` is a function that compares two things, and returns an `Ordering`. We
1169+
return either `Less`, `Greater`, or `Equal`, depending on if the two values
1170+
are greater, less, or equal.
11651171

11661172
The `ordering` variable has the type `Ordering`, and so contains one of the
11671173
three values. We can then do a bunch of `if`/`else` comparisons to check
@@ -1172,12 +1178,12 @@ that not only makes them nicer to read, but also makes sure that you never
11721178
miss a case. Before we get to that, though, let's talk about another kind of
11731179
enum: one with values.
11741180

1175-
This enum has two variants, one of which has a value.:
1181+
This enum has two variants, one of which has a value:
11761182

1177-
```
1183+
```{rust}
11781184
enum OptionalInt {
11791185
Value(int),
1180-
Missing
1186+
Missing,
11811187
}
11821188
11831189
fn main() {
@@ -1261,30 +1267,46 @@ for every possible value of `x`, and so our program will now compile.
12611267
section on enums?
12621268

12631269
```{rust}
1264-
let x = 5i;
1265-
let y = 10i;
1270+
fn cmp(a: int, b: int) -> Ordering {
1271+
if a < b { Less }
1272+
else if a > b { Greater }
1273+
else { Equal }
1274+
}
1275+
1276+
fn main() {
1277+
let x = 5i;
1278+
let y = 10i;
12661279
1267-
let ordering = x.cmp(&y);
1280+
let ordering = cmp(x, y);
12681281
1269-
if ordering == Less {
1270-
println!("less");
1271-
} else if ordering == Greater {
1272-
println!("greater");
1273-
} else if ordering == Equal {
1274-
println!("equal");
1282+
if ordering == Less {
1283+
println!("less");
1284+
} else if ordering == Greater {
1285+
println!("greater");
1286+
} else if ordering == Equal {
1287+
println!("equal");
1288+
}
12751289
}
12761290
```
12771291

12781292
We can re-write this as a `match`:
12791293

12801294
```{rust}
1281-
let x = 5i;
1282-
let y = 10i;
1295+
fn cmp(a: int, b: int) -> Ordering {
1296+
if a < b { Less }
1297+
else if a > b { Greater }
1298+
else { Equal }
1299+
}
12831300
1284-
match x.cmp(&y) {
1285-
Less => println!("less"),
1286-
Greater => println!("greater"),
1287-
Equal => println!("equal"),
1301+
fn main() {
1302+
let x = 5i;
1303+
let y = 10i;
1304+
1305+
match cmp(x, y) {
1306+
Less => println!("less"),
1307+
Greater => println!("greater"),
1308+
Equal => println!("equal"),
1309+
}
12881310
}
12891311
```
12901312

@@ -1297,17 +1319,25 @@ make sure to cover all of our bases.
12971319
`match` is also an expression, which means we can use it on the right hand side
12981320
of a `let` binding. We could also implement the previous line like this:
12991321

1300-
```
1301-
let x = 5i;
1302-
let y = 10i;
1322+
```{rust}
1323+
fn cmp(a: int, b: int) -> Ordering {
1324+
if a < b { Less }
1325+
else if a > b { Greater }
1326+
else { Equal }
1327+
}
13031328
1304-
let result = match x.cmp(&y) {
1305-
Less => "less",
1306-
Greater => "greater",
1307-
Equal => "equal",
1308-
};
1329+
fn main() {
1330+
let x = 5i;
1331+
let y = 10i;
13091332
1310-
println!("{}", result);
1333+
let result = match cmp(x, y) {
1334+
Less => "less",
1335+
Greater => "greater",
1336+
Equal => "equal",
1337+
};
1338+
1339+
println!("{}", result);
1340+
}
13111341
```
13121342

13131343
In this case, it doesn't make a lot of sense, as we are just making a temporary
@@ -1527,16 +1557,68 @@ a full line of input. Nice and easy.
15271557
.ok().expect("Failed to read line");
15281558
```
15291559

1530-
Here's the thing: reading a line from standard input could fail. For example,
1531-
if this program isn't running in a terminal, but is running as part of a cron
1532-
job, or some other context where there's no standard input. So Rust expects us
1533-
to handle this case. Given that we plan on always running this program in a
1534-
terminal, we use the `ok()` method to tell Rust that we're expecting everything
1535-
to be just peachy, and the `expect()` method on that result to give an error
1536-
message if our expectation goes wrong.
1560+
Do you remember this code?
1561+
1562+
```
1563+
enum OptionalInt {
1564+
Value(int),
1565+
Missing,
1566+
}
1567+
1568+
fn main() {
1569+
let x = Value(5);
1570+
let y = Missing;
1571+
1572+
match x {
1573+
Value(n) => println!("x is {:d}", n),
1574+
Missing => println!("x is missing!"),
1575+
}
1576+
1577+
match y {
1578+
Value(n) => println!("y is {:d}", n),
1579+
Missing => println!("y is missing!"),
1580+
}
1581+
}
1582+
```
1583+
1584+
We had to match each time, to see if we had a value or not. In this case,
1585+
though, we _know_ that `x` has a `Value`. But `match` forces us to handle
1586+
the `missing` case. This is what we want 99% of the time, but sometimes, we
1587+
know better than the compiler.
1588+
1589+
Likewise, `read_line()` does not return a line of input. It _might_ return a
1590+
line of input. It might also fail to do so. This could happen if our program
1591+
isn't running in a terminal, but as part of a cron job, or some other context
1592+
where there's no standard input. Because of this, `read_line` returns a type
1593+
very similar to our `OptionalInt`: an `IoResult<T>`. We haven't talked about
1594+
`IoResult<T>` yet because it is the **generic** form of our `OptionalInt`.
1595+
Until then, you can think of it as being the same thing, just for any type, not
1596+
just `int`s.
1597+
1598+
Rust provides a method on these `IoResult<T>`s called `ok()`, which does the
1599+
same thing as our `match` statement, but assuming that we have a valid value.
1600+
If we don't, it will terminate our program. In this case, if we can't get
1601+
input, our program doesn't work, so we're okay with that. In most cases, we
1602+
would want to handle the error case explicitly. The result of `ok()` has a
1603+
method, `expect()`, which allows us to give an error message if this crash
1604+
happens.
15371605

15381606
We will cover the exact details of how all of this works later in the Guide.
1539-
For now, this is all you need.
1607+
For now, this gives you enough of a basic understanding to work with.
1608+
1609+
Back to the code we were working on! Here's a refresher:
1610+
1611+
```{rust,ignore}
1612+
use std::io;
1613+
1614+
fn main() {
1615+
println!("Type something!");
1616+
1617+
let input = io::stdin().read_line().ok().expect("Failed to read line");
1618+
1619+
println!("{}", input);
1620+
}
1621+
```
15401622

15411623
With long lines like this, Rust gives you some flexibility with the whitespace.
15421624
We _could_ write the example like this:

branches/try2/src/etc/vim/syntax/rust.vim

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
" Maintainer: Patrick Walton <[email protected]>
44
" Maintainer: Ben Blum <[email protected]>
55
" Maintainer: Chris Morgan <[email protected]>
6-
" Last Change: July 18, 2014
6+
" Last Change: July 06, 2014
77

88
if version < 600
99
syntax clear
@@ -38,7 +38,7 @@ syn keyword rustKeyword for in if impl let
3838
syn keyword rustKeyword loop once proc pub
3939
syn keyword rustKeyword return super
4040
syn keyword rustKeyword unsafe virtual while
41-
syn keyword rustKeyword use nextgroup=rustModPath,rustModPathInUse skipwhite skipempty
41+
syn keyword rustKeyword use nextgroup=rustModPath skipwhite skipempty
4242
" FIXME: Scoped impl's name is also fallen in this category
4343
syn keyword rustKeyword mod trait struct enum type nextgroup=rustIdentifier skipwhite skipempty
4444
syn keyword rustStorage mut ref static const
@@ -60,10 +60,6 @@ syn region rustBoxPlacementBalance start="(" end=")" containedin=rustBoxPlace
6060
syn region rustBoxPlacementBalance start="\[" end="\]" containedin=rustBoxPlacement transparent
6161
" {} are handled by rustFoldBraces
6262

63-
syn region rustMacroRepeat matchgroup=rustMacroRepeatDelimiters start="$(" end=")" contains=TOP nextgroup=rustMacroRepeatCount
64-
syn match rustMacroRepeatCount ".\?[*+]" contained
65-
syn match rustMacroVariable "$\w\+"
66-
6763
" Reserved (but not yet used) keywords {{{2
6864
syn keyword rustReservedKeyword alignof be do offsetof priv pure sizeof typeof unsized yield
6965

@@ -142,9 +138,8 @@ syn keyword rustBoolean true false
142138
" If foo::bar changes to foo.bar, change this ("::" to "\.").
143139
" If foo::bar changes to Foo::bar, change this (first "\w" to "\u").
144140
syn match rustModPath "\w\(\w\)*::[^<]"he=e-3,me=e-3
145-
syn match rustModPathInUse "\w\(\w\)*" contained " only for 'use path;'
141+
syn match rustModPath "\w\(\w\)*" contained " only for 'use path;'
146142
syn match rustModPathSep "::"
147-
" rustModPathInUse is split out from rustModPath so that :syn-include can get the group list right.
148143

149144
syn match rustFuncCall "\w\(\w\)*("he=e-1,me=e-1
150145
syn match rustFuncCall "\w\(\w\)*::<"he=e-3,me=e-3 " foo::<T>();
@@ -238,9 +233,6 @@ hi def link rustBinNumber rustNumber
238233
hi def link rustIdentifierPrime rustIdentifier
239234
hi def link rustTrait rustType
240235

241-
hi def link rustMacroRepeatCount rustMacroRepeatDelimiters
242-
hi def link rustMacroRepeatDelimiters Macro
243-
hi def link rustMacroVariable Define
244236
hi def link rustSigil StorageClass
245237
hi def link rustEscape Special
246238
hi def link rustEscapeUnicode rustEscape
@@ -263,7 +255,6 @@ hi def link rustReservedKeyword Error
263255
hi def link rustConditional Conditional
264256
hi def link rustIdentifier Identifier
265257
hi def link rustCapsIdent rustIdentifier
266-
hi def link rustModPathInUse rustModPath
267258
hi def link rustModPath Include
268259
hi def link rustModPathSep Delimiter
269260
hi def link rustFunction Function

0 commit comments

Comments
 (0)