본문 바로가기

전체 글293

[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.
[Rust] Vector 예제 (16일차) // main.rs fn main() { // Create an empty vector let mut v: Vec = Vec::new(); // Push some elements onto the vector v.push(1); v.push(2); v.push(3); // Access elements of the vector using indexing let third: &i32 = &v[2]; println!("The third element is {}", third); // Iterate over the elements of the vector for i in &v { println!("{}", i); } } use std::vec::Vec; fn main() { // Create a new, empt.. 2022. 12. 27.
[Rust] Use - Rust Programming 15일차 - Use 를 통해 Scope 내 함수를 간단히 호출 pub mod a { pub mod series { pub mod of { pub fn nested_modules() {} } } } use a::series::of::nested_modules; fn main() { nested_modules(); } - 열거형의 Variant 가져오기 enum TrafficLight { Red, Yellow, Green, } use TrafficLight::{Red, Yellow}; fn main() { let red = Red; let yellow = Yellow; let green = TrafficLight::Green; } - * (Glob)을 통해 모두 가져오기 enum TrafficLight { Red, .. 2022. 12. 22.
[Rust] Pub&Private - Rust Programming 14일차 - module private & pulbic - 모든 Code Default 값은 Private - Privacy Rules ==> 만일 어떤 Item이 Public이면, 부모 Module의 어디서든 접근 가능 ==> Item이 Private면 같은 File 내 부모 Module 및 해당 부모의 자식 Module만 접근 가능. ==> lib.rs 파일 내 Code 를 Pub mod를 통해 나누기 mod outermost { pub fn middle_function() {} fn middle_secret_function() {} mod inside { pub fn inner_function() {} fn secret_function() {} } } fn try_me() { outermost::middle.. 2022. 12. 21.
[Rust] Rust Programming 13일차 - if let 예제를 공부 [예제 code_1] // 비교를 위한 Match Code // Make `optional` of type `Option` let optional = Some(7); match optional { Some(i) => { println!("This is a really long string and `{:?}`", i); // ^ Needed 2 indentations just so we could destructure // `i` from the option. }, _ => {}, // ^ Required because `match` is exhaustive. Doesn't it seem // like wasted space? }; [예제 code_2] fn main() { /.. 2022. 12. 20.
[Rust] Rust Programming 12일차 - match : 흐름 제어 연산자 - enum 과 arm(갈래) ==> arm은 패턴 & Code로 구성. ==> 패턴이 해당 값과 일치하면 Code를 실행하고, 아니면 다음 Arm으로 넘어감. - Option(T) 와 Solme(T) 에 대하여 - 모든 것에 알맞는 '_ 패턴' - Match 를 간단하게 쓰는 방법? if let & else - Match 예제 코드 fn main() { let number = 20; // TODO ^ try different values for `number` println!("Tell me about {}", number); match number { // Match a single value 1 => println!("One!"), // Match several .. 2022. 12. 19.
[Rust] Rust Programming 11일차 - enumeration (열거) enum IpAddr { V4(Ipv4addr), V6(Ipv6Addr), } ==> IP를 저장하는 흔한 enum 방식의 library - 다양한 타입을 저장하는 방식 enum Message { Quit, Move { x: i32, y: i32 }, Write(String), ChangeColor(i32, i32, i32), } ==> Quit : 연관 Data가 없음 ==> Move : 익명 Struct 을 포함 ==> Write : 하나의 String 포함 ==> Changecolor : 3개의 i32 포함 2022. 12. 18.