mirror of
https://github.com/TrudeEH/web.git
synced 2025-12-06 08:23:37 +00:00
Misc fixes; Update index; Add meson to compiling note
This commit is contained in:
@@ -8,21 +8,19 @@ author: TrudeEH
|
||||
showToc: true
|
||||
---
|
||||
|
||||
## Bash Language
|
||||
|
||||
### Strings
|
||||
## Strings
|
||||
|
||||
- `""` Defines a string which supports substitutions (`$` and `\`, for example).
|
||||
- `''` Defines a string, but preserves its actual value (substitutions are treated as regular characters).
|
||||
- [ANSI Escape Sequences](c-language.md#ANSI%20Escape%20Sequences) apply when using `""`.
|
||||
|
||||
### Comments
|
||||
## Comments
|
||||
|
||||
```bash
|
||||
# comment
|
||||
```
|
||||
|
||||
### Commands
|
||||
## Commands
|
||||
|
||||
A shell command consists of the command itself, followed by its arguments.
|
||||
|
||||
@@ -32,7 +30,7 @@ command "arg1" "arg2"
|
||||
|
||||
If the first word of a command is a reserved word, bash handles the command, otherwise, it searches for an executable on the system's `$PATH`, a list of directories where a binary could be located.
|
||||
|
||||
#### Reserved Words
|
||||
### Reserved Words
|
||||
|
||||
| | | | | | |
|
||||
|---|---|---|---|---|---|
|
||||
@@ -41,16 +39,16 @@ If the first word of a command is a reserved word, bash handles the command, oth
|
||||
|`case`|`esac`|`coproc`|`select`|`function`|
|
||||
|`{`|`}`|`[[`|`]]`|`!`|
|
||||
|
||||
### List of Commands
|
||||
## List of Commands
|
||||
|
||||
- `command1 ; command2` Execute command2 after command1, sequentially.
|
||||
- `command1 &` Execute command1 asynchronously in a subshell.
|
||||
- `command1 && command2` *AND*: Only execute command2 if command1 returns 0 (success).
|
||||
- `command1 || command2` *OR*: Only execute command2 if command1 returns a non-zero exit value (failure).
|
||||
|
||||
### Loops
|
||||
## Loops
|
||||
|
||||
#### `until`
|
||||
### `until`
|
||||
|
||||
```bash
|
||||
until test-commands; do
|
||||
@@ -60,7 +58,7 @@ done
|
||||
|
||||
Execute the code in `...` for as long as `test-commands` return non-zero.
|
||||
|
||||
#### `while`
|
||||
### `while`
|
||||
|
||||
```bash
|
||||
while test-commands; do
|
||||
@@ -70,11 +68,11 @@ done
|
||||
|
||||
Execute `...` for as long as `test-commands` return 0.
|
||||
|
||||
#### `for`
|
||||
### `for`
|
||||
|
||||
Expand `words` and execute `...` for each member in the resultant list, with `name` bound to the current member.
|
||||
|
||||
##### Iterate through List
|
||||
#### Iterate through List
|
||||
|
||||
```bash
|
||||
for item in list; do
|
||||
@@ -82,7 +80,7 @@ for item in list; do
|
||||
done
|
||||
```
|
||||
|
||||
##### C-like Loop
|
||||
#### C-like Loop
|
||||
|
||||
```bash
|
||||
for (( i=1; i<=10; i++ )); do
|
||||
@@ -90,7 +88,7 @@ for (( i=1; i<=10; i++ )); do
|
||||
done
|
||||
```
|
||||
|
||||
##### Infinite Loop
|
||||
#### Infinite Loop
|
||||
|
||||
```bash
|
||||
for (( ; ; )); do
|
||||
@@ -98,9 +96,9 @@ for (( ; ; )); do
|
||||
done
|
||||
```
|
||||
|
||||
### Conditional Constructs
|
||||
## Conditional Constructs
|
||||
|
||||
#### `if`
|
||||
### `if`
|
||||
|
||||
```bash
|
||||
if test-commands; then
|
||||
@@ -115,7 +113,7 @@ fi
|
||||
Execute the first `...` if `test-commands` returns 0, and evaluate the next condition otherwise. This process repeats until `else` is found, or one of the `tests` evaluates to a 0.
|
||||
Once any `...` executes, the remaining `if` construct is skipped.
|
||||
|
||||
#### `case`
|
||||
### `case`
|
||||
|
||||
```bash
|
||||
case word in
|
||||
@@ -132,7 +130,7 @@ The `|` operator separates multiple patterns, and each clause can end with `;;`,
|
||||
|
||||
Using `;&` instead of `;;` would cause the next `...` to be executed as well, and `;;&` would test the next clause, instead of immediately exiting.
|
||||
|
||||
#### `select`
|
||||
### `select`
|
||||
|
||||
```bash
|
||||
PS3="Enter a number: "
|
||||
@@ -157,29 +155,29 @@ Output:
|
||||
Enter a number:
|
||||
```
|
||||
|
||||
#### `((...))`
|
||||
### `((...))`
|
||||
|
||||
The arithmetic expression is evaluated according to the rules described below (see [Shell Arithmetic]() TODO link to shell arithmetic).
|
||||
|
||||
#### `[[...]]`
|
||||
### `[[...]]`
|
||||
|
||||
Return a status of 0 or 1 depending on the evaluation of the conditional expression expression. Expressions are composed of the primaries described below in [Bash Conditional Expressions](https://www.gnu.org/software/bash/manual/bash.html#Bash-Conditional-Expressions).
|
||||
|
||||
#### Combine Expressions
|
||||
### Combine Expressions
|
||||
|
||||
- `( expression )` Returns the value of expression. (Can be used to override precedence).
|
||||
- `! expression` *NOT* an expression. (`true` if expression is `false`).
|
||||
- `exp1 && exp2` *AND* - `true` if both expressions are `true`.
|
||||
- `exp1 || exp2` *OR* - `true` if either expressions are `true`.
|
||||
|
||||
#### Grouping Commands
|
||||
### Grouping Commands
|
||||
|
||||
Bash allows for commands to be grouped as a single unit. That way, if the group is redirected, the output of every command in the list is passed to a single stream.
|
||||
|
||||
- `( list )` Create a subshell (variables created inside it can't be accessed outside).
|
||||
- `{ list; }` No subshell is created.
|
||||
|
||||
### Functions
|
||||
## Functions
|
||||
|
||||
```bash
|
||||
fname() {
|
||||
@@ -199,7 +197,7 @@ fname
|
||||
|
||||
Any variables defined inside the function
|
||||
|
||||
#### Arguments
|
||||
### Arguments
|
||||
|
||||
```bash
|
||||
fname() {
|
||||
@@ -209,7 +207,7 @@ fname() {
|
||||
fname "a" "b"
|
||||
```
|
||||
|
||||
#### Scope
|
||||
### Scope
|
||||
|
||||
```bash
|
||||
var1='A'
|
||||
@@ -228,7 +226,7 @@ echo "var1: $var1, var2: $var2" # A, D
|
||||
|
||||
Defining a variable inside the function overwrites the global scope. To prevent this, use the `local` keyword.
|
||||
|
||||
#### `return`
|
||||
### `return`
|
||||
|
||||
```bash
|
||||
fname() {
|
||||
@@ -241,7 +239,7 @@ echo $? # 1
|
||||
|
||||
Use the `return` command to exit the function and return a value.
|
||||
|
||||
### Variables (Parameters)
|
||||
## Variables (Parameters)
|
||||
|
||||
```bash
|
||||
name="Trude"
|
||||
@@ -254,7 +252,7 @@ echo ${name}
|
||||
|
||||
Variables can be of any type, and grow to any needed size.
|
||||
|
||||
#### Special Variables
|
||||
### Special Variables
|
||||
|
||||
- `$*` Expands to every positional parameter: `$1$2$3`.
|
||||
- `$@` Expands to every positional parameter, separated by spaces: `"$1" "$2" "$3"`.
|
||||
@@ -265,21 +263,21 @@ Variables can be of any type, and grow to any needed size.
|
||||
- `$!` Process ID of the latest job placed into the background.
|
||||
- `$0` Name of the shell or script.
|
||||
|
||||
### Shell Expansions
|
||||
## Shell Expansions
|
||||
|
||||
#### Brace Expansion
|
||||
### Brace Expansion
|
||||
|
||||
```bash
|
||||
echo a{d,c,b}e # ade ace abe
|
||||
```
|
||||
|
||||
#### Tilde Expansion
|
||||
### Tilde Expansion
|
||||
|
||||
- `~` = `$HOME`
|
||||
- `~+` = `$PWD`
|
||||
- `~-` = `$OLDPWD`
|
||||
|
||||
#### Shell Parameter Expansion
|
||||
### Shell Parameter Expansion
|
||||
|
||||
- `${var}` Braces are required if the variable is positional and over one digit, or if it is followed by a character that is not part of its name.
|
||||
- `${!var}` Access the value of `var`, and checks if it is the name of another variable. If so, expands that variable. (Pointer)
|
||||
@@ -316,7 +314,7 @@ echo a{d,c,b}e # ade ace abe
|
||||
- `a` Output the flags corresponding to `var`'s attributes.
|
||||
- `k` Same as `K`, but separates keys and values using spaces, making it easier to loop through them.
|
||||
|
||||
#### Command Substitution
|
||||
### Command Substitution
|
||||
|
||||
```bash
|
||||
echo "$(command)"
|
||||
@@ -325,7 +323,7 @@ echo "`command`"
|
||||
|
||||
Execute a command and substitute itself with the command's result.
|
||||
|
||||
#### Arithmetic Expansion
|
||||
### Arithmetic Expansion
|
||||
|
||||
```bash
|
||||
echo "$(( expression ))"
|
||||
@@ -333,7 +331,7 @@ echo "$(( expression ))"
|
||||
|
||||
Performs an arithmetic expression and substitutes itself with the result.
|
||||
|
||||
#### Process Substitution
|
||||
### Process Substitution
|
||||
|
||||
```bash
|
||||
cat <(command) >(command)
|
||||
@@ -347,7 +345,7 @@ The `>()` substitution is also executed asynchronously, and creates a temporary
|
||||
|
||||
> Neither `cat` nor `gzip` are `bash` commands ('builtins'), but external programs.
|
||||
|
||||
#### Pattern Matching
|
||||
### Pattern Matching
|
||||
|
||||
- `*` Matches any string.
|
||||
- `?` Matches any single character.
|
||||
@@ -358,7 +356,7 @@ The `>()` substitution is also executed asynchronously, and creates a temporary
|
||||
- `@(pattern-list)` Matches one of the given patterns.
|
||||
- `!(pattern-list)` Matches anything except the given patterns.
|
||||
|
||||
### Redirections
|
||||
## Redirections
|
||||
|
||||
- `command > dest` Redirect the output of a command to a destination: A file, device, command, list, etc.
|
||||
- `command >> dest` Append to the destination instead of overwriting.
|
||||
@@ -379,12 +377,12 @@ string
|
||||
EOF
|
||||
```
|
||||
|
||||
### Shell Builtin Commands
|
||||
## Shell Builtin Commands
|
||||
|
||||
This section is an introduction to every command available in `bash`.
|
||||
To learn more about some command, run `help command`.
|
||||
|
||||
#### Bourne Shell Commands
|
||||
### Bourne Shell Commands
|
||||
|
||||
- `: arguments` Do nothing beyond expanding arguments and performing redirections.
|
||||
- `. file` Read and execute commands from `file`.
|
||||
@@ -419,7 +417,7 @@ To learn more about some command, run `help command`.
|
||||
- `umask` Defines which permissions should be removed from newly created files.
|
||||
- `unset` Remove a variable or function name. (Use `-f` to remove the actual function definition)
|
||||
|
||||
#### Bash Commands
|
||||
### Bash Commands
|
||||
|
||||
- `alias` Prints the list of aliases or defines new ones (with `alias name=value`).
|
||||
- `bind` Displays or sets key and function bindings for Readline.
|
||||
@@ -444,9 +442,9 @@ To learn more about some command, run `help command`.
|
||||
- `ulimit` Controls resource limits for processes created by the shell.
|
||||
- `unalias` Removes defined aliases, with an option to remove all.
|
||||
|
||||
### Shell Variables
|
||||
## Shell Variables
|
||||
|
||||
#### Bourne Shell Variables
|
||||
### Bourne Shell Variables
|
||||
|
||||
- `CDPATH` Search path directories for the `cd` command.
|
||||
- `HOME` Current user's home directory, default for `cd`.
|
||||
@@ -459,7 +457,7 @@ To learn more about some command, run `help command`.
|
||||
- `PS1` Primary prompt string displayed interactively.
|
||||
- `PS2` Secondary prompt string for continued commands.
|
||||
|
||||
#### Bash Variables
|
||||
### Bash Variables
|
||||
|
||||
- `_` Pathname of invoked shell/script, or last argument of previous command.
|
||||
- `BASH` Full pathname used to execute the current Bash instance.
|
||||
@@ -559,7 +557,7 @@ To learn more about some command, run `help command`.
|
||||
- `TMPDIR` Directory used for creating temporary files.
|
||||
- `UID` Numeric real user ID of the current user (readonly).
|
||||
|
||||
### Parse Arguments
|
||||
## Parse Arguments
|
||||
|
||||
```bash
|
||||
while getopts "ab:c" opt; do
|
||||
@@ -673,5 +671,21 @@ Arithmetic is performed using `(())`, `let` and `declare -i`.
|
||||
- `=` `*=` `/=` `%=` `+=` `-=` `<<=` `>>=` `&=` `^=` `|=` Assignment
|
||||
- `expr1 , expr2` Comma
|
||||
|
||||
## Arrays
|
||||
|
||||
### Indexed arrays
|
||||
|
||||
```bash
|
||||
declare -a name
|
||||
name[]=value
|
||||
```
|
||||
|
||||
### Associative arrays
|
||||
|
||||
```bash
|
||||
declare -A name
|
||||
name=(value1 value2 ...)
|
||||
```
|
||||
|
||||
6.7 Arrays
|
||||
https://www.gnu.org/software/bash/manual/bash.html#Bash-Features
|
||||
https://www.gnu.org/software/bash/manual/bash.html#Bash-Features
|
||||
@@ -1,50 +1,133 @@
|
||||
---
|
||||
title: "rust"
|
||||
title: Rust
|
||||
description:
|
||||
draft: true
|
||||
tags:
|
||||
author: TrudeEH
|
||||
showToc: true
|
||||
summary:
|
||||
---
|
||||
|
||||
## Vocabulary
|
||||
|
||||
|Command / Word|Action / Meaning|Example|
|
||||
|---|---|---|
|
||||
|Statement|Performs an action, but does not return a value.|Function definitions, code that ends with `;`.|
|
||||
|Expression|Evaluate to a resultant value.|Tests, math.|
|
||||
|
||||
## Tools
|
||||
|
||||
- Install Rust: `curl --proto '=https' --tlsv1.2 -sSf <https://sh.rustup.rs> | sh`
|
||||
- `rustup`
|
||||
- `rustc`
|
||||
- `cargo`
|
||||
- Install Rust: `curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh`
|
||||
- `rustup`
|
||||
- `rustc`
|
||||
- `cargo`
|
||||
|
||||
## Hello World!
|
||||
## Hello World
|
||||
|
||||
```rust
|
||||
fn main() {
|
||||
println!("Hello world!");
|
||||
println!("Hello world!"); // Macro to print text
|
||||
}
|
||||
```
|
||||
|
||||
## Variables
|
||||
## Comments
|
||||
|
||||
- *Regular comments* which are ignored by the compiler:
|
||||
- `// Line comments which go to the end of the line.`
|
||||
- `/* Block comments which go to the closing delimiter. */`
|
||||
- *Doc comments* which are parsed into HTML library documentation:
|
||||
- `/// Generate library docs for the following item.`
|
||||
- `//! Generate library docs for the enclosing item.`
|
||||
|
||||
## Formatted Print
|
||||
|
||||
## Primitives (Variables)
|
||||
|
||||
### Scalar Types
|
||||
|
||||
- Signed integers (default to `i32`): `i8`, `i16`, `i32`, `i64`, `i128` and `isize` (pointer size)
|
||||
- Unsigned integers: `u8`, `u16`, `u32`, `u64`, `u128` and `usize` (pointer size)
|
||||
- Floating point (default to `f64`): `f32`, `f64`
|
||||
- `char` Unicode scalar values like `'a'`, `'α'` and `'∞'` (4 bytes each)
|
||||
- `bool` either `true` or `false`
|
||||
- The unit type `()`, whose only possible value is an empty tuple: `()`
|
||||
|
||||
### Compound Types
|
||||
|
||||
- Arrays: `[1, 2, 3]`
|
||||
- Tuples: `(1, true)`
|
||||
|
||||
### Examples
|
||||
|
||||
Variables can either be *type annotated*, or infered by context. By default, a variable is always constant, and can be made mutable with the `mut` keyword. After creating a variable, its data type cannot be changed.
|
||||
|
||||
```rust
|
||||
let x: i32; // A variable can only be used if it has been initialized (contains a value)
|
||||
|
||||
let y: i8 = 5; // All variables are constant by default.
|
||||
let mut z = 1; // The mut keyword makes the variable mutable. (Explicit type annotation is not necessary, but recommended).
|
||||
|
||||
let (k, f); //Same as "let k; let f;"
|
||||
|
||||
let t = { // Initialize a variable as the result of an expression.
|
||||
let squared = y * y;
|
||||
squared
|
||||
};
|
||||
fn main() {
|
||||
let x: i32; // Declare a variable
|
||||
|
||||
let a_float: f64 = 1.0; // Regular annotation
|
||||
let an_integer = 5i32; // Suffix annotation
|
||||
|
||||
let default_float = 3.0; // `f64`
|
||||
let default_integer = 7; // `i32`
|
||||
|
||||
// A type can also be inferred from context.
|
||||
let mut inferred_type = 12; // Type i64 is inferred from another line.
|
||||
inferred_type = 4294967296i64;
|
||||
|
||||
let mut mutable = 12; // Mutable `i32`
|
||||
mutable = 21;
|
||||
|
||||
// Variables can be overwritten with shadowing.
|
||||
let mutable = true;
|
||||
|
||||
// Array signature consists of Type T and length as [T; length].
|
||||
let my_array: [i32; 5] = [1, 2, 3, 4, 5];
|
||||
|
||||
// Tuple is a collection of values of different types
|
||||
// and is constructed using parentheses ().
|
||||
let my_tuple = (5u32, 1u8, true, -5.04f32);
|
||||
|
||||
let (k, f); //Same as "let k; let f;"
|
||||
|
||||
let t = { // Initialize a variable as the result of an expression.
|
||||
let squared = y * y;
|
||||
squared
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
> A variable can only be used after it has been initialized (contains a value).
|
||||
|
||||
### Literals and Operators
|
||||
|
||||
Integers can also be expressed using hexadecimal `0x`, octal `0o` or binary `0b`.
|
||||
To improve readability, `_` can be added to numbers: `1_000` is the same as `1000`.
|
||||
Scientific e-notation such as `1e6`, `7.4e-4` is also supported, and defaults to `f64`.
|
||||
|
||||
The operators available and their precedence are similar to `C`:
|
||||
|
||||
```Rust
|
||||
fn main() {
|
||||
println!("1 + 2 = {}", 1u32 + 2); // Integer addition
|
||||
println!("1 - 2 = {}", 1i32 - 2); // Integer subtraction
|
||||
// Changing `1i32` to `1u32` would causa an integer underflow.
|
||||
|
||||
// Scientific notation
|
||||
println!("1e4 is {}, -2.5e-3 is {}", 1e4, -2.5e-3);
|
||||
|
||||
// Short-circuiting boolean logic
|
||||
println!("true AND false is {}", true && false);
|
||||
println!("true OR false is {}", true || false);
|
||||
println!("NOT true is {}", !true);
|
||||
|
||||
// Bitwise operations
|
||||
println!("0011 AND 0101 is {:04b}", 0b0011u32 & 0b0101);
|
||||
println!("0011 OR 0101 is {:04b}", 0b0011u32 | 0b0101);
|
||||
println!("0011 XOR 0101 is {:04b}", 0b0011u32 ^ 0b0101);
|
||||
println!("1 << 5 is {}", 1u32 << 5);
|
||||
println!("0x80 >> 2 is 0x{:x}", 0x80u32 >> 2);
|
||||
|
||||
// Use underscores to improve readability!
|
||||
println!("One million is written as {}", 1_000_000u32);
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Data Types
|
||||
|
||||
### Integer Types
|
||||
|
||||
Reference in New Issue
Block a user