Add Smart Pointers

main
sgoudham 3 years ago
parent cc6483b5b3
commit 3f5d729285
Signed by: hammy
GPG Key ID: 44E818FD5457EEA4

@ -0,0 +1,8 @@
[package]
name = "smart_pointers"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

@ -0,0 +1,33 @@
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 hello(name: &str) {
println!("Hello, {}!", name);
}
fn main() {
let x = 5;
let y = MyBox::new(x);
assert_eq!(5, x);
assert_eq!(5, *y);
let m = MyBox::new("Rust");
hello(&m);
get_name();
}
Loading…
Cancel
Save