COMP3007_Modern_Programming_Languages (4)
COMP3007_Modern_Programming_Languages (4)
Fall 2024-2025
1 fn main () {
2 panic !("crash and burn");
3 }
1 use std::io;
2 use std::io:: Read;
3 use std::fs:: File;
4
5 fn read_username_from_file () -> Result <String , io::Error > {
6 let f = File :: open("hello.txt");
7
8 let mut f = match f {
9 Ok(file) => file ,
10 Err(e) => return Err(e),
11 };
12
13 let mut s = String ::new();
14
15 match f.read_to_string (& mut s) {
16 Ok(_) => Ok(s),
17 Err(e) => Err(e),
18 }
19 }
1 use std::io;
2 use std::io:: Read;
3 use std::fs:: File;
4
5 fn read_username_from_file () -> Result <String , io::Error > {
6 let mut f = File :: open("hello.txt")?;
7 let mut s = String ::new();
8 f.read_to_string (& mut s)?;
9 Ok(s)
10 }