File tree Expand file tree Collapse file tree 2 files changed +35
-2
lines changed Expand file tree Collapse file tree 2 files changed +35
-2
lines changed Original file line number Diff line number Diff line change 1
1
[package ]
2
- name = " chapter2"
3
- version = " 0.1.0"
4
2
authors = [
" yytyd <[email protected] >" ]
5
3
edition = " 2018"
4
+ name = " chapter2"
5
+ version = " 0.1.0"
6
6
7
7
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8
8
@@ -16,6 +16,10 @@ path = "src/2_4_1/main.rs"
16
16
name = " 2_4_2"
17
17
path = " src/2_4_2/main.rs"
18
18
19
+ [[bin ]]
20
+ name = " 2_4_3"
21
+ path = " src/2_4_3/main.rs"
22
+
19
23
[[bin ]]
20
24
name = " client_2_4_4"
21
25
path = " src/2_4_4/client.rs"
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments