Skip to content

Commit 41307ca

Browse files
committed
Added pipe based usages.
1 parent 86bad7a commit 41307ca

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

fops/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::io_ops::*;
55
mod data;
66
mod game;
77
mod io_ops;
8+
mod std_ops;
89

910
fn main() -> std::io::Result<()> {
1011
let mut classic_games = vec![

fops/src/std_ops.rs

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
use std::io::{self, BufRead, Write};
2+
3+
fn write_to_file() -> io::Result<()> {
4+
let mut input = String::new();
5+
println!("Please enter some text:");
6+
7+
io::stdin().read_line(&mut input)?;
8+
println!("Your text is: {}", input.trim());
9+
10+
Ok(())
11+
}
12+
13+
fn sum() -> io::Result<i32> {
14+
let mut input1 = String::new();
15+
let mut input2 = String::new();
16+
17+
println!("Please enter the first number:");
18+
io::stdin().read_line(&mut input1)?;
19+
20+
println!("Second number:");
21+
io::stdin().read_line(&mut input2)?;
22+
23+
let x: i32 = input1.trim().parse().expect("Please enter a number!");
24+
let y: i32 = input2.trim().parse().expect("Please enter a number!");
25+
26+
Ok(x + y)
27+
}
28+
29+
fn read_loop() -> io::Result<()> {
30+
let stdin = io::stdin();
31+
let reader = stdin.lock();
32+
33+
println!("Please enter some text (Ctrl+Z for exit):");
34+
for line in reader.lines() {
35+
let line = line?;
36+
println!("Input: {}", line);
37+
}
38+
39+
Ok(())
40+
}
41+
42+
/*
43+
Çalıştırmak için;
44+
45+
cat logs.dat | cargo run
46+
*/
47+
fn read_from_pipe() -> io::Result<()> {
48+
let stdin = io::stdin();
49+
let reader = stdin.lock();
50+
51+
println!("Data is retrieving...");
52+
for line in reader.lines() {
53+
let line = line?;
54+
println!("Data: {}", line);
55+
}
56+
57+
Ok(())
58+
}
59+
60+
/*
61+
Çalıştırmak için;
62+
63+
cargo run > logs.txt
64+
*/
65+
fn write_log() -> io::Result<()> {
66+
let stdout = io::stdout();
67+
let mut handle = stdout.lock();
68+
69+
writeln!(handle, "This will be written to a file.")?;
70+
71+
Ok(())
72+
}
73+
74+
/*
75+
Çalıştırmak için;
76+
77+
cat logs.dat | cargo run > output_logs.txt
78+
*/
79+
fn double_usage() -> io::Result<()> {
80+
let stdin = io::stdin();
81+
let stdout = io::stdout();
82+
83+
let reader = stdin.lock();
84+
let mut writer = stdout.lock();
85+
86+
for line in reader.lines() {
87+
let line = line?;
88+
writeln!(writer, "Data received {}", line)?;
89+
}
90+
Ok(())
91+
}
92+
93+
pub fn run() -> io::Result<()> {
94+
// write_to_file()?;
95+
//
96+
// let total = sum()?;
97+
// println!("Total: {}", total);
98+
//
99+
// read_loop()?;
100+
101+
// read_from_pipe()?;
102+
103+
// write_log()?;
104+
105+
double_usage()?;
106+
107+
Ok(())
108+
}

0 commit comments

Comments
 (0)