@@ -1143,25 +1143,31 @@ can only be _one_ of `Less`, `Equal`, or `Greater` at any given time. Here's
1143
1143
an example:
1144
1144
1145
1145
``` 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 ;
1148
1155
1149
- let ordering = x . cmp (& y );
1156
+ let ordering = cmp (x , y );
1150
1157
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
+ }
1157
1165
}
1158
1166
```
1159
1167
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.
1165
1171
1166
1172
The ` ordering ` variable has the type ` Ordering ` , and so contains one of the
1167
1173
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
1172
1178
miss a case. Before we get to that, though, let's talk about another kind of
1173
1179
enum: one with values.
1174
1180
1175
- This enum has two variants, one of which has a value. :
1181
+ This enum has two variants, one of which has a value:
1176
1182
1177
- ```
1183
+ ``` {rust}
1178
1184
enum OptionalInt {
1179
1185
Value(int),
1180
- Missing
1186
+ Missing,
1181
1187
}
1182
1188
1183
1189
fn main() {
@@ -1261,30 +1267,46 @@ for every possible value of `x`, and so our program will now compile.
1261
1267
section on enums?
1262
1268
1263
1269
``` {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;
1266
1279
1267
- let ordering = x. cmp(& y);
1280
+ let ordering = cmp(x, y);
1268
1281
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
+ }
1275
1289
}
1276
1290
```
1277
1291
1278
1292
We can re-write this as a ` match ` :
1279
1293
1280
1294
``` {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
+ }
1283
1300
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
+ }
1288
1310
}
1289
1311
```
1290
1312
@@ -1297,17 +1319,25 @@ make sure to cover all of our bases.
1297
1319
` match ` is also an expression, which means we can use it on the right hand side
1298
1320
of a ` let ` binding. We could also implement the previous line like this:
1299
1321
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
+ }
1303
1328
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;
1309
1332
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
+ }
1311
1341
```
1312
1342
1313
1343
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.
1527
1557
.ok().expect("Failed to read line");
1528
1558
```
1529
1559
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.
1537
1605
1538
1606
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
+ ```
1540
1622
1541
1623
With long lines like this, Rust gives you some flexibility with the whitespace.
1542
1624
We _ could_ write the example like this:
0 commit comments