본문 바로가기

Rust61

[Rust] Derive 예제 (54일차) // 'Centimeters', a tuple struct that can be compared #[derive(PartialEq, PartialOrd)] struct Centimeters(f64); // 'Inches', a tuple struct that can be printed #[derive(Debug)] struct Inches(i32); impl Inches { fn to_centimeters(&self) -> Centimeters { let &Inches(inches) = self; Centimeters(inches as f64 * 2.54) } } // 'Seconds', a tuple struct with no additional attributes struct Seconds(i32);.. 2023. 2. 7.
[Rust] Tokio 예제 : 기본 (53일차) use tokio::net::TcpListener; use tokio::io::{AsyncReadExt, AsyncWriteExt}; #[tokio::main] async fn main() -> Result { let listener = TcpListener::bind("127.0.0.1:8080").await?; loop { let (mut socket, _) = listener.accept().await?; tokio::spawn(async move { let mut buf = [0; 1024]; // In a loop, read data from the socket and write the data back. loop { let n = match socket.read(&mut buf).await {.. 2023. 2. 5.
[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.