Skip to content

Commit 73a5430

Browse files
KnightKnight
authored andcommitted
Add solution
1 parent 0811622 commit 73a5430

4 files changed

Lines changed: 40 additions & 0 deletions

File tree

generic/exercise/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[package]
2+
name = "exercise"
3+
version = "0.1.0"
4+
authors = ["Knight <Knight42@mail.ustc.edu.cn>"]
5+
6+
[dependencies]

generic/exercise/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
README
2+
=======
3+
4+
`cargo run`

generic/exercise/src/main.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
mod problem1;
2+
3+
fn main() {
4+
problem1::demo("Cargo.toml");
5+
problem1::demo("");
6+
}

generic/exercise/src/problem1.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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\nlong\ntext";
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+
}

0 commit comments

Comments
 (0)