From f91da817be5688f052bce317b1f70ad7176fd13b Mon Sep 17 00:00:00 2001 From: Jordan Gong Date: Sun, 20 Sep 2020 15:55:28 +0800 Subject: Loading data on the heap using box --- smart_pointer/.gitignore | 18 ++++++++++++++++++ smart_pointer/Cargo.toml | 9 +++++++++ smart_pointer/src/main.rs | 14 ++++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 smart_pointer/.gitignore create mode 100644 smart_pointer/Cargo.toml create mode 100644 smart_pointer/src/main.rs diff --git a/smart_pointer/.gitignore b/smart_pointer/.gitignore new file mode 100644 index 0000000..e629269 --- /dev/null +++ b/smart_pointer/.gitignore @@ -0,0 +1,18 @@ + +# Created by https://www.toptal.com/developers/gitignore/api/rust +# Edit at https://www.toptal.com/developers/gitignore?templates=rust + +### Rust ### +# Generated by Cargo +# will have compiled files and executables +/target/ + +# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries +# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html +Cargo.lock + +# These are backup files generated by rustfmt +**/*.rs.bk + +# End of https://www.toptal.com/developers/gitignore/api/rust + diff --git a/smart_pointer/Cargo.toml b/smart_pointer/Cargo.toml new file mode 100644 index 0000000..cdc8b9b --- /dev/null +++ b/smart_pointer/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "smart_pointer" +version = "0.1.0" +authors = ["Jordan Gong "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/smart_pointer/src/main.rs b/smart_pointer/src/main.rs new file mode 100644 index 0000000..b19ab5f --- /dev/null +++ b/smart_pointer/src/main.rs @@ -0,0 +1,14 @@ +// Recursive type using boxes +enum List { + Cons(i32, Box), + Nil, +} + +fn main() { + // Storing `i32` value on heap + let b = Box::new(5); + println!("b = {}", b); + + use crate::List::{Cons, Nil}; + let list = Cons(1, Box::new(Cons(2, Box::new(Cons(3, Box::new(Nil)))))); +} -- cgit v1.2.3