본문 바로가기

Example28

[Rust] 실시간 Chat App in Rocket (56일차) # root폴더/Cargo.toml [package] name = "chat-app" version = "0.1.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] rocket = { version = "0.5.0-rc.1", features = ["json"] } [dev-dependecies] rand = "0.8" // root폴더/src/main.rs #[macro_use] extern crate rocket; #[cfg(test)] mod tests; use rocket::{tokio::sync::broadcast.. 2023. 2. 9.
[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.