본문 바로가기

예제15

[Rust] Variable : Python 코드 비교 예제 (58일차) [Python] if __name__ == '__main__': x = 1; print(x); # bind x again shadows old one from above x = 'i'; print(x) # declare, init something = None; x = 5; print("x, something", x, something); something = x * 5; print("x, something", x, something); # mutability y = 0; y = y * 2 + x; print(y); BLAH = 42; # only const by convention y *= BLAH; [Rust] fn main() { let x = 1; println!("x: {}", x); // bi.. 2023. 2. 18.
[Rust] Web Crawler 예제 (55일차) ==> HTTP Request 생성 # Cargo.toml # ...중략 [dependencies] reqwest = { version = "0.11", features = ["json", "blocking"] }# Request with JSON parsing support futures = "0.3"# for our async / await blocks tokio = { version = "1.12.0", features = {"full"} } # for our async runtime // src/main.rs use std::io::Read; fn main() { let client = reqwest::blocking::Client::new(); let origin_url = "https://ro.. 2023. 2. 8.
[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.