2023-11-29 10:39:24 -05:00
|
|
|
---
|
|
|
|
minutes: 10
|
|
|
|
---
|
|
|
|
|
2022-12-21 16:36:30 +01:00
|
|
|
# `Read` and `Write`
|
|
|
|
|
2023-02-09 21:47:47 +00:00
|
|
|
Using [`Read`][1] and [`BufRead`][2], you can abstract over `u8` sources:
|
2022-12-21 16:36:30 +01:00
|
|
|
|
|
|
|
```rust,editable
|
|
|
|
use std::io::{BufRead, BufReader, Read, Result};
|
|
|
|
|
|
|
|
fn count_lines<R: Read>(reader: R) -> usize {
|
|
|
|
let buf_reader = BufReader::new(reader);
|
|
|
|
buf_reader.lines().count()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() -> Result<()> {
|
|
|
|
let slice: &[u8] = b"foo\nbar\nbaz\n";
|
|
|
|
println!("lines in slice: {}", count_lines(slice));
|
|
|
|
|
|
|
|
let file = std::fs::File::open(std::env::current_exe()?)?;
|
|
|
|
println!("lines in file: {}", count_lines(file));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2023-02-09 21:47:47 +00:00
|
|
|
Similarly, [`Write`][3] lets you abstract over `u8` sinks:
|
2022-12-21 16:36:30 +01:00
|
|
|
|
|
|
|
```rust,editable
|
|
|
|
use std::io::{Result, Write};
|
|
|
|
|
|
|
|
fn log<W: Write>(writer: &mut W, msg: &str) -> Result<()> {
|
|
|
|
writer.write_all(msg.as_bytes())?;
|
|
|
|
writer.write_all("\n".as_bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() -> Result<()> {
|
2023-01-06 09:47:42 -07:00
|
|
|
let mut buffer = Vec::new();
|
2022-12-21 16:36:30 +01:00
|
|
|
log(&mut buffer, "Hello")?;
|
|
|
|
log(&mut buffer, "World")?;
|
|
|
|
println!("Logged: {:?}", buffer);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
```
|
2023-02-09 21:47:47 +00:00
|
|
|
|
|
|
|
[1]: https://doc.rust-lang.org/std/io/trait.Read.html
|
|
|
|
[2]: https://doc.rust-lang.org/std/io/trait.BufRead.html
|
|
|
|
[3]: https://doc.rust-lang.org/std/io/trait.Write.html
|