part of ch8
This commit is contained in:
parent
41d6c74c1f
commit
b2fe34eb06
7
ch8/strings/Cargo.lock
generated
Normal file
7
ch8/strings/Cargo.lock
generated
Normal file
@ -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"
|
6
ch8/strings/Cargo.toml
Normal file
6
ch8/strings/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "strings"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
31
ch8/strings/src/main.rs
Normal file
31
ch8/strings/src/main.rs
Normal file
@ -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
|
||||||
|
}
|
7
ch8/vectors/Cargo.lock
generated
Normal file
7
ch8/vectors/Cargo.lock
generated
Normal file
@ -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"
|
6
ch8/vectors/Cargo.toml
Normal file
6
ch8/vectors/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "vectors"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
47
ch8/vectors/src/main.rs
Normal file
47
ch8/vectors/src/main.rs
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
fn main() {
|
||||||
|
let v: Vec<i32> = Vec::new();
|
||||||
|
let v = vec![1, 2, 3];
|
||||||
|
|
||||||
|
let mut v: Vec<i32> = 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),
|
||||||
|
];
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user