Updated 'src/smart_pointers/deref-t'.

This commit is contained in:
Hector PENG
2026-04-03 12:50:29 +08:00
parent 5ded3d4242
commit 6ee46dc93a
5 changed files with 70 additions and 265 deletions

View File

@@ -0,0 +1,6 @@
[package]
name = "deref-example"
version = "0.1.0"
edition = "2024"
[dependencies]

View 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);
}