본문 바로가기
Rust

[Rust] Deref(역참조) 예제 (36일차)

by 꾸압 2023. 1. 19.

 

<예제 코드_1>

use std::ops::Deref;

struct MyBox<T>(T);

impl<T> MyBox<T> {
    fn new(x: T) -> MyBox<T> {
        MyBox(x)
    }
}

impl<T> Deref for MyBox<T> {
    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);
}

 

 


 

<참조 1> https://rinthel.github.io/rust-lang-book-ko/ch15-02-deref.html

<참조_2>

 

 

댓글