Skip to content

Instantly share code, notes, and snippets.

Created April 25, 2016 14:30
Show Gist options
  • Save anonymous/7a3417d17b920debbe31478bbff31c74 to your computer and use it in GitHub Desktop.
Save anonymous/7a3417d17b920debbe31478bbff31c74 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
struct Book {
name: String
}
impl Book {
fn show_book_name(&self) {
println!("Name is : {}", self.name);
}
}
use std::ops::{Deref, DerefMut};
struct MyBook {
p: Book,
author: String,
}
impl MyBook {
fn new() -> MyBook {
MyBook {
p: Book{ name: "知乎大全".to_string()},
author: "我是谁".to_string()
}
}
}
impl Deref for MyBook {
type Target = Book;
fn deref<'a>(&'a self) -> &'a Book {
&self.p
}
}
fn main() {
let mut mybook = MyBook::new();
mybook.show_book_name();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment