diff --git a/Cargo.lock b/Cargo.lock index 07fef02..cf35f11 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -48,6 +48,10 @@ version = "0.2.189" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3eaf3ede3fee6db1a4c2ee091bf8a8b4dccdc6d17f656fb07896ee72867612f2" +[[package]] +name = "ownership" +version = "0.1.0" + [[package]] name = "ppv-lite86" version = "0.2.21" diff --git a/Cargo.toml b/Cargo.toml index daab00e..27e5161 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [workspace] -members = ["common-programming-concepts", "common-programming-concepts-fibonacci", "common-programming-concepts-temp-converter", "common-programming-concepts-twelve-days-of-christmas","guessing-game"] +members = ["common-programming-concepts", "common-programming-concepts-fibonacci", "common-programming-concepts-temp-converter", "common-programming-concepts-twelve-days-of-christmas","guessing-game", "ownership"] resolver = "2" [workspace.package] diff --git a/ownership/Cargo.toml b/ownership/Cargo.toml new file mode 100644 index 0000000..7d7dc80 --- /dev/null +++ b/ownership/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "ownership" +edition.workspace = true +version.workspace = true +license.workspace = true + +[dependencies] diff --git a/ownership/README.md b/ownership/README.md new file mode 100644 index 0000000..c8f2fa5 --- /dev/null +++ b/ownership/README.md @@ -0,0 +1,17 @@ +# Ownership + +## The Stack and the Heap +The stack is fast memory which follows the `Last in First out (LIFO)` principle. +On the stack, only data with a know size can live. If it can shrink or grow, then the data must be allocated to the heap. +When data is allocated to the heap, it gets a pointer to an address in memory. +Keeping track of these things are really important in languges like C. Other programming languages like Go use a garbage collector (GC) +which cleans up data in memory on a regular basis. +Rust does neither. It uses `Ownership` to manage memory and data, so each piece of data has an owner, and when that owner gets out of scope, the value is dropped. + +## Ownership rules + +- Each value in Rust has an owner. +- There can only be one owner at a time. +- When the owner goes out of scope, the value will be dropped. + +## Variable Scope diff --git a/ownership/src/main.rs b/ownership/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/ownership/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +}