본문 바로가기

Study56

[Rust] Deref(역참조) 예제 (36일차) use std::ops::Deref; struct MyBox(T); impl MyBox { fn new(x: T) -> MyBox { MyBox(x) } } impl Deref for MyBox { type Target = T; fn deref(&self) -> &T { &self.0 } } fn hello(name: &str) { println!("Hello, {}!", name); } fn main() { let m = MyBox::new(String::from("Rust")); hello(&m); } https://rinthel.github.io/rust-lang-book-ko/ch15-02-deref.html 2023. 1. 19.
[Rust] Ownership(소유권) 기초 예제 : For Loop (35일차) [예제 코드_1] fn main() { let names = vec![ String::from("John"), String::from("Jane"), ]; for name in names { println!("{}", name); } } [실행 결과] John Jane [예제 코드_2] fn main() { let names = vec![ String::from("John"), String::from("Jane"), ]; for name in names { println!("{}", name); } println!("Names: {:?}", names); } [실행 결과] error[E0382]: borrow of moved value: `names` --> src/main.rs:11:29 [예제 코드_.. 2023. 1. 18.
[Rust] Ownership(소유권) 기초 예제 : Vec<String> (34일차) [예제 코드_1] fn main() { let names = vec![ String::from("John"), String::from("Jane"), ]; do_something(names); } fn do_something(names: Vec) { println!("{:?}", names); } [실행 결과] ["John", "Jane"] [예제 코드_2] fn main() { let names = vec![ String::from("John"), String::from("Jane"), ]; do_something(names); println!("{:?}", names); } fn do_something(names: Vec) { println!("{:?}", names); } [실행 결과] error[.. 2023. 1. 16.
[Rust] Ownership(소유권) 기초 예제 : Struct (33일차) [예제 코드_1] #[derive(Debug)] struct Movie { title: String, } fn main() { let movie = Movie { title: String::from("Rust") }; do_something(movie); } fn do_something(movie: Movie) { println!("Movie: {:?}!", movie); } [실행 결과] Movie: Movie { title: "Rust" }! [예제 코드_2] #[derive(Debug)] struct Movie { title: String, } fn main() { let movie = Movie { title: String::from("Rust") }; do_something(movie); pri.. 2023. 1. 15.