diff --git a/ch8/strings/Cargo.lock b/ch8/strings/Cargo.lock new file mode 100644 index 0000000..d85cfd9 --- /dev/null +++ b/ch8/strings/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "strings" +version = "0.1.0" diff --git a/ch8/strings/Cargo.toml b/ch8/strings/Cargo.toml new file mode 100644 index 0000000..636ed4f --- /dev/null +++ b/ch8/strings/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "strings" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/ch8/strings/src/main.rs b/ch8/strings/src/main.rs new file mode 100644 index 0000000..e7f529d --- /dev/null +++ b/ch8/strings/src/main.rs @@ -0,0 +1,31 @@ +fn main() { + let mut s = String::new(); + + let data = "initial contents"; + let s = data.to_string(); + + // this also works + let s = "initial contents".to_string(); + + // Updating a String + let mut s = String::from("foo"); + s.push_str("bar"); + + let mut s1 = String::from("foo"); + let s2 = "bar"; + s1.push_str(s2); + println!("s2 is {s2}"); + + // push method takes a character and not a string + let mut s = String::from("lo"); + s.push('l'); + + let s1 = String::from("tic"); + let s2 = String::from("tac"); + let s3 = String::from("toe"); + + let s_total = format!("{s1}-{s2}-{s3}"); + + // stopped here: + // https://rust-book.cs.brown.edu/ch08-02-strings.html#internal-representation +} diff --git a/ch8/vectors/Cargo.lock b/ch8/vectors/Cargo.lock new file mode 100644 index 0000000..b70f19b --- /dev/null +++ b/ch8/vectors/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "vectors" +version = "0.1.0" diff --git a/ch8/vectors/Cargo.toml b/ch8/vectors/Cargo.toml new file mode 100644 index 0000000..46314ed --- /dev/null +++ b/ch8/vectors/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "vectors" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/ch8/vectors/src/main.rs b/ch8/vectors/src/main.rs new file mode 100644 index 0000000..cce02f2 --- /dev/null +++ b/ch8/vectors/src/main.rs @@ -0,0 +1,47 @@ +fn main() { + let v: Vec = Vec::new(); + let v = vec![1, 2, 3]; + + let mut v: Vec = Vec::new(); + v.push(5); + v.push(6); + v.push(7); + + // can access via traditional indexing with [] + // or via the .get(index) function. + let v = vec![1, 2, 3, 4, 5]; + + let third: &i32 = &v[2]; + println!("The third element is: {third}"); + + let third: Option<&i32> = v.get(2); + match third { + Some(third) => println!("The third element is: {third}"), + None => println!("There is no third element!"), + } + + let v = vec![100, 32, 57]; + for n_ref in &v { + let n_plus_one: i32 = *n_ref + 1; + println!("{n_plus_one}"); + } + + let mut v = vec![100, 32, 57]; + for n_ref in &mut v { + *n_ref += 50; + } + println!("v post 50 addition: {v:?}"); + + // Using an Enum to Store Multiple Types + enum SpreadsheetCell { + Int(i32), + Float(f64), + Text(String), + } + + let row = vec![ + SpreadsheetCell::Int(3), + SpreadsheetCell::Text(String::from("blue")), + SpreadsheetCell::Float(10.12), + ]; +}