File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ [package ]
2+ name = " exercise"
3+ version = " 0.1.0"
4+ authors = [" Knight <Knight42@mail.ustc.edu.cn>" ]
5+
6+ [dependencies ]
Original file line number Diff line number Diff line change 1+ README
2+ =======
3+
4+ ` cargo run `
Original file line number Diff line number Diff line change 1+ mod problem1;
2+
3+ fn main ( ) {
4+ problem1:: demo ( "Cargo.toml" ) ;
5+ problem1:: demo ( "" ) ;
6+ }
Original file line number Diff line number Diff line change 1+ use std:: borrow:: Borrow ;
2+ use std:: fs:: File ;
3+ use std:: io:: { BufRead , BufReader } ;
4+
5+ fn parse < I > ( lines : I )
6+ where I : IntoIterator , I :: Item : Borrow < str >
7+ {
8+ for line in lines {
9+ println ! ( "{}" , line. borrow( ) ) ;
10+ }
11+ }
12+
13+ pub fn demo ( path : & str ) {
14+ if path. is_empty ( ) {
15+ println ! ( "==== Parsing long string ====" ) ;
16+ let content = "some\n long\n text" ;
17+ parse ( content. lines ( ) ) ;
18+ } else {
19+ println ! ( "==== Parsing text file ====" ) ;
20+ let f = File :: open ( path) . unwrap ( ) ;
21+ let b = BufReader :: new ( f) ;
22+ parse ( b. lines ( ) . map ( |l| l. unwrap_or ( "" . to_owned ( ) ) ) ) ;
23+ }
24+ }
You can’t perform that action at this time.
0 commit comments