diff options
author | Jasper Ras <jras@hostnet.nl> | 2025-08-05 08:53:21 +0200 |
---|---|---|
committer | Jasper Ras <jras@hostnet.nl> | 2025-08-05 08:53:21 +0200 |
commit | 295b343aecf330e830d79f06e0efc511e7d76da1 (patch) | |
tree | 85057ef1fe401adcd0d958ddf6ff966f25d8fef0 | |
parent | f43372529e655f3b039946c969e4465193eb2042 (diff) |
vault backup: 2025-08-05 08:53:21
30 files changed, 218 insertions, 2 deletions
diff --git a/.obsidian/appearance.json b/.obsidian/appearance.json index 9e26dfe..4be7969 100644 --- a/.obsidian/appearance.json +++ b/.obsidian/appearance.json @@ -1 +1,3 @@ -{}
\ No newline at end of file +{ + "theme": "obsidian" +}
\ No newline at end of file diff --git a/.obsidian/graph.json b/.obsidian/graph.json index 863f273..2dc66d1 100644 --- a/.obsidian/graph.json +++ b/.obsidian/graph.json @@ -17,6 +17,6 @@ "repelStrength": 10, "linkStrength": 1, "linkDistance": 500, - "scale": 0.04303687666455931, + "scale": 0.4828332660330065, "close": true }
\ No newline at end of file diff --git a/A cool tool that visualizes the borrow checker.md b/A cool tool that visualizes the borrow checker.md new file mode 100644 index 0000000..425e6d2 --- /dev/null +++ b/A cool tool that visualizes the borrow checker.md @@ -0,0 +1,5 @@ +[[Rust]] + +--- + +https://github.com/cognitive-engineering-lab/aquascope
\ No newline at end of file diff --git a/A place is anything that is valid to put on the left-hand side of an assignment.md b/A place is anything that is valid to put on the left-hand side of an assignment.md new file mode 100644 index 0000000..cd679e7 --- /dev/null +++ b/A place is anything that is valid to put on the left-hand side of an assignment.md @@ -0,0 +1,12 @@ +[[Borrow checker]] + +It is not THE left hand side of an assignment but anything that is valid to put there. But they can appear anywhere from what it looks like. + +It includes: +- variables `a` +- dereferences of places `*a` +- Array accesses of places `a[0]` +- Fields of places `a.0` +- Any combination of the above + +The borrow checker expects a place to have certain permissions depending on the operation.
\ No newline at end of file diff --git a/Accessed data from dereferencing a pointer cannot be mutated.md b/Accessed data from dereferencing a pointer cannot be mutated.md new file mode 100644 index 0000000..6fe3195 --- /dev/null +++ b/Accessed data from dereferencing a pointer cannot be mutated.md @@ -0,0 +1 @@ +[[Creating a reference to a variable is called borrowing]] diff --git a/Aliasing is accessing the same data through different variables.md b/Aliasing is accessing the same data through different variables.md new file mode 100644 index 0000000..16db4a3 --- /dev/null +++ b/Aliasing is accessing the same data through different variables.md @@ -0,0 +1,9 @@ +[[Rust]] +[[Ownership]] +[[Dereferencing is to access the data behind a pointer]] +[[Pointer safety principle]] + + +--- + +Mutating an alias is dangerous.
\ No newline at end of file diff --git a/Borrow checker.md b/Borrow checker.md new file mode 100644 index 0000000..09700ba --- /dev/null +++ b/Borrow checker.md @@ -0,0 +1,8 @@ +[[Rust]] +[[Ownership]] +[[Dereferencing is to access the data behind a pointer]] +[[Aliasing is accessing the same data through different variables]] +[[Variables are subject to permissions similar to files on linux]] +[[References temporarily remove permissions]] +[[Creating a reference to a variable is called borrowing]] +[[It is illegal to create a mutable reference while an immutable reference is live]] diff --git a/Box deallocation principle.md b/Box deallocation principle.md new file mode 100644 index 0000000..01418d4 --- /dev/null +++ b/Box deallocation principle.md @@ -0,0 +1,6 @@ +[[Rust principles]] +[[When boxes are deallocated]] + +--- + +If a variable owns a box, when Rust deallocates the variable’s frame, then Rust deallocates the box’s heap memory.
\ No newline at end of file diff --git a/Boxes live in the heap.md b/Boxes live in the heap.md new file mode 100644 index 0000000..ec296c9 --- /dev/null +++ b/Boxes live in the heap.md @@ -0,0 +1,15 @@ +[[Rust]] +[[Variables lives in the stack]] +[[When boxes are deallocated]] +[[Ownership]] + +--- + +The heap is where data can live indefinitely and we can create pointers to it on the stack using a Box. + +```rust +let a = Box::new([0; 1_000_000]); +let b = a; +``` + +Creates an array with 1 million entries on the heap and two variables on the stack that point to it.
\ No newline at end of file diff --git a/Cloning a box does a deep copy of the heap data.md b/Cloning a box does a deep copy of the heap data.md new file mode 100644 index 0000000..25c861d --- /dev/null +++ b/Cloning a box does a deep copy of the heap data.md @@ -0,0 +1,7 @@ +[[Rust]] +[[Ownership]] +[[Moved heap data principle]] + +--- + +To get around the issue of not being able to use a box after we moved ownership we can call .clone() on it. A clone is a deep copy of the heap data rather than just a shallow copy of the pointer to the original heap data.
\ No newline at end of file diff --git a/Creating a reference to a variable is called borrowing.md b/Creating a reference to a variable is called borrowing.md new file mode 100644 index 0000000..245d060 --- /dev/null +++ b/Creating a reference to a variable is called borrowing.md @@ -0,0 +1,13 @@ +[[Rust]] +[[References are also immutable by default]] +[[Mutable references are safe because they prevent aliasing]] +[[Ownership]] +[[Dereferencing is to access the data behind a pointer]] + +--- + +Creating a reference is done with an ampersand. It creates a pointer to another variable in the stack, which could be a pointer to something on the heap. + +This is called borrowing. + +A reference is a non-owning pointer; so if we create a reference to a Box we can safely have it discarded and continue using the box because the heap data is not deallocated.
\ No newline at end of file diff --git a/Dereferencing is to access the data behind a pointer.md b/Dereferencing is to access the data behind a pointer.md new file mode 100644 index 0000000..73cbeee --- /dev/null +++ b/Dereferencing is to access the data behind a pointer.md @@ -0,0 +1,10 @@ +[[Rust]] +[[Ownership]] +[[References are also immutable by default]] +[[Accessed data from dereferencing a pointer cannot be mutated]] + +--- + +The dereference operator is similar as in Go an asterisk. By dereferencing a pointer it is possible to access the data it points to. + +When you use the dereference operator in an assignment the variable has the same value as what the pointer holds.
\ No newline at end of file diff --git a/Downgrading a mutable reference.md b/Downgrading a mutable reference.md new file mode 100644 index 0000000..e1e61a2 --- /dev/null +++ b/Downgrading a mutable reference.md @@ -0,0 +1,10 @@ +[[Creating a reference to a variable is called borrowing]] + +--- + +It can be done: +```rust +let x: i32 = 1; <- R - O +let y: &mut x; <- R W O +let z: &*y; <- R - O +```
\ No newline at end of file diff --git a/Go Strings.md b/Go Strings.md index 51eab51..26aed7b 100644 --- a/Go Strings.md +++ b/Go Strings.md @@ -1,4 +1,5 @@ [[Golang]] +[[Char is a unicode scalar value]] --- diff --git a/It is illegal to create a mutable reference while an immutable reference is live.md b/It is illegal to create a mutable reference while an immutable reference is live.md new file mode 100644 index 0000000..a201199 --- /dev/null +++ b/It is illegal to create a mutable reference while an immutable reference is live.md @@ -0,0 +1,2 @@ + +|
\ No newline at end of file diff --git a/Lifetime specifier.md b/Lifetime specifier.md new file mode 100644 index 0000000..b1c5caa --- /dev/null +++ b/Lifetime specifier.md @@ -0,0 +1,3 @@ +[[Borrow checker]] + +A flow permission cannot be given if a function signature states that it returns a reference but the function has multiple returns referencing potentially different variables so the borrow checker doesn't know if it's safe or not.
\ No newline at end of file diff --git a/Moved heap data principle.md b/Moved heap data principle.md new file mode 100644 index 0000000..a25f60a --- /dev/null +++ b/Moved heap data principle.md @@ -0,0 +1,6 @@ +[[Rust principles]] +[[Ownership]] + +--- + +If a variable `x` moves ownership of heap data to another variable `y`, then `x` cannot be used after the move.
\ No newline at end of file diff --git a/Mutable references are safe because they prevent aliasing.md b/Mutable references are safe because they prevent aliasing.md new file mode 100644 index 0000000..bb837f7 --- /dev/null +++ b/Mutable references are safe because they prevent aliasing.md @@ -0,0 +1,7 @@ +[[Pointer safety principle]] +[[Aliasing is accessing the same data through different variables]] +[[Downgrading a mutable reference]] + +--- + +A reference that is mutable is created with `&mut` . They are safe because all permissions are dropped from the borrowed place so it can not be read while the reference exists. It is therefore not an alias.
\ No newline at end of file diff --git a/Ownership.md b/Ownership.md new file mode 100644 index 0000000..5484772 --- /dev/null +++ b/Ownership.md @@ -0,0 +1,14 @@ +[[Rust]] +[[When boxes are deallocated]] +[[Moved heap data principle]] +[[Creating a reference to a variable is called borrowing]] + + +--- + +When we assign a Box to a name we say that name owns the box. Any time we copy the pointer to that box to another name we also pass ownership to that name. + +After we pass ownership and move into a new scope the original variable is freed. Thus when we return to the original scope it is no longer possible to use it. + +![[Pasted image 20250804184242.png]] +> Wherever the stack variable is greyed out means it is no longer the owner.
\ No newline at end of file diff --git a/Pasted image 20250804184242.png b/Pasted image 20250804184242.png Binary files differnew file mode 100644 index 0000000..2201b83 --- /dev/null +++ b/Pasted image 20250804184242.png diff --git a/Pointer safety principle.md b/Pointer safety principle.md new file mode 100644 index 0000000..d47520c --- /dev/null +++ b/Pointer safety principle.md @@ -0,0 +1,8 @@ +[[Rust principles]] +[[Borrow checker]] + +--- + +Data should never be aliased and mutated at the same time. + +Boxes are not allowed to be aliased. References are meant to be aliased: the pointer safety principle is ensured via the borrow checker.
\ No newline at end of file diff --git a/References are also immutable by default.md b/References are also immutable by default.md index f434b63..523f4d5 100644 --- a/References are also immutable by default.md +++ b/References are also immutable by default.md @@ -1,4 +1,5 @@ [[Rust]] +[[Mutable references are safe because they prevent aliasing]] --- diff --git a/References temporarily remove permissions.md b/References temporarily remove permissions.md new file mode 100644 index 0000000..817b801 --- /dev/null +++ b/References temporarily remove permissions.md @@ -0,0 +1,8 @@ +[[Variables are subject to permissions similar to files on linux]] +[[Creating a reference to a variable is called borrowing]] +[[Pointer safety principle]] +[[References are also immutable by default]] + +--- + +Borrowing from a variable temporarily removes its write and own permissions. Thus guaranteeing the safety principle.
\ No newline at end of file diff --git a/Rust principles.md b/Rust principles.md new file mode 100644 index 0000000..dc39b12 --- /dev/null +++ b/Rust principles.md @@ -0,0 +1,4 @@ +[[Rust]] + +[[Box deallocation principle]] +[[Moved heap data principle]] @@ -1,4 +1,6 @@ [[Why it is good to learn rust]] +[[A cool tool that visualizes the borrow checker]] +[[Rust principles]] [[Cargo]] [[Items that are always imported by default in Rust are called the prelude]] [[Static class functions are called associated functions]] @@ -23,3 +25,11 @@ [[If is not a statement but an expression]] [[Returning values from loops]] [[Loop over an array with for-in]] +[[Undefined behaviour is caught by the compiler]] +[[Variables lives in the stack]] +[[Boxes live in the heap]] +[[Cloning a box does a deep copy of the heap data]] +[[Aliasing is accessing the same data through different variables]] +[[Borrow checker]] +[[A place is anything that is valid to put on the left-hand side of an assignment]] + diff --git a/Undefined behaviour is caught by the compiler.md b/Undefined behaviour is caught by the compiler.md new file mode 100644 index 0000000..62adbff --- /dev/null +++ b/Undefined behaviour is caught by the compiler.md @@ -0,0 +1,10 @@ +[[Rust]] +[[What is a segmentation fault]] + +Undefined behaviour is prevented by compiler checks. For example we can't use an undefined variable in a function call. If that were unchecked it's behaviour is undefined: +- it could cause a segvault +- it could cause no harm +- it could become the target of a hack + +A list of behaviours considered undefined: +https://doc.rust-lang.org/reference/behavior-considered-undefined.html
\ No newline at end of file diff --git a/Variables are subject to permissions similar to files on linux.md b/Variables are subject to permissions similar to files on linux.md new file mode 100644 index 0000000..5a47739 --- /dev/null +++ b/Variables are subject to permissions similar to files on linux.md @@ -0,0 +1,13 @@ +[[Rust]] +[[A place is anything that is valid to put on the left-hand side of an assignment]] +[[Lifetime specifier]] + +--- + +Rust has a permission system similar to file permissions for variables: +- R: read: data can be copied +- W: write: data can be mutated +- O: own: data can be moved or dropped +- F: Flow: expected when a function input uses a reference or when a function returns a reference + +The default is : "RO". `mut` adds "W".
\ No newline at end of file diff --git a/Variables lives in the stack.md b/Variables lives in the stack.md new file mode 100644 index 0000000..a929a67 --- /dev/null +++ b/Variables lives in the stack.md @@ -0,0 +1,19 @@ +[[Rust]] +[[Boxes live in the heap]] + + +--- + +The stack consists of frames. + +Frames are just mappings of names to values within a single scope, they are tightly coupled to a specific function + +Freeing is the act of discarding such a frame. + +If we assign a variable such as +```rust +let a = [0; 1_000_000]; +let b = a; +``` + +the value of a is **copied** to b (thus we get two huge arrays). This is **expensive**. Think about what happens when we call a function.
\ No newline at end of file diff --git a/What is a segmentation fault.md b/What is a segmentation fault.md new file mode 100644 index 0000000..63dac0a --- /dev/null +++ b/What is a segmentation fault.md @@ -0,0 +1,4 @@ +[[Linux]] +[[Rust]] + +A segmentation fault is raised by the kernel when a program attempts to access a segment of memory that does not belong to the process or for example is not writable and it tries to write there.
\ No newline at end of file diff --git a/When boxes are deallocated.md b/When boxes are deallocated.md new file mode 100644 index 0000000..96c2c34 --- /dev/null +++ b/When boxes are deallocated.md @@ -0,0 +1,8 @@ +[[Rust]] +[[Boxes live in the heap]] +[[Ownership]] + + +--- + +A box's heap memory is freed automatically when the frame of the variable who owns the box is freed.
\ No newline at end of file |