mirror of
https://github.com/gnu4cn/rust-lang-zh_CN.git
synced 2026-08-19 04:33:27 +08:00
Updated 'src/smart_pointers/deref-t'.
This commit is contained in:
6
projects/deref-example/Cargo.toml
Normal file
6
projects/deref-example/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "deref-example"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
25
projects/deref-example/src/main.rs
Normal file
25
projects/deref-example/src/main.rs
Normal file
@@ -0,0 +1,25 @@
|
||||
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) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let x = 5;
|
||||
let y = MyBox::new(x);
|
||||
|
||||
assert_eq! (5, x);
|
||||
assert_eq! (5, *y);
|
||||
}
|
||||
Reference in New Issue
Block a user