This commit is contained in:
2025-03-26 12:41:58 -04:00
parent 637b26a0e9
commit 416dd617e6
36 changed files with 732 additions and 0 deletions

345
Cargo.lock generated Normal file
View File

@@ -0,0 +1,345 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "anyhow"
version = "1.0.86"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b3d1d046238990b9cf5bcde22a3fb3584ee5cf65fb2765f454ed428c7a0063da"
[[package]]
name = "autocfg"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0"
[[package]]
name = "bitflags"
version = "2.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "crossbeam"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1137cd7e7fc0fb5d3c5a8678be38ec56e819125d8d7907411fe24ccb943faca8"
dependencies = [
"crossbeam-channel",
"crossbeam-deque",
"crossbeam-epoch",
"crossbeam-queue",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-channel"
version = "0.5.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "33480d6946193aa8033910124896ca395333cae7e2d1113d1fef6c3272217df2"
dependencies = [
"crossbeam-utils",
]
[[package]]
name = "crossbeam-deque"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "613f8cc01fe9cf1a3eb3d7f488fd2fa8388403e97039e2f73692932e291a770d"
dependencies = [
"crossbeam-epoch",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-epoch"
version = "0.9.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e"
dependencies = [
"crossbeam-utils",
]
[[package]]
name = "crossbeam-queue"
version = "0.3.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df0346b5d5e76ac2fe4e327c5fd1118d6be7c51dfb18f9b7922923f287471e35"
dependencies = [
"crossbeam-utils",
]
[[package]]
name = "crossbeam-utils"
version = "0.8.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80"
[[package]]
name = "either"
version = "1.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0"
[[package]]
name = "example_1"
version = "0.1.0"
[[package]]
name = "example_10"
version = "0.1.0"
dependencies = [
"serde",
]
[[package]]
name = "example_11"
version = "0.1.0"
[[package]]
name = "example_2"
version = "0.1.0"
dependencies = [
"crossbeam",
"parking_lot",
]
[[package]]
name = "example_3"
version = "0.1.0"
dependencies = [
"rayon",
]
[[package]]
name = "example_4"
version = "0.1.0"
[[package]]
name = "example_5"
version = "0.1.0"
[[package]]
name = "example_6"
version = "0.1.0"
[[package]]
name = "example_7"
version = "0.1.0"
[[package]]
name = "example_8"
version = "0.1.0"
[[package]]
name = "example_9"
version = "0.1.0"
dependencies = [
"anyhow",
]
[[package]]
name = "libc"
version = "0.2.155"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c"
[[package]]
name = "lock_api"
version = "0.4.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "07af8b9cdd281b7915f413fa73f29ebd5d55d0d3f0155584dade1ff18cea1b17"
dependencies = [
"autocfg",
"scopeguard",
]
[[package]]
name = "parking_lot"
version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bf18183cf54e8d6059647fc3063646a1801cf30896933ec2311622cc4b9a27"
dependencies = [
"lock_api",
"parking_lot_core",
]
[[package]]
name = "parking_lot_core"
version = "0.9.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8"
dependencies = [
"cfg-if",
"libc",
"redox_syscall",
"smallvec",
"windows-targets",
]
[[package]]
name = "proc-macro2"
version = "1.0.86"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.36"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7"
dependencies = [
"proc-macro2",
]
[[package]]
name = "rayon"
version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b418a60154510ca1a002a752ca9714984e21e4241e804d32555251faf8b78ffa"
dependencies = [
"either",
"rayon-core",
]
[[package]]
name = "rayon-core"
version = "1.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1465873a3dfdaa8ae7cb14b4383657caab0b3e8a0aa9ae8e04b044854c8dfce2"
dependencies = [
"crossbeam-deque",
"crossbeam-utils",
]
[[package]]
name = "redox_syscall"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a908a6e00f1fdd0dfd9c0eb08ce85126f6d8bbda50017e74bc4a4b7d4a926a4"
dependencies = [
"bitflags",
]
[[package]]
name = "scopeguard"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
[[package]]
name = "serde"
version = "1.0.207"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5665e14a49a4ea1b91029ba7d3bca9f299e1f7cfa194388ccc20f14743e784f2"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.207"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6aea2634c86b0e8ef2cfdc0c340baede54ec27b1e46febd7f80dffb2aa44a00e"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "smallvec"
version = "1.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67"
[[package]]
name = "syn"
version = "2.0.74"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fceb41e3d546d0bd83421d3409b1460cc7444cd389341a4c880fe7a042cb3d7"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "unicode-ident"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "windows-targets"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_gnullvm",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
[[package]]
name = "windows_i686_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b"
[[package]]
name = "windows_i686_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
[[package]]
name = "windows_i686_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"

16
Cargo.toml Normal file
View File

@@ -0,0 +1,16 @@
[workspace]
resolver = "2"
members = [
"example_1",
"example_10",
"example_11",
"example_2",
"example_3",
"example_4",
"example_5",
"example_6",
"example_7",
"example_8",
"example_9",
]

1
example_1/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/target

7
example_1/Cargo.lock generated Normal file
View File

@@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "example_1"
version = "0.1.0"

6
example_1/Cargo.toml Normal file
View File

@@ -0,0 +1,6 @@
[package]
name = "example_1"
version = "0.1.0"
edition = "2021"
[dependencies]

6
example_1/src/main.rs Normal file
View File

@@ -0,0 +1,6 @@
fn main() {
let mut vec = vec![1, 2, 3];
let first: &i32 = &vec[0];
vec.push(92);
println!("{}", *first);
}

1
example_10/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/target

12
example_10/Cargo.toml Normal file
View File

@@ -0,0 +1,12 @@
[package]
name = "example_10"
version = "0.1.0"
edition = "2021"
[dependencies]
serde = { version = "1", features = ["derive"] }
[features]
default = ["test"]
test = []

28
example_10/src/main.rs Normal file
View File

@@ -0,0 +1,28 @@
use serde::Deserialize;
use serde::Serialize;
macro_rules! assert_eq {
($left:expr, $right:expr) => {
let left = $left;
let right = $right;
if left != right {
panic!("assertion failed: {:?} != {:?}", left, right);
}
};
}
#[derive(Serialize, Deserialize)]
struct Person {
name: String,
age: u32,
}
fn main() {
assert_eq!(1 + 1, 2);
if cfg!(feature = "test") {
println!("yes")
} else {
println!("no")
}
}

1
example_11/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/target

6
example_11/Cargo.toml Normal file
View File

@@ -0,0 +1,6 @@
[package]
name = "example_11"
version = "0.1.0"
edition = "2021"
[dependencies]

14
example_11/src/main.rs Normal file
View File

@@ -0,0 +1,14 @@
use std::slice;
fn main() {
let some_vector = vec![1, 2, 3, 4];
let pointer = some_vector.as_ptr();
let length = some_vector.len();
unsafe {
let my_slice: &[u32] = slice::from_raw_parts(pointer, length);
assert_eq!(some_vector.as_slice(), my_slice);
}
}

1
example_2/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/target

8
example_2/Cargo.toml Normal file
View File

@@ -0,0 +1,8 @@
[package]
name = "example_2"
version = "0.1.0"
edition = "2021"
[dependencies]
parking_lot = "0.12.3"
crossbeam = "0.8.4"

21
example_2/src/main.rs Normal file
View File

@@ -0,0 +1,21 @@
use crossbeam::scope;
use parking_lot::{Mutex, MutexGuard};
fn main() {
let mut counter = Mutex::new(0);
scope(|s| {
for _ in 0..10 {
s.spawn(|_| {
for _ in 0..10 {
let mut guard: MutexGuard<i32> = counter.lock();
*guard += 1;
}
});
}
})
.unwrap();
let total: &mut i32 = counter.get_mut();
println!("total = {}", *total)
}

1
example_3/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/target

7
example_3/Cargo.toml Normal file
View File

@@ -0,0 +1,7 @@
[package]
name = "example_3"
version = "0.1.0"
edition = "2021"
[dependencies]
rayon = "1.10.0"

19
example_3/src/main.rs Normal file
View File

@@ -0,0 +1,19 @@
use rayon::iter::{IntoParallelIterator, IntoParallelRefIterator, ParallelIterator};
use std::fs::read_to_string;
fn main() {
// read a csv file to a string
let my_string = read_to_string("my_file.txt").unwrap();
let my_vec = my_string.lines().collect::<Vec<&str>>();
let my_vec: Vec<String> = my_vec
//.into_iter()
.into_par_iter()
.filter(|x| x.contains("ERROR"))
.map(|x| x.to_owned())
.collect();
// this should now print a vec of vecs
// where every single value is the "Hello world!" string
println!("{:?}", my_vec);
}

1
example_4/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/target

6
example_4/Cargo.toml Normal file
View File

@@ -0,0 +1,6 @@
[package]
name = "example_4"
version = "0.1.0"
edition = "2021"
[dependencies]

15
example_4/src/main.rs Normal file
View File

@@ -0,0 +1,15 @@
#[derive(Debug)]
enum Tree<T> {
Empty,
Node(T, Box<Tree<T>>, Box<Tree<T>>),
}
fn main() {
let tree = Tree::Node(
42,
Box::new(Tree::Node(0, Box::new(Tree::Empty), Box::new(Tree::Empty))),
Box::new(Tree::Empty),
);
println!("{:?}", tree); // prints Node(42, Node(0, Empty, Empty), Empty)
}

1
example_5/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/target

6
example_5/Cargo.toml Normal file
View File

@@ -0,0 +1,6 @@
[package]
name = "example_5"
version = "0.1.0"
edition = "2021"
[dependencies]

27
example_5/src/main.rs Normal file
View File

@@ -0,0 +1,27 @@
struct Years(i64);
struct Days(i64);
impl Years {
pub fn to_days(&self) -> Days {
Days(self.0 * 365)
}
}
impl Days {
/// truncates partial years
pub fn to_years(&self) -> Years {
Years(self.0 / 365)
}
}
fn is_adult(age: &Years) -> bool {
age.0 >= 18
}
fn main() {
let age = Years(25);
let age_days = age.to_days();
println!("Is an adult? {}", is_adult(&age));
println!("Is an adult? {}", is_adult(&age_days.to_years()));
// println!("Is an adult? {}", is_adult(&age_days));
}

1
example_6/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/target

6
example_6/Cargo.toml Normal file
View File

@@ -0,0 +1,6 @@
[package]
name = "example_6"
version = "0.1.0"
edition = "2021"
[dependencies]

46
example_6/src/main.rs Normal file
View File

@@ -0,0 +1,46 @@
use std::fmt::Display;
struct GameScore<T> {
name: String,
scores: Vec<T>,
}
struct Example {
name: String,
}
impl Example {
fn new(name: String) -> Self {
Self { name }
}
}
impl<T: Ord> GameScore<T> {
fn high_score(&self) -> Option<&T> {
self.scores.iter().max()
}
}
impl<T: Display> GameScore<T> {
fn reverse_print_scores(&mut self) {
while let Some(last) = self.scores.pop() {
println!("{}", last);
}
}
}
fn main() {
let mut taylor_score = GameScore {
name: "taylor".to_string(),
scores: vec![Example::new("test".to_string())],
};
let mut bad_score = GameScore {
name: "taylor".to_string(),
scores: vec![10, 200, 3],
};
println!("{:?}", taylor_score.high_score());
taylor_score.reverse_print_scores();
}

1
example_7/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/target

6
example_7/Cargo.toml Normal file
View File

@@ -0,0 +1,6 @@
[package]
name = "example_7"
version = "0.1.0"
edition = "2021"
[dependencies]

27
example_7/src/main.rs Normal file
View File

@@ -0,0 +1,27 @@
fn double_free() {
// every value has an owner, responsible for destructor (RAII).
// compiler checks:
// only ever one owner:
// no double-free
let x: Vec<u32> = Vec::new();
let y = x;
drop(x); // illegal, y is now owner
}
fn main() {
double_free();
immutable();
}
// Functions and structs can be declared any order (looking at you c++ and python!)
fn immutable() {
let v = Vec::new();
// this compiles just fine:
println!("len: {}", v.len());
// this will not compile; would need mutable access
v.push(42);
}

1
example_8/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/target

6
example_8/Cargo.toml Normal file
View File

@@ -0,0 +1,6 @@
[package]
name = "example_8"
version = "0.1.0"
edition = "2021"
[dependencies]

23
example_8/src/main.rs Normal file
View File

@@ -0,0 +1,23 @@
use std::rc::Rc; // reference-counted, non atomic
use std::sync::Arc; // reference-counted, atomic
fn main() {
// this will not compile:
let rc = Rc::new("not thread safe");
std::thread::spawn(move || {
println!("I have an rc with: {}", rc);
});
// this compiles fine:
let arc = Arc::new("thread safe");
std::thread::spawn(move || {
println!("I have an arc with: {}", arc);
});
// this will also not compile:
let mut v = Vec::new();
std::thread::spawn(|| {
v.push(42);
});
let _ = v.pop();
}

1
example_9/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/target

7
example_9/Cargo.toml Normal file
View File

@@ -0,0 +1,7 @@
[package]
name = "example_9"
version = "0.1.0"
edition = "2021"
[dependencies]
anyhow = "1.0.86"

51
example_9/src/main.rs Normal file
View File

@@ -0,0 +1,51 @@
use anyhow::{Context, Result};
use std::num::NonZero;
enum CustomOption<T> {
Some(T),
None,
}
enum CustomResult<T, E> {
Ok(T),
Err(E),
}
struct CustomVec {}
impl CustomVec {
fn new() -> Self {
Self {}
}
}
pub trait Findable<T> {
fn find(&self, fun: T) -> Option<u32>;
}
impl<F> Findable<F> for CustomVec
where
F: Fn(i32) -> bool,
{
fn find(&self, fun: F) -> Option<u32> {
todo!()
}
}
fn do_stuff() -> Result<NonZero<i32>> {
let my_vec = CustomVec::new();
// v is Option<&T>, not &T -- cannot use without checking for None
let v = my_vec.find(|t| t >= 42);
// n is Result<i32, ParseIntError> -- cannot use without checking for Err
let n = "42".parse::<u32>();
// ? suffix is "return Err if Err, otherwise unwrap Ok"
let n = "42".parse::<u32>()?;
let n = NonZero::new(43).context("oopie")?;
Ok(n)
}
fn main() {
do_stuff();
}