본문 바로가기

Rust61

[Rust] Generic 예제 (22일차) // A concrete type 'A' struct A; // In defining the type 'Single', the first use of 'A' is not preceded by '' // Therefore, 'Single' is a concrete type, and 'A' is definded as above. struct Single(A); // ^ Here is 'Single's first use of the type 'A'. // Here, '' precedes the first use of 't', so 'SingleGen' is a generic type. // Because the type parameter 'T' is generic, it could be anything, in.. 2023. 1. 4.
[Rust] Enum 예제 (21일차) #[derive(Debug)] enum Gender{ Male, Female, } #[derive(Debug)] enum Info { // String 타입 Name(String), // u32 타입 Age(u32), // 구조체 Location{x : i32, y : i32}, // enum Gender(Gender) } fn main() { let name = Info::Name(String::from("Nach Folge")); let age = Info::Age(29); let location = Info::Location{ x : 132, y : 80 }; let gender = Info::Gender(Gender::Male); println!("Name : {:?}", name); printl.. 2023. 1. 2.
[Rust] Guess 예제 (20일차) fn main() { let secret_number = 5; let mut guess = String::new(); println!("Please enter your guess:"); std::io::stdin().read_line(&mut guess) .expect("Failed to read line"); let guess: u32 = match guess.trim().parse() { Ok(num) => num, Err(_) => { println!("Please enter a number!"); return; }, }; if guess == secret_number { println!("You guessed it!"); } else { println!("Sorry, try again."); } .. 2023. 1. 1.
[Rust] Error 처리 Result 예제 (19일차) use std::fs::File; fn main() { let f = File::open("hello.txt"); let f = match f { Ok(file) => file, Err(error) => { panic!("There was a problem opening the file: {:?}", error) }, }; } use std::fs::File; use std::io::ErrorKind; fn main() { let f = File::open("hello.txt"); let f = match f { Ok(file) => file, Err(ref error) if error.kind() == ErrorKind::NotFound => { match File::create("hello.txt").. 2023. 1. 1.