oh just cargo fmt it all
This commit is contained in:
@@ -6,25 +6,23 @@ pub fn run() {
|
||||
// arrays and simple variables are allocated to the stack. These have fixed and known lentghs at
|
||||
// compile time. This is fast on the stack.
|
||||
// However, when working with data of unknown lengths, for example String::new(), Rust cannot
|
||||
// know the length at compile time. This is then allocated to the heap with a reference in memory.
|
||||
|
||||
// know the length at compile time. This is then allocated to the heap with a reference in memory.
|
||||
|
||||
// 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 is dropped.
|
||||
|
||||
|
||||
// VARIABLE SCOPE
|
||||
|
||||
|
||||
// This is a string literal. This is hardcoded into the program at compile time.
|
||||
|
||||
{ // s is not valid here, since it has not been declared
|
||||
let s = "hello"; // s is valid from this point onward
|
||||
|
||||
{
|
||||
// s is not valid here, since it has not been declared
|
||||
let s = "hello"; // s is valid from this point onward
|
||||
println!("{s}");
|
||||
// Do stuff with s
|
||||
|
||||
} // this scope is now over, and the variable s is no longer valid
|
||||
// when s comes into scope, it is valid
|
||||
// when s goes out of scope, it is invalid
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -9,7 +9,6 @@ pub fn run() {
|
||||
// Here is how you can use a variable as a reference to a parameter without taking ownership of
|
||||
// it
|
||||
|
||||
|
||||
// s is a reference to s1 which has a reference to an address in memory
|
||||
let s1 = String::from("hello");
|
||||
let len = calculate_length(&s1);
|
||||
@@ -20,10 +19,10 @@ pub fn run() {
|
||||
|
||||
// At any given time, you can have either one mutable reference or any number of immutable references.
|
||||
// References must always be valid.
|
||||
|
||||
}
|
||||
|
||||
fn calculate_length(s: &String) -> usize { // s is a reference to a string
|
||||
fn calculate_length(s: &String) -> usize {
|
||||
// s is a reference to a string
|
||||
s.len()
|
||||
} // Here, s goes out of scope. But because s does not have ownership of what it refers to
|
||||
// The String is not dropped.
|
||||
@@ -49,5 +48,4 @@ fn calculate_length(s: &String) -> usize { // s is a reference to a string
|
||||
|
||||
fn change(some_string: &mut String) {
|
||||
some_string.push_str(", world!");
|
||||
|
||||
}
|
||||
|
||||
@@ -14,7 +14,6 @@ pub fn run() {
|
||||
let f_word = first_word(&s);
|
||||
println!("{f_word}");
|
||||
|
||||
|
||||
let my_string = String::from("hello world");
|
||||
|
||||
// `first_word` works on slices of `String`s, whether partial or whole.
|
||||
|
||||
Reference in New Issue
Block a user