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