start work on ownership model
This commit is contained in:
Generated
+4
@@ -48,6 +48,10 @@ version = "0.2.189"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "3eaf3ede3fee6db1a4c2ee091bf8a8b4dccdc6d17f656fb07896ee72867612f2"
|
checksum = "3eaf3ede3fee6db1a4c2ee091bf8a8b4dccdc6d17f656fb07896ee72867612f2"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ownership"
|
||||||
|
version = "0.1.0"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ppv-lite86"
|
name = "ppv-lite86"
|
||||||
version = "0.2.21"
|
version = "0.2.21"
|
||||||
|
|||||||
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
[workspace]
|
[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"
|
resolver = "2"
|
||||||
|
|
||||||
[workspace.package]
|
[workspace.package]
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
[package]
|
||||||
|
name = "ownership"
|
||||||
|
edition.workspace = true
|
||||||
|
version.workspace = true
|
||||||
|
license.workspace = true
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
@@ -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
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
fn main() {
|
||||||
|
println!("Hello, world!");
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user