1
+ use std:: fs;
2
+ use std:: fs:: File ;
3
+ use std:: io:: { self , Read } ;
4
+
5
+ pub fn main ( ) {
6
+ // We use unwrap() here to use the String inside the Result our function returned. If an error happened it will panic accordingly.
7
+ // V
8
+ let readme_contents = read_file ( "README.md" ) . unwrap ( ) ;
9
+ println ! ( "The contents of the README are:\n {readme_contents}" ) ;
10
+
11
+
12
+ // Exact same thing but cool
13
+ let readme_contents = read_file_cool ( "README.md" ) . unwrap ( ) ;
14
+ println ! ( "The contents of the README are:\n {readme_contents}" ) ;
15
+
16
+
17
+ // Exact same thing, but even cooler
18
+ let readme_contents = read_file_quick ( "README.md" ) . unwrap ( ) ;
19
+ println ! ( "The contents of the README are:\n {readme_contents}" ) ;
20
+
21
+
22
+ let really_green = Color :: new ( 0 , 255 , 0 ) ;
23
+
24
+ println ! ( "Really green G: {}" , really_green. getG( ) ) ;
25
+ }
26
+
27
+
28
+ // This returns a Result<a,b> a is the content of the file read, and b is a possible error.
29
+ fn read_file ( file : & str ) -> Result < String , io:: Error > {
30
+ // First read the file.
31
+ let result = File :: open ( file) ;
32
+
33
+ // Then handle any errors that might have occurred
34
+ let mut file = match result {
35
+ Ok ( file) => file, // if no errors occurred store the file in file
36
+ Err ( e) => return Err ( e)
37
+ } ;
38
+
39
+ // in here we store the contents of the file we just read
40
+ let mut contents = String :: new ( ) ;
41
+
42
+ // attempt to read the file. If errors happen, we handle that
43
+ match file. read_to_string ( & mut contents) {
44
+ Ok ( _) => Ok ( contents) , // if no errors occurred we write it.
45
+ Err ( e) => Err ( e) // else return the error
46
+ }
47
+ }
48
+
49
+
50
+ // This is the same, but shorter
51
+ fn read_file_cool ( file : & str ) -> Result < String , io:: Error > {
52
+ // first read the file. The ? means, do the operation, and if something happens return an error(Err(e)) and else return the result(Ok(r).
53
+ let mut file = File :: open ( file) ?; // ?
54
+
55
+ // store contents here
56
+ let mut contents = String :: new ( ) ;
57
+
58
+ // write the data in contents. Errors are handled same as first line.
59
+ file. read_to_string ( & mut contents) ?; // ?
60
+
61
+ // Then return the contents within an Ok(). This will work 100& because we have already outruled any other errors.
62
+ Ok ( contents)
63
+ }
64
+
65
+
66
+ // SHORTER
67
+ fn read_file_cooler ( file : & str ) -> Result < String , io:: Error > {
68
+ // Init contents var
69
+ let mut contents = String :: new ( ) ;
70
+
71
+ // read file
72
+ File :: open ( file) ? // <- handle errors
73
+ // read contents
74
+ . read_to_string (
75
+ & mut contents // write contents
76
+ ) ?; // <- handle errors
77
+
78
+ Ok ( contents) // <- If no errors occured return the contents within an Ok()
79
+ }
80
+
81
+ // SHOERR+RROURURPUYGIOP
82
+ fn read_file_quick ( file : & str ) -> Result < String , io:: Error > {
83
+ // This litterly does everything we just made in one function.
84
+ fs:: read_to_string ( file)
85
+ // Looking at the source code of fs::read_to_string() this looks more reliable to use.
86
+ }
87
+
88
+
89
+ pub struct Color {
90
+ r : u16 ,
91
+ g : u16 ,
92
+ b : u16 ,
93
+ }
94
+
95
+ impl Color {
96
+ pub fn new ( r : u16 , g : u16 , b : u16 ) -> Color {
97
+ if r > 255 || g > 255 || b > 255 {
98
+ panic ! ( "A color can't be more than 255!" )
99
+ }
100
+ Color { r, g, b }
101
+ }
102
+
103
+ pub fn getR ( & self ) -> u16 {
104
+ self . r
105
+ }
106
+ pub fn getG ( & self ) -> u16 {
107
+ self . g
108
+ }
109
+ pub fn getB ( & self ) -> u16 {
110
+ self . b
111
+ }
112
+ }
0 commit comments