From 619f32677cd26d00a1fa94eae593edffc3425e95 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Wed, 19 Mar 2025 15:58:03 -0500 Subject: [PATCH] more ch4 progress --- ch4/ownership/src/main.rs | 15 ++++++++++++--- ch4/refs_and_borrow/Cargo.lock | 7 +++++++ ch4/refs_and_borrow/Cargo.toml | 6 ++++++ ch4/refs_and_borrow/src/main.rs | 26 ++++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 ch4/refs_and_borrow/Cargo.lock create mode 100644 ch4/refs_and_borrow/Cargo.toml create mode 100644 ch4/refs_and_borrow/src/main.rs diff --git a/ch4/ownership/src/main.rs b/ch4/ownership/src/main.rs index 7a453c9..d481cf6 100644 --- a/ch4/ownership/src/main.rs +++ b/ch4/ownership/src/main.rs @@ -52,12 +52,16 @@ fn main() { // When variable owns a box, when Rust deallocates the variable's frame, // then Rust deallocates the box's heap memory. let d = Box::new([0; 1_000_000]); // d owns this Box here - let e = d; // e has ownership of the box transfered to it from d + let e = d; // e has ownership of the box transfered to it from d. Say it's moved // Rust data structures like String, Vec, and HashMap use Boxes. - // Stopped here for the night: - // https://rust-book.cs.brown.edu/ch04-01-what-is-ownership.html#variables-cannot-be-used-after-being-moved + // Variables can't be used after moving + // Can use .clone() to avoid moving data + let first = String::from("Ferris"); + let first_clone = first.clone(); + let full = add_suffix(first_clone); + println!("{full}, originally {first}"); } fn plus_one(x: i32) -> i32 { @@ -67,3 +71,8 @@ fn plus_one(x: i32) -> i32 { fn make_and_drop() { let a_box = Box::new(5); } + +fn add_suffix(mut name: String) -> String { + name.push_str(" Jr. "); + name +} diff --git a/ch4/refs_and_borrow/Cargo.lock b/ch4/refs_and_borrow/Cargo.lock new file mode 100644 index 0000000..c665cc8 --- /dev/null +++ b/ch4/refs_and_borrow/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "refs_and_borrow" +version = "0.1.0" diff --git a/ch4/refs_and_borrow/Cargo.toml b/ch4/refs_and_borrow/Cargo.toml new file mode 100644 index 0000000..e135a01 --- /dev/null +++ b/ch4/refs_and_borrow/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "refs_and_borrow" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/ch4/refs_and_borrow/src/main.rs b/ch4/refs_and_borrow/src/main.rs new file mode 100644 index 0000000..705c23c --- /dev/null +++ b/ch4/refs_and_borrow/src/main.rs @@ -0,0 +1,26 @@ +fn main() { + // These lines wont work bc m1 and m2 are moved + // let m1 = String::from("Hello"); + // let m2 = String::from("World"); + // greet(m1, m2); + // let s = format!("{} {}", m1, m2); + + // This is suboptimal + let m1 = String::from("Hello"); + let m2 = String::from("World"); + let (m1_again, m2_again) = greet_ret(m1, m2); + let s = format!("{} {}", m1_again, m2_again); + println!("{s}"); + + // stopped here: + // https://rust-book.cs.brown.edu/ch04-02-references-and-borrowing.html#references-are-non-owning-pointers +} + +fn greet(g1: String, g2: String) { + println!("{} {}!", g1, g2); +} + +fn greet_ret(g1: String, g2: String) -> (String, String) { + println!("{} {}!", g1, g2); + (g1, g2) +}