Skip to content

Commit e74bf93

Browse files
authored
Merge pull request #23 from shnmorimoto/2_4_3
added 2_4_3
2 parents 69c105e + 7ffdd82 commit e74bf93

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

chapter2/Cargo.toml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
[package]
2-
name = "chapter2"
3-
version = "0.1.0"
42
authors = ["yytyd <[email protected]>"]
53
edition = "2018"
4+
name = "chapter2"
5+
version = "0.1.0"
66

77
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
88

@@ -16,6 +16,10 @@ path = "src/2_4_1/main.rs"
1616
name = "2_4_2"
1717
path = "src/2_4_2/main.rs"
1818

19+
[[bin]]
20+
name = "2_4_3"
21+
path = "src/2_4_3/main.rs"
22+
1923
[[bin]]
2024
name = "client_2_4_4"
2125
path = "src/2_4_4/client.rs"

chapter2/src/2_4_3/main.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use std::io::Write;
2+
use std::str;
3+
4+
#[derive(Debug)]
5+
enum BufferError {
6+
IOError(std::io::Error),
7+
StrError(str::Utf8Error),
8+
}
9+
10+
impl From<std::io::Error> for BufferError {
11+
fn from(error: std::io::Error) -> Self {
12+
BufferError::IOError(error)
13+
}
14+
}
15+
16+
impl From<str::Utf8Error> for BufferError {
17+
fn from(error: str::Utf8Error) -> Self {
18+
BufferError::StrError(error)
19+
}
20+
}
21+
22+
fn main() -> Result<(), BufferError> {
23+
let mut buffer: Vec<u8> = Vec::new();
24+
buffer.write(b"bytes.Buffer example1\n")?;
25+
println!("{}", str::from_utf8(&buffer)?);
26+
buffer.write(b"bytes.Buffer example2\n")?;
27+
println!("{}", str::from_utf8(&buffer)?);
28+
Ok(())
29+
}

0 commit comments

Comments
 (0)