본문 바로가기

Study56

[Rust] HashMap 예제 (52일차) fn main() { use std::collections::HashMap; // HashMap 생성 let mut hash_ages : HashMap = HashMap::new(); // 데이터 삽입 hash_ages.insert(String::from("Nachfolge"), 32); println!("HashMap : {:?}", hash_ages); } fn main() { use std::collections::HashMap; // HashMap 키에 해당하는 벡터 생성 let names = vec![String::from("Nachfolge"), String::from("Selah")]; // HashMap 값에 해당하는 벡터 생성 let ages = vec![10, 50]; // HashMa.. 2023. 2. 3.
[Rust] Overloading 예제 (51일차) use std::ops; struct Foo; struct Bar; #[derive(Debug)] struct FooBar; #[derive(Debug)] struct BarFoo; // The 'std::ops::Add' trait is used to specify the functionality of '+'. // Here, we make 'Add' - the trait for addition with a RHS of type 'Bar'. // The follow block implements the operation: Foo + bar = FooBar impl ops::Add for Foo { type Output = FooBar; fn add(self, _rhs: Bar) -> FooBar { p.. 2023. 2. 2.
[Rust] Field Init Shorthand, 필드 초기화 (50일차) Field Init Shorthand : 필드 초기화 단축 을 쓰는 이유? >> name field를가진 Data Structure (struct, enum, union)를 초기화할 때 fieldname: fieldname 형식으로 fieldname 작성을 압축 시켜 Code 중복을 적게하고 압축된 Syntax를 제공, >> When initializing a data structure (struct, enum, union) with named fields, allow writing fieldname as a shorthand for fieldname: fieldname. This allows a compact syntax for initialization, with less duplication. #[.. 2023. 2. 1.
[Rust] Web Server : Multi-Threading 추가 (49일차) >> 이전에 작업한 lib.rs 파일 Code 를 수정 >> 중간에 lifetime syntax ` 로 인해 주석처럼 보이나, 정상 Compile 되는 코드다. use std::thread; use std::sync::mpsc; use std::sync::Arc; use std::sync::Mutex; pub struct ThreadPool { workers: Vec, sender: mpsc::Sender, } trait FnBox { fn call_box(self: Box); } impl FnBox for F { fn call_box(self: Box) { (*self)() } } type Job = Box ThreadPool { assert!(size > 0); let (sender, receiver.. 2023. 1. 31.