본문 바로가기

Rust61

[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.
[Rust] Web-Server 제작 (48일차) ==> 수신 스트림 대기 및 수신 시 msg 출력 // main.rs use std::net::TcpListener; fn main() { let listener = TcpListener::bind("127.0.0.1:7878").unwrap(); for stream in listener.incoming() { let stream = stream.unwrap(); println!("Connection established!"); } } >> TcpStream 에서 오는 Data 출력 use std::io::prelude::*; use std::net::TcpStream; use std::net::TcpListener; fn main() { let listener = TcpListener::bind("12.. 2023. 1. 30.
[Rust] Thunk 예제 (47일차) fn main() { // Define and call a thunk let thunk = || println!("I am a thunk"); thunk(); // Defining a closure and storing it in a variable to be used later let closure = || { println!("This is a closure stored in a variable"); }; closure(); } fn main() { // Pass a thunk as an argument to a function fn call_thunk(thunk: &dyn Fn()) { thunk(); } let thunk = || println!("I am a thunk"); call_thunk(.. 2023. 1. 29.