From 931eb0894e7cd3717f1218f0eb06382b11734c13 Mon Sep 17 00:00:00 2001 From: Jasper Ras Date: Sun, 3 Aug 2025 22:29:47 +0200 Subject: vault backup: 2025-08-03 22:29:47 --- .obsidian/graph.json | 2 +- ...ssing a tuple by index is different from an array.md | 3 +++ An empty tuple is called a unit.md | 3 +++ Array notation.md | 13 +++++++++++++ ...e useful when you know the length will not change.md | 3 +++ Cargo.md | 9 +++++++++ Char is a unicode scalar value.md | 3 +++ Integers can overflow if compiled with --release.md | 11 +++++++++++ ...mported by default in Rust are called the prelude.md | 5 +++++ Pattern matching with a match expression.md | 13 +++++++++++++ ...lues such as the array are allocated on the stack.md | 3 +++ Random numbers in Rust.md | 3 +++ References are also immutable by default.md | 5 +++++ Rust.md | 17 +++++++++++++++++ Saturating a value when wrapping.md | 12 ++++++++++++ Shadowing variables.md | 12 ++++++++++++ ...c class functions are called associated functions.md | 6 ++++++ ...floats do not have an architecture dependent size.md | 5 +++++ Why is it called destructuring.md | 8 ++++++++ Why it is good to learn rust.md | 5 +++++ 20 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 Accessing a tuple by index is different from an array.md create mode 100644 An empty tuple is called a unit.md create mode 100644 Array notation.md create mode 100644 Arrays are useful when you know the length will not change.md create mode 100644 Cargo.md create mode 100644 Char is a unicode scalar value.md create mode 100644 Integers can overflow if compiled with --release.md create mode 100644 Items that are always imported by default in Rust are called the prelude.md create mode 100644 Pattern matching with a match expression.md create mode 100644 Primitive values such as the array are allocated on the stack.md create mode 100644 Random numbers in Rust.md create mode 100644 References are also immutable by default.md create mode 100644 Saturating a value when wrapping.md create mode 100644 Shadowing variables.md create mode 100644 Static class functions are called associated functions.md create mode 100644 Unlike integers floats do not have an architecture dependent size.md create mode 100644 Why is it called destructuring.md create mode 100644 Why it is good to learn rust.md diff --git a/.obsidian/graph.json b/.obsidian/graph.json index 1cdef47..863f273 100644 --- a/.obsidian/graph.json +++ b/.obsidian/graph.json @@ -17,6 +17,6 @@ "repelStrength": 10, "linkStrength": 1, "linkDistance": 500, - "scale": 0.0889892578125, + "scale": 0.04303687666455931, "close": true } \ No newline at end of file diff --git a/Accessing a tuple by index is different from an array.md b/Accessing a tuple by index is different from an array.md new file mode 100644 index 0000000..58a93a9 --- /dev/null +++ b/Accessing a tuple by index is different from an array.md @@ -0,0 +1,3 @@ +[[Rust]] + +In rust the way to access a tuple's value by a specific index is done with a `.` rather than `[]`. For an array it is no different from other languages. \ No newline at end of file diff --git a/An empty tuple is called a unit.md b/An empty tuple is called a unit.md new file mode 100644 index 0000000..a97056a --- /dev/null +++ b/An empty tuple is called a unit.md @@ -0,0 +1,3 @@ +[[Rust]] + +An empty tuple `()` is named a `unit`. This acts as a sort of null value, it is the default value of functions that return nothing. So instead of saying it returns null I should say it returns a unit. \ No newline at end of file diff --git a/Array notation.md b/Array notation.md new file mode 100644 index 0000000..b4cc810 --- /dev/null +++ b/Array notation.md @@ -0,0 +1,13 @@ +[[Rust]] + +The array notation is a bit special compared to other languages so I will write it down here: + +Unlike tuples, arrays can only hold one type: + +`let a: [i32; 5] = [1, 2, 3, 4, 5]` Specifies an array of length 5. +`let a = [3; 5];` specifies an array of length 5 with all values set to 3. +> Note the semicolon rather than a comma! + +Indexing as usual + +Out of bounds access result in a panic. \ No newline at end of file diff --git a/Arrays are useful when you know the length will not change.md b/Arrays are useful when you know the length will not change.md new file mode 100644 index 0000000..574dd01 --- /dev/null +++ b/Arrays are useful when you know the length will not change.md @@ -0,0 +1,3 @@ +[[Rust]] + +Because they are more efficient than vectors, if you know ahead of time that the length of the array isn't going to change, for example the names of the months: use an array. \ No newline at end of file diff --git a/Cargo.md b/Cargo.md new file mode 100644 index 0000000..baa57ed --- /dev/null +++ b/Cargo.md @@ -0,0 +1,9 @@ +[[Rust]] + +Standard stuff: +- cargo build +- cargo update +- cargo run + +Cool tidbit: +- cargo doc --open diff --git a/Char is a unicode scalar value.md b/Char is a unicode scalar value.md new file mode 100644 index 0000000..00d1f59 --- /dev/null +++ b/Char is a unicode scalar value.md @@ -0,0 +1,3 @@ +[[Rust]] + +In rust a `char` is a scalar value, it is 4 bytes in size and contains a unicode value so it can represent any character. \ No newline at end of file diff --git a/Integers can overflow if compiled with --release.md b/Integers can overflow if compiled with --release.md new file mode 100644 index 0000000..94d80ca --- /dev/null +++ b/Integers can overflow if compiled with --release.md @@ -0,0 +1,11 @@ +[[Rust]] + +Integers that overflow wrap around. ie in an i8 256 -> 0, 257 -> 1, etc. + +The standard library includes families of methods for primitive numeric types to handle wrapping: +- `wrapping_*`: wrap in all modes +- `checked_*`: return None if overflow +- `overflowing_*`: return val + bool that indicates whether overflow occurred +- `saturating_*`: saturate at the minimum or maximum values + +[[Saturating a value when wrapping]] diff --git a/Items that are always imported by default in Rust are called the prelude.md b/Items that are always imported by default in Rust are called the prelude.md new file mode 100644 index 0000000..6a4fa2f --- /dev/null +++ b/Items that are always imported by default in Rust are called the prelude.md @@ -0,0 +1,5 @@ +[[Rust]] + +Every function and other thing that are always accessible in every Rust program are together called the `prelude`. + +What is included in the prelude is documented [here](https://doc.rust-lang.org/std/prelude/index.html) \ No newline at end of file diff --git a/Pattern matching with a match expression.md b/Pattern matching with a match expression.md new file mode 100644 index 0000000..affc752 --- /dev/null +++ b/Pattern matching with a match expression.md @@ -0,0 +1,13 @@ +[[Rust]] + +This looks like a regular function call but then followed by a block of code. It is called a match expression. A match expression consists of arms, that are the pattern to match and the code to execute. + +For example if a function returns an enum it is a nice way to write each case, it is somewhat similar to a switch statement. + +```rust +guess.cmp(&secret_number) { + Ordering::Less => println!("Too small!"), + Ordering::Greater => println!("Too big!"), + Ordering::Equal => println!("You win!!"), +} +``` diff --git a/Primitive values such as the array are allocated on the stack.md b/Primitive values such as the array are allocated on the stack.md new file mode 100644 index 0000000..c46ff60 --- /dev/null +++ b/Primitive values such as the array are allocated on the stack.md @@ -0,0 +1,3 @@ +[[Rust]] + +This may be useful to know when optimizing for speed. But for example arrays are limited (or more difficult) in use than vectors which are allocated on the heap instead. \ No newline at end of file diff --git a/Random numbers in Rust.md b/Random numbers in Rust.md new file mode 100644 index 0000000..85b4e59 --- /dev/null +++ b/Random numbers in Rust.md @@ -0,0 +1,3 @@ +[[Rust]] + +To do random numbers we can use the [rand crate](https://crates.io/crates/rand). \ No newline at end of file diff --git a/References are also immutable by default.md b/References are also immutable by default.md new file mode 100644 index 0000000..f434b63 --- /dev/null +++ b/References are also immutable by default.md @@ -0,0 +1,5 @@ +[[Rust]] + +--- + +Because references are also immutable by default, if we would declare a variable mutable such as `let mut guess = String::new();` and we were to pass a references to guess to another function, we must place the `mut` keyword there as well as such: `read_line(&mut guess)` rather than just `read_line(&guess)`. diff --git a/Rust.md b/Rust.md index e69de29..1733c8f 100644 --- a/Rust.md +++ b/Rust.md @@ -0,0 +1,17 @@ +[[Why it is good to learn rust]] +[[Cargo]] +[[Items that are always imported by default in Rust are called the prelude]] +[[Static class functions are called associated functions]] +[[References are also immutable by default]] +[[Random numbers in Rust]] +[[Pattern matching with a match expression]] +[[Shadowing variables]] +[[Integers can overflow if compiled with --release]] +[[Char is a unicode scalar value]] +[[Unlike integers floats do not have an architecture dependent size]] +[[Why is it called destructuring]] +[[Accessing a tuple by index is different from an array]] +[[An empty tuple is called a unit]] +[[Primitive values such as the array are allocated on the stack]] +[[Arrays are useful when you know the length will not change]] +[[Array notation]] diff --git a/Saturating a value when wrapping.md b/Saturating a value when wrapping.md new file mode 100644 index 0000000..2cc5bf9 --- /dev/null +++ b/Saturating a value when wrapping.md @@ -0,0 +1,12 @@ +[[Integers can overflow if compiled with --release]] +[[Rust]] + +This was a bit confusing to me, but [this forum post](https://users.rust-lang.org/t/saturating-what-does-it-really-do-and-when-is-it-useful/52720/2) sums it up quite well: + +Instead of wrapping around or error'ing, the maximum or minimum value of the type is chosen depending on which is closest to the result. + +Example with `u8`: +`0 - 1 = 0` +`7 - 8 = 0` +`8 + 1 = 8` +`1 + 8 = 8` \ No newline at end of file diff --git a/Shadowing variables.md b/Shadowing variables.md new file mode 100644 index 0000000..ce10d66 --- /dev/null +++ b/Shadowing variables.md @@ -0,0 +1,12 @@ +[[Rust]] + +It is basically redeclaring the same variable, but it can be redeclared with a **different type**. A immutable variable can also be shadowed. + +Shadowing is undone when the scope ends, for example in a closure I can shadow a variable from the parent scope, but in the parent scope after the child scope ends we have the original value not the one from the child scope. + +It is useful for reusing a name, ie: +```rust +let spaces = " "; +let spaces = spaces.len(); +``` +> the type of spaces changes from string to u32. diff --git a/Static class functions are called associated functions.md b/Static class functions are called associated functions.md new file mode 100644 index 0000000..dc4c195 --- /dev/null +++ b/Static class functions are called associated functions.md @@ -0,0 +1,6 @@ +[[Rust]] + +--- +An `associated function` is what in other OOP languages like PHP would be considered a static function on a class. So it's callable directly from on type rather than an object. An example in Rust is `String::new()`. + +It is indicated by the double colon `::`. \ No newline at end of file diff --git a/Unlike integers floats do not have an architecture dependent size.md b/Unlike integers floats do not have an architecture dependent size.md new file mode 100644 index 0000000..f6b962f --- /dev/null +++ b/Unlike integers floats do not have an architecture dependent size.md @@ -0,0 +1,5 @@ +[[Rust]] + +Integers come in many sizes with the smallest being 8-bit and the largest 128-bit. However there is also `usize or isize` which specifies that the size is dependent on the CPU architecture. + +In contrast, floating points only have two sizes: f32 and f64. There is no fsize. \ No newline at end of file diff --git a/Why is it called destructuring.md b/Why is it called destructuring.md new file mode 100644 index 0000000..6f8945c --- /dev/null +++ b/Why is it called destructuring.md @@ -0,0 +1,8 @@ +[[Rust]] + +Destructuring is binding each value in a compound type to a name. It is called destructuring because we break the compound element into individual elements. + +```rust +let tup = (1, 2, 3); +let (a, b, c) = tup; +``` diff --git a/Why it is good to learn rust.md b/Why it is good to learn rust.md new file mode 100644 index 0000000..c60bbc6 --- /dev/null +++ b/Why it is good to learn rust.md @@ -0,0 +1,5 @@ +[[Rust]] + +--- +I want to do game engine development and game development in my spare time +I can use it for systems programming too \ No newline at end of file -- cgit v1.2.3