From 823321ee27f221db98b0e06a22601f126797eed0 Mon Sep 17 00:00:00 2001 From: gbrochar Date: Mon, 7 Dec 2020 12:44:23 +0100 Subject: [PATCH] smart pointers --- refcells/Cargo.toml | 9 ++++ refcells/src/main.rs | 18 ++++++++ smart_pointers/Cargo.toml | 9 ++++ smart_pointers/src/main.rs | 91 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 127 insertions(+) create mode 100644 refcells/Cargo.toml create mode 100644 refcells/src/main.rs create mode 100644 smart_pointers/Cargo.toml create mode 100644 smart_pointers/src/main.rs diff --git a/refcells/Cargo.toml b/refcells/Cargo.toml new file mode 100644 index 0000000..a0411eb --- /dev/null +++ b/refcells/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "refcells" +version = "0.1.0" +authors = ["gbrochar "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/refcells/src/main.rs b/refcells/src/main.rs new file mode 100644 index 0000000..8834f24 --- /dev/null +++ b/refcells/src/main.rs @@ -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); +} diff --git a/smart_pointers/Cargo.toml b/smart_pointers/Cargo.toml new file mode 100644 index 0000000..8f7d6bc --- /dev/null +++ b/smart_pointers/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "smart_pointers" +version = "0.1.0" +authors = ["gbrochar "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/smart_pointers/src/main.rs b/smart_pointers/src/main.rs new file mode 100644 index 0000000..d6b228b --- /dev/null +++ b/smart_pointers/src/main.rs @@ -0,0 +1,91 @@ +use std::rc::Rc; + +#[derive(Debug)] +enum List { + Cons(i32, Rc), + Nil, +} + +use std::ops::Deref; + +struct MyBox(T); + +impl MyBox { + fn new(x: T) -> MyBox { + MyBox(x) + } +} + +impl Deref for MyBox { + 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)); +} \ No newline at end of file