@@ -43,12 +43,12 @@ $ ./example numbers.txt
43
43
44
44
An example program that does this task reads like this:
45
45
46
- ~~~~ {.xfail-test}
46
+ ~~~~
47
47
# #[allow(unused_imports)];
48
48
use std::io::buffered::BufferedReader;
49
- use std::io::fs:: File;
49
+ use std::io::File;
50
50
# mod BufferedReader {
51
- # use std::io::fs:: File;
51
+ # use std::io::File;
52
52
# use std::io::mem::MemReader;
53
53
# use std::io::buffered::BufferedReader;
54
54
# static s : &'static [u8] = bytes!("1 2\n\
@@ -71,10 +71,9 @@ fn main() {
71
71
fn read_int_pairs() -> ~[(int,int)] {
72
72
let mut pairs = ~[];
73
73
74
- let args = std::os::args();
75
-
76
74
// Path takes a generic by-value, rather than by reference
77
- let path = Path::new(args.get_opt(1).expect("No input file parameter!").as_slice());
75
+ # let _g = std::io::ignore_io_error();
76
+ let path = Path::new(&"foo.txt");
78
77
let mut reader = BufferedReader::new(File::open(&path));
79
78
80
79
// 1. Iterate over the lines of our file.
@@ -242,13 +241,13 @@ If the example is rewritten to use failure, these error cases can be trapped.
242
241
In this rewriting, failures are trapped by placing the I/O logic in a sub-task,
243
242
and trapping its exit status using ` task::try ` :
244
243
245
- ~~~~ {.xfail-test}
244
+ ~~~~
246
245
# #[allow(unused_imports)];
247
246
use std::io::buffered::BufferedReader;
248
- use std::io::fs:: File;
247
+ use std::io::File;
249
248
use std::task;
250
249
# mod BufferedReader {
251
- # use std::io::fs:: File;
250
+ # use std::io::File;
252
251
# use std::io::mem::MemReader;
253
252
# use std::io::buffered::BufferedReader;
254
253
# static s : &'static [u8] = bytes!("1 2\n\
@@ -280,8 +279,8 @@ fn main() {
280
279
281
280
fn read_int_pairs() -> ~[(int,int)] {
282
281
let mut pairs = ~[];
283
- let args = std::os::args ();
284
- let path = Path::new(args.get_opt(1).expect("No input file parameter!").as_slice() );
282
+ # let _g = std::io::ignore_io_error ();
283
+ let path = Path::new(&"foo.txt" );
285
284
286
285
let mut reader = BufferedReader::new(File::open(&path));
287
286
for line in reader.lines() {
@@ -346,12 +345,12 @@ If no handler is found, `Condition::raise` will fail the task with an appropriat
346
345
Rewriting the example to use a condition in place of ignoring malformed lines makes it slightly longer,
347
346
but similarly clear as the version that used ` fail! ` in the logic where the error occurs:
348
347
349
- ~~~~ {.xfail-test}
348
+ ~~~~
350
349
# #[allow(unused_imports)];
351
350
use std::io::buffered::BufferedReader;
352
- use std::io::fs:: File;
351
+ use std::io::File;
353
352
# mod BufferedReader {
354
- # use std::io::fs:: File;
353
+ # use std::io::File;
355
354
# use std::io::mem::MemReader;
356
355
# use std::io::buffered::BufferedReader;
357
356
# static s : &'static [u8] = bytes!("1 2\n\
@@ -378,8 +377,8 @@ fn main() {
378
377
379
378
fn read_int_pairs() -> ~[(int,int)] {
380
379
let mut pairs = ~[];
381
- let args = std::os::args ();
382
- let path = Path::new(args.get_opt(1).expect("No input file parameter!").as_slice() );
380
+ # let _g = std::io::ignore_io_error ();
381
+ let path = Path::new(&"foo.txt" );
383
382
384
383
let mut reader = BufferedReader::new(File::open(&path));
385
384
for line in reader.lines() {
@@ -415,12 +414,12 @@ To trap a condition, use `Condition::trap` in some caller of the site that calls
415
414
For example, this version of the program traps the ` malformed_line ` condition
416
415
and replaces bad input lines with the pair ` (-1,-1) ` :
417
416
418
- ~~~~ {.xfail-test}
417
+ ~~~~
419
418
# #[allow(unused_imports)];
420
419
use std::io::buffered::BufferedReader;
421
- use std::io::fs:: File;
420
+ use std::io::File;
422
421
# mod BufferedReader {
423
- # use std::io::fs:: File;
422
+ # use std::io::File;
424
423
# use std::io::mem::MemReader;
425
424
# use std::io::buffered::BufferedReader;
426
425
# static s : &'static [u8] = bytes!("1 2\n\
@@ -452,8 +451,8 @@ fn main() {
452
451
453
452
fn read_int_pairs() -> ~[(int,int)] {
454
453
let mut pairs = ~[];
455
- let args = std::os::args ();
456
- let path = Path::new(args.get_opt(1).expect("No input file parameter!").as_slice() );
454
+ # let _g = std::io::ignore_io_error ();
455
+ let path = Path::new(&"foo.txt" );
457
456
458
457
let mut reader = BufferedReader::new(File::open(&path));
459
458
for line in reader.lines() {
@@ -490,12 +489,12 @@ In the example program, the first form of the `malformed_line` API implicitly as
490
489
This assumption may not be correct; some callers may wish to skip malformed lines, for example.
491
490
Changing the condition's return type from ` (int,int) ` to ` Option<(int,int)> ` will suffice to support this type of recovery:
492
491
493
- ~~~~ {.xfail-test}
492
+ ~~~~
494
493
# #[allow(unused_imports)];
495
494
use std::io::buffered::BufferedReader;
496
- use std::io::fs:: File;
495
+ use std::io::File;
497
496
# mod BufferedReader {
498
- # use std::io::fs:: File;
497
+ # use std::io::File;
499
498
# use std::io::mem::MemReader;
500
499
# use std::io::buffered::BufferedReader;
501
500
# static s : &'static [u8] = bytes!("1 2\n\
@@ -528,8 +527,8 @@ fn main() {
528
527
529
528
fn read_int_pairs() -> ~[(int,int)] {
530
529
let mut pairs = ~[];
531
- let args = std::os::args ();
532
- let path = Path::new(args.get_opt(1).expect("No input file parameter!").as_slice() );
530
+ # let _g = std::io::ignore_io_error ();
531
+ let path = Path::new(&"foo.txt" );
533
532
534
533
let mut reader = BufferedReader::new(File::open(&path));
535
534
for line in reader.lines() {
@@ -575,12 +574,12 @@ until all relevant combinations encountered in practice are encoded.
575
574
In the example, suppose a third possible recovery form arose: reusing the previous value read.
576
575
This can be encoded in the handler API by introducing a helper type: ` enum MalformedLineFix ` .
577
576
578
- ~~~~ {.xfail-test}
577
+ ~~~~
579
578
# #[allow(unused_imports)];
580
579
use std::io::buffered::BufferedReader;
581
- use std::io::fs:: File;
580
+ use std::io::File;
582
581
# mod BufferedReader {
583
- # use std::io::fs:: File;
582
+ # use std::io::File;
584
583
# use std::io::mem::MemReader;
585
584
# use std::io::buffered::BufferedReader;
586
585
# static s : &'static [u8] = bytes!("1 2\n\
@@ -622,8 +621,8 @@ fn main() {
622
621
623
622
fn read_int_pairs() -> ~[(int,int)] {
624
623
let mut pairs = ~[];
625
- let args = std::os::args ();
626
- let path = Path::new(args.get_opt(1).expect("No input file parameter!").as_slice() );
624
+ # let _g = std::io::ignore_io_error ();
625
+ let path = Path::new(&"foo.txt" );
627
626
628
627
let mut reader = BufferedReader::new(File::open(&path));
629
628
for line in reader.lines() {
@@ -699,12 +698,12 @@ task <unnamed> failed at 'called `Option::unwrap()` on a `None` value', .../libs
699
698
To make the program robust &mdash ; or at least flexible &mdash ; in the face of this potential failure,
700
699
a second condition and a helper function will suffice:
701
700
702
- ~~~~ {.xfail-test}
701
+ ~~~~
703
702
# #[allow(unused_imports)];
704
703
use std::io::buffered::BufferedReader;
705
- use std::io::fs:: File;
704
+ use std::io::File;
706
705
# mod BufferedReader {
707
- # use std::io::fs:: File;
706
+ # use std::io::File;
708
707
# use std::io::mem::MemReader;
709
708
# use std::io::buffered::BufferedReader;
710
709
# static s : &'static [u8] = bytes!("1 2\n\
@@ -760,8 +759,8 @@ fn parse_int(x: &str) -> int {
760
759
761
760
fn read_int_pairs() -> ~[(int,int)] {
762
761
let mut pairs = ~[];
763
- let args = std::os::args ();
764
- let path = Path::new(args.get_opt(1).expect("No input file parameter!").as_slice() );
762
+ # let _g = std::io::ignore_io_error ();
763
+ let path = Path::new(&"foo.txt" );
765
764
766
765
let mut reader = BufferedReader::new(File::open(&path));
767
766
for line in reader.lines() {
0 commit comments