-
Notifications
You must be signed in to change notification settings - Fork 19
/
main.rs
33 lines (27 loc) · 1003 Bytes
/
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
// Write a program that uses an enum to represent different file types (Text, Binary, Image) and use pattern matching to process files based on their type.
enum FileType {
Text(String),
Binary(Vec<u8>),
Image { width: u32, height: u32 },
}
fn process_file(file: FileType) {
match file {
FileType::Text(content) => {
println!("Processing text file with content: {}", content);
}
FileType::Binary(data) => {
println!("Processing binary file with {} bytes.", data.len());
}
FileType::Image { width, height } => {
println!("Processing image with dimensions: {}x{}", width, height);
}
}
}
fn main() {
let text_file = FileType::Text(String::from("Hello, world!"));
let binary_file = FileType::Binary(vec![0xDE, 0xAD, 0xBE, 0xEF]);
let image_file = FileType::Image { width: 1920, height: 1080 };
process_file(text_file);
process_file(binary_file);
process_file(image_file);
}