-
Notifications
You must be signed in to change notification settings - Fork 29
/
main.rs
60 lines (45 loc) · 1.27 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use std::fmt;
#[derive(Debug)]
struct MyError {
message: String,
}
impl fmt::Display for MyError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "MyError: {}", self.message)
}
}
fn risky_operation() -> Result<(), MyError> {
if true { // 假定这里是一些业务逻辑的结果
Err(MyError { message: String::from("Something went wrong") })
} else {
Ok(())
}
}
fn divide(dividend: f64, divisor: f64) -> Result<f64, &'static str> {
if divisor == 0.0 {
Err("Cannot divide by zero")
} else {
Ok(dividend / divisor)
}
}
fn get_data_from_file() -> Result<String, std::io::Error> {
let path = "data.txt";
let content = std::fs::read_to_string(path)?;
Ok(content)
}
fn main() {
let result = divide(10.0, 0.0);
match result {
Ok(value) => println!("Result: {}", value),
Err(e) => println!("Error: {}", e),
}
match get_data_from_file() {
Ok(data) => println!("File content: {}", data),
Err(e) => println!("Failed to read file: {}", e),
}
match risky_operation() {
Ok(data) => println!("File content: {:?}", data),
Err(e) => println!("Failed to read file: {}", e),
}
panic!("This is a critical error!");
}