summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJasper Ras <jras@hostnet.nl>2025-08-04 08:46:49 +0200
committerJasper Ras <jras@hostnet.nl>2025-08-04 08:46:49 +0200
commit00a52e3d631df0a7610f4f17a9bd6d60205239fd (patch)
tree99aac9c4c78c2ec693d7800a946fd6da7a0ea597
parent931eb0894e7cd3717f1218f0eb06382b11734c13 (diff)
vault backup: 2025-08-04 08:46:49
-rw-r--r--A scope block is an expression.md13
-rw-r--r--Adding a semicolon to the end of a line turns it into a statement.md4
-rw-r--r--How to return from a function.md6
-rw-r--r--If is not a statement but an expression.md4
-rw-r--r--It is not possible to do a chain assignment.md7
-rw-r--r--Loop labels.md27
-rw-r--r--Loop over an array with for-in.md7
-rw-r--r--Returning values from loops.md23
-rw-r--r--Rust.md8
-rw-r--r--Statement vs expression.md9
-rw-r--r--There is no truthiness.md13
11 files changed, 121 insertions, 0 deletions
diff --git a/A scope block is an expression.md b/A scope block is an expression.md
new file mode 100644
index 0000000..29f6214
--- /dev/null
+++ b/A scope block is an expression.md
@@ -0,0 +1,13 @@
+[[Rust]]
+
+An expression evaluates to a value. In Rust that can also mean a scope block as follows:
+```rust
+fn main() {
+ let y = {
+ let x = 3;
+ x + 1
+ };
+
+ println!("The value of y is: {y}");
+}
+``` \ No newline at end of file
diff --git a/Adding a semicolon to the end of a line turns it into a statement.md b/Adding a semicolon to the end of a line turns it into a statement.md
new file mode 100644
index 0000000..e9f4bf1
--- /dev/null
+++ b/Adding a semicolon to the end of a line turns it into a statement.md
@@ -0,0 +1,4 @@
+[[Rust]]
+[[How to return from a function]]
+
+Any line that ends with a semicolon is a statement, thus it returns no value. A line ending without a semicolon is an expression that returns a value. \ No newline at end of file
diff --git a/How to return from a function.md b/How to return from a function.md
new file mode 100644
index 0000000..0fde1fc
--- /dev/null
+++ b/How to return from a function.md
@@ -0,0 +1,6 @@
+[[Rust]]
+[[Adding a semicolon to the end of a line turns it into a statement]]
+
+In Rust we can return from a function with simply an expression at the end of the scope.
+
+The `return` keyword is mostly used for returning early. \ No newline at end of file
diff --git a/If is not a statement but an expression.md b/If is not a statement but an expression.md
new file mode 100644
index 0000000..4ece748
--- /dev/null
+++ b/If is not a statement but an expression.md
@@ -0,0 +1,4 @@
+[[Rust]]
+[[Statement vs expression]]
+
+An if statement is not a statement in rust! It is an expression! Thus we can use it in an assignment for example.
diff --git a/It is not possible to do a chain assignment.md b/It is not possible to do a chain assignment.md
new file mode 100644
index 0000000..419c5dc
--- /dev/null
+++ b/It is not possible to do a chain assignment.md
@@ -0,0 +1,7 @@
+[[Rust]]
+[[Statement vs expression]]
+
+Because a statement does not have a return value it is not possible to do
+`let x = (let y = 6)`
+
+Also `let x = y = 6;` is not possible because now `y` is never defined. \ No newline at end of file
diff --git a/Loop labels.md b/Loop labels.md
new file mode 100644
index 0000000..2110c71
--- /dev/null
+++ b/Loop labels.md
@@ -0,0 +1,27 @@
+[[Rust]]
+[[Returning values from loops]]
+
+In nested loops we can use labels to determine which one to break from:
+```rust
+fn main() {
+ let mut count = 0;
+ 'counting_up: loop {
+ println!("count = {count}");
+ let mut remaining = 10;
+
+ loop {
+ println!("remaining = {remaining}");
+ if remaining == 9 {
+ break;
+ }
+ if count == 2 {
+ break 'counting_up;
+ }
+ remaining -= 1;
+ }
+
+ count += 1;
+ }
+ println!("End count = {count}");
+}
+``` \ No newline at end of file
diff --git a/Loop over an array with for-in.md b/Loop over an array with for-in.md
new file mode 100644
index 0000000..edeacfb
--- /dev/null
+++ b/Loop over an array with for-in.md
@@ -0,0 +1,7 @@
+[[Rust]]
+
+```rust
+for number in (1..4).rev() {
+ println!("reversed: {number}");
+}
+``` \ No newline at end of file
diff --git a/Returning values from loops.md b/Returning values from loops.md
new file mode 100644
index 0000000..7f0b116
--- /dev/null
+++ b/Returning values from loops.md
@@ -0,0 +1,23 @@
+[[Rust]]
+[[A scope block is an expression]]
+[[Loop labels]]
+
+
+A loop can return values, it is an expression. Thus it can be used in an assignment as well.
+It is possible to `return ` but that would exit the function, instead we can also `break expression;` and return a value just from the loop by breaking it with a value (as we would normally write a `return`).
+
+```rust
+fn main() {
+ let mut counter = 0;
+
+ let result = loop {
+ counter += 1;
+
+ if counter == 10 {
+ break counter * 2;
+ }
+ };
+
+ println!("The result is {result}");
+}
+```
diff --git a/Rust.md b/Rust.md
index 1733c8f..39825ee 100644
--- a/Rust.md
+++ b/Rust.md
@@ -15,3 +15,11 @@
[[Primitive values such as the array are allocated on the stack]]
[[Arrays are useful when you know the length will not change]]
[[Array notation]]
+[[A scope block is an expression]]
+[[Adding a semicolon to the end of a line turns it into a statement]]
+[[How to return from a function]]
+[[Statement vs expression]]
+[[There is no truthiness]]
+[[If is not a statement but an expression]]
+[[Returning values from loops]]
+[[Loop over an array with for-in]]
diff --git a/Statement vs expression.md b/Statement vs expression.md
new file mode 100644
index 0000000..e899857
--- /dev/null
+++ b/Statement vs expression.md
@@ -0,0 +1,9 @@
+[[Rust]]
+[[A scope block is an expression]]
+[[How to return from a function]]
+[[Adding a semicolon to the end of a line turns it into a statement]]
+[[It is not possible to do a chain assignment]]
+[[An empty tuple is called a unit]]
+
+An expression returns something, ie `1 + 2` returns `3`.
+A statement on the other hand does not return anything: as assignment is a statement. It equals to the unit! \ No newline at end of file
diff --git a/There is no truthiness.md b/There is no truthiness.md
new file mode 100644
index 0000000..a573369
--- /dev/null
+++ b/There is no truthiness.md
@@ -0,0 +1,13 @@
+[[Rust]]
+
+It does not work to write something like:
+```rust
+let number = 3;
+
+if number {
+// do something
+}
+```
+like in other languages
+
+Rust expects only a `bool` in an if statement, so we'd need to compare it. \ No newline at end of file