본문 바로가기

전체 글293

[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.
[MySQL] With 예제 WITH RECURSIVE cte (n, fibonacci) AS ( SELECT 1, 1 UNION ALL SELECT n + 1, fibonacci + cte.fibonacci FROM cte WHERE n with를 통해 조건_1로 필터링 한 data를 조건_2로 다시 필터링 WITH cte AS ( SELECT * FROM table_1 WHERE condition_1 ) SELECT * FROM cte WHERE condition_2; https://chat.openai.com/chat 2023. 1. 29.
[Rust] 고급 Trait 예제 (46일차) impl Iterator for Counter { type Item = u32; fn next(&mut self) -> Option; } pub trait Iterator { fn next(&mut self) -> Option; } use std::ops::Add; #[derive(Debug, PartialEq)] struct Point { x: i32, y: i32, } impl Add for Point { type Output = Point; fn add(self, other: Point) -> Point { Point { x: self.x + other.x, y: self.y + other.y, } } } fn main() { assert_eq!(Point { x: 1, y: 0 } + Point .. 2023. 1. 29.
[Rust] Pattern : 여러 예제 (45일차) fn main() { let x = Some(5); let y = 10; match x { Some(50) => println!("Got 50"), Some(y) => println!("Matched, y = {:?}", y), _ => println!("Default case, x = {:?}", x), } println!("at the end: x = {:?}, y = {:?}", x, y); } fn main() { let x = 1; match x { 1 | 2 => println!("one or two"), 3 => println!("three"), _ => println!("anything"), } } fn main() { let x = 5; match x { 1..=5 => println!(.. 2023. 1. 28.
[Rust] Trait 예제 (44일차) #![allow(dead_code)] fn main() { trait Animal { fn eat(&self); } struct Herbivore; struct Carnivore; impl Animal for Herbivore { fn eat(&self) { println!("I eat plants"); } } impl Animal for Carnivore { fn eat(&self) { println!("I eat flesh"); } } let h = Herbivore; h.eat(); } [실행 결과] $ I eat plants fn main() { trait Animal { fn eat(&self); } struct Herbivore; struct Carnivore; impl Animal for H.. 2023. 1. 27.
[Rust] 공유 상태 동시성 예제 (Shared State Concurrency, 43일차) use std::sync::{Mutex, Arc}; use std::thread; fn main() { let counter = Arc::new(Mutex::new(0)); let mut handles = vec![]; for _ in 0..10 { let counter = Arc::clone(&counter); let handle = thread::spawn(move || { let mut num = counter.lock().unwrap(); *num += 1; }); handles.push(handle); } for handle in handles { handle.join().unwrap(); } println!("Result: {}", *counter.lock().unwrap()); } https.. 2023. 1. 26.
[Rust] Concurrency : Message Passing 예제 (42일차) use std::thread; use std::sync::mpsc; fn main() { let (tx, rx) = mpsc::channel(); thread::spawn(move || { let val = String::from("Hi"); tx.send(val).unwrap(); }); let received = rx.recv().unwrap(); println!("Got: {}", received); } use std::thread; use std::sync::mpsc; fn main() { let (tx, rx) = mpsc::channel(); thread::spawn(move || { let val = String::from("Greetings"); tx.send(val).unwrap(); p.. 2023. 1. 25.