smart pointers
This commit is contained in:
parent
7ea306ec32
commit
823321ee27
|
@ -0,0 +1,9 @@
|
|||
[package]
|
||||
name = "refcells"
|
||||
version = "0.1.0"
|
||||
authors = ["gbrochar <gaetanbrochard@protonmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
|
@ -0,0 +1,18 @@
|
|||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
fn main() {
|
||||
let mut x = 5;
|
||||
let y = &mut x;
|
||||
|
||||
*y = 10;
|
||||
println!("y: {}", y);
|
||||
drop(y);
|
||||
println!("x: {}", x);
|
||||
|
||||
let a = Rc::new(RefCell::new(5));
|
||||
let b = Rc::clone(&a);
|
||||
|
||||
*b.borrow_mut() += 10;
|
||||
println!("a: {:?}, b: {:?}", a, b);
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
[package]
|
||||
name = "smart_pointers"
|
||||
version = "0.1.0"
|
||||
authors = ["gbrochar <gaetanbrochard@protonmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
|
@ -0,0 +1,91 @@
|
|||
use std::rc::Rc;
|
||||
|
||||
#[derive(Debug)]
|
||||
enum List {
|
||||
Cons(i32, Rc<List>),
|
||||
Nil,
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
struct CustomSmartPointer {
|
||||
data: String,
|
||||
}
|
||||
|
||||
impl Drop for CustomSmartPointer {
|
||||
fn drop(&mut self) {
|
||||
println!("Dropping CustomSmartPointer with data `{}`!", self.data);
|
||||
}
|
||||
}
|
||||
|
||||
use crate::List::{Cons, Nil};
|
||||
|
||||
fn hello(name: &str) {
|
||||
println!("Hello, {}!", name);
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let b = Box::new(5);
|
||||
println!("b = {}", b);
|
||||
|
||||
let list = Cons(1, Rc::new(Cons(2, Rc::new(Cons(3, Rc::new(Nil))))));
|
||||
|
||||
println!("{:?}", list);
|
||||
|
||||
let x = 5;
|
||||
let y = MyBox::new(x);
|
||||
|
||||
assert_eq!(5, x);
|
||||
assert_eq!(5, *y);
|
||||
|
||||
let m = MyBox::new(String::from("Rust"));
|
||||
hello(&m);
|
||||
hello(&(*m)[..]);
|
||||
|
||||
|
||||
let c = CustomSmartPointer {
|
||||
data: String::from("my stuff"),
|
||||
};
|
||||
let d = CustomSmartPointer {
|
||||
data: String::from("other stuff"),
|
||||
};
|
||||
println!("CustomSmartPointers created.");
|
||||
println!("{}, {}", c.data, d.data);
|
||||
drop(c);
|
||||
println!("CustomSmartPointer dropped before the end of main.");
|
||||
|
||||
let a = Rc::new(Cons(5, Rc::new(Cons(10, Rc::new(Nil)))));
|
||||
println!("count after creating a = {}", Rc::strong_count(&a));
|
||||
let b = Cons(3, Rc::clone(&a));
|
||||
println!("count after creating b = {}", Rc::strong_count(&a));
|
||||
{
|
||||
let _c = Cons(4, Rc::clone(&a));
|
||||
println!("count after creating c = {}", Rc::strong_count(&a));
|
||||
}
|
||||
match b {
|
||||
Cons(_aa, bb) => {
|
||||
match &*bb {
|
||||
Cons(cc, _dd) => println!("{}", cc),
|
||||
Nil => println!("Nil"),
|
||||
}
|
||||
}
|
||||
Nil => println!("Nil"),
|
||||
};
|
||||
println!("count after c goes out of scope = {}", Rc::strong_count(&a));
|
||||
}
|
Loading…
Reference in New Issue