본문 바로가기

Study56

[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.
[Rust] Hash Map 예제 (18일차) #![allow(unused)] fn main() { use std::collections::HashMap; let teams = vec![String::from("Blue"), String::from("Yellow")]; let initial_scores = vec![10, 50]; let scores: HashMap = teams.iter().zip(initial_scores.iter()).collect(); } #![allow(unused)] fn main() { use std::collections::HashMap; let mut scores = HashMap::new(); scores.insert(String::from("Blue"), 10); scores.insert(String::from(".. 2022. 12. 29.
[Rust] String 예제 (17일차) fn main() { let mut a = String::new(); a = a + "Rust on running"; println!("{}", a); let data = "initial contents"; let s = data.to_string(); println!("{}", s); // the method also works on a literal directlys: let s = "finale contents".to_string(); println!("{}", s); } fn main() { let mut s = String::from("foo"); s.push_str("bar"); println!("{}", s); let mut s1 = String::from("foo"); let s2 = "b.. 2022. 12. 28.