mirror of
https://github.com/TrudeEH/web.git
synced 2025-12-06 08:23:37 +00:00
[BREAKING] Simplify URL paths
This commit is contained in:
47
content/drafts/assembly.md
Normal file
47
content/drafts/assembly.md
Normal file
@@ -0,0 +1,47 @@
|
||||
---
|
||||
tags:
|
||||
- computer-science
|
||||
- notes
|
||||
author: TrudeEH
|
||||
draft: true
|
||||
showToc: true
|
||||
title: Assembly
|
||||
---
|
||||
|
||||
#todo
|
||||
|
||||
## Assembler/Compiler
|
||||
|
||||
- `gcc` GNU C Compiler (The package includes `as` (assembler) and `ld` (linker))
|
||||
|
||||
```Shell
|
||||
as <file.asm> -o <output.o> # Assemble
|
||||
gcc -o <output_file> <output.o> -nostdlib -static # Link using gcc
|
||||
ld <output.o> # Link using ld
|
||||
```
|
||||
|
||||
## Hello World
|
||||
|
||||
```Assembly
|
||||
.global _start
|
||||
.intel_syntax noprefix
|
||||
_start:
|
||||
// sys_write (Print to stdout)
|
||||
mov rax, 1
|
||||
mov rdi, 1
|
||||
lea rsi, [hello_world]
|
||||
// Buffer length
|
||||
mov rdx, 14
|
||||
syscall
|
||||
// sys_exit (safely end the program with a re turn code)
|
||||
mov rax, 60
|
||||
// Exit with code 0
|
||||
mov rdi, 0
|
||||
syscall
|
||||
|
||||
hello_world:
|
||||
// ASCII, zero delimited
|
||||
.asciz "Hello, World!\n"
|
||||
```
|
||||
|
||||
To know which system calls we can execute and which values needed for the registers, refer to [this table](https://blog.rchapman.org/posts/Linux_System_Call_Table_for_x86_64/).
|
||||
286
content/drafts/bash.md
Normal file
286
content/drafts/bash.md
Normal file
@@ -0,0 +1,286 @@
|
||||
---
|
||||
title: Shell Scripting [BASH]
|
||||
description:
|
||||
summary:
|
||||
draft: true
|
||||
tags:
|
||||
author: TrudeEH
|
||||
showToc: true
|
||||
---
|
||||
|
||||
## Bash Language
|
||||
|
||||
### 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
|
||||
|
||||
```bash
|
||||
# comment
|
||||
```
|
||||
|
||||
### Commands
|
||||
|
||||
A shell command consists of the command itself, followed by its arguments.
|
||||
|
||||
```bash
|
||||
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
|
||||
|
||||
| | | | | | |
|
||||
|---|---|---|---|---|---|
|
||||
|`if`|`then`|`elif`|`else`|`fi`|`time`|
|
||||
|`for`|`in`|`until`|`while`|`do`|`done`|
|
||||
|`case`|`esac`|`coproc`|`select`|`function`|
|
||||
|`{`|`}`|`[[`|`]]`|`!`|
|
||||
|
||||
### 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
|
||||
|
||||
#### `until`
|
||||
|
||||
```bash
|
||||
until test-commands; do
|
||||
...
|
||||
done
|
||||
```
|
||||
|
||||
Execute the code in `...` for as long as `test-commands` return non-zero.
|
||||
|
||||
#### `while`
|
||||
|
||||
```bash
|
||||
while test-commands; do
|
||||
...
|
||||
done
|
||||
```
|
||||
|
||||
Execute `...` for as long as `test-commands` return 0.
|
||||
|
||||
#### `for`
|
||||
|
||||
Expand `words` and execute `...` for each member in the resultant list, with `name` bound to the current member.
|
||||
|
||||
##### Iterate through List
|
||||
|
||||
```bash
|
||||
for item in list; do
|
||||
echo $item
|
||||
done
|
||||
```
|
||||
|
||||
##### C-like Loop
|
||||
|
||||
```bash
|
||||
for (( i=1; i<=10; i++ )); do
|
||||
echo "Loop number:" $i
|
||||
done
|
||||
```
|
||||
|
||||
##### Infinite Loop
|
||||
|
||||
```bash
|
||||
for (( ; ; )); do
|
||||
echo "Press Ctrl+C to stop..."
|
||||
done
|
||||
```
|
||||
|
||||
### Conditional Constructs
|
||||
|
||||
#### `if`
|
||||
|
||||
```bash
|
||||
if test-commands; then
|
||||
...
|
||||
elif more-test-commands; then
|
||||
...
|
||||
else
|
||||
...
|
||||
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`
|
||||
|
||||
```bash
|
||||
case word in
|
||||
p1 | p2 | p3) ... ;;
|
||||
p4 | p5 )
|
||||
...
|
||||
;;
|
||||
*) ... ;;
|
||||
esac
|
||||
```
|
||||
|
||||
Execute the `...` corresponding to the first pattern (`pX`) that matches the `word`.
|
||||
The `|` operator separates multiple patterns, and each clause can end with `;;`, `;&` or `;;&`. It's common to use `*` as the default case, since the pattern will always match.
|
||||
|
||||
Using `;&` instead of `;;` would cause the next `...` to be executed as well, and `;;&` would test the next clause, instead of immediately exiting.
|
||||
|
||||
#### `select`
|
||||
|
||||
```bash
|
||||
PS3="Enter a number: "
|
||||
|
||||
select option in entry1 entry2 entry3 entry4 entry5
|
||||
do
|
||||
echo "Selected character: $option"
|
||||
echo "Selected number: $REPLY"
|
||||
done
|
||||
```
|
||||
|
||||
The `select` command generates a menu, displaying each `entryX` in a list. The user is then prompted to select an option (in this case, a number from 1-5), and the resultant `$option` and `$REPLY` are then provided as variables.
|
||||
|
||||
Output:
|
||||
|
||||
```
|
||||
1) entry1
|
||||
2) entry2
|
||||
3) entry3
|
||||
4) entry4
|
||||
5) entry5
|
||||
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
|
||||
|
||||
- `( 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
|
||||
|
||||
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
|
||||
|
||||
```bash
|
||||
fname() {
|
||||
...
|
||||
}
|
||||
|
||||
function fname {
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
A function can store a block of code (compound command), so it can be reused by calling its name:
|
||||
|
||||
```bash
|
||||
fname
|
||||
```
|
||||
|
||||
Any variables defined inside the function
|
||||
|
||||
#### Arguments
|
||||
|
||||
```bash
|
||||
fname() {
|
||||
echo $1 $2
|
||||
}
|
||||
|
||||
fname "a" "b"
|
||||
```
|
||||
|
||||
#### Scope
|
||||
|
||||
```bash
|
||||
var1='A'
|
||||
var2='B'
|
||||
|
||||
fname () {
|
||||
local var1='C'
|
||||
var2='D'
|
||||
echo "var1: $var1, var2: $var2" # C, D
|
||||
}
|
||||
|
||||
echo "$var1, var2: $var2" # A, B
|
||||
fname # C, D
|
||||
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`
|
||||
|
||||
```bash
|
||||
fname() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
fname
|
||||
echo $? # 1
|
||||
```
|
||||
|
||||
Use the `return` command to exit the function and return a value.
|
||||
|
||||
### Variables (Parameters)
|
||||
|
||||
```bash
|
||||
name="Trude"
|
||||
echo $name # Trude
|
||||
|
||||
name+=" EH"
|
||||
echo $name # Trude EH
|
||||
echo ${name}
|
||||
```
|
||||
|
||||
Variables can be of any type, and grow to any needed size.
|
||||
|
||||
#### Special Variables
|
||||
|
||||
- `$*` Expands to every positional parameter: `$1$2$3`.
|
||||
- `$@` Expands to every positional parameter, separated by spaces: `"$1" "$2" "$3"`.
|
||||
- `$#` Number of positional arguments.
|
||||
- `$?` Exit status of last command / pipeline.
|
||||
- `$-` Current option flags set by `set`, or by the shell itself.
|
||||
- `$$` Process ID of the shell. In a subshell, it expands to the process ID of the parent shell.
|
||||
- `$!` Process ID of the latest job placed into the background.
|
||||
- `$0` Name of the shell or script.
|
||||
|
||||
### Shell Expansions
|
||||
|
||||
#### Brace Expansion
|
||||
|
||||
```bash
|
||||
echo a{d,c,b}e # ade ace abe
|
||||
```
|
||||
|
||||
#### Tilde Expansion
|
||||
|
||||
- `~` = `$HOME`
|
||||
- `~+` = `$PWD`
|
||||
- `~-` = `$OLDPWD`
|
||||
|
||||
#### Shell Parameter Expansion
|
||||
|
||||
### Builtins ---------------------
|
||||
|
||||
Bash doesn't come with any programs such as `cat`, `grep` and `ls` by default, for example. Those
|
||||
19
content/drafts/hardware-tools.md
Normal file
19
content/drafts/hardware-tools.md
Normal file
@@ -0,0 +1,19 @@
|
||||
---
|
||||
title: "hardware-tools"
|
||||
description:
|
||||
draft: true
|
||||
tags:
|
||||
author: TrudeEH
|
||||
showToc: true
|
||||
---
|
||||
|
||||
|
||||
Scrcpy
|
||||
|
||||
scrcpy —otg
|
||||
|
||||
scrcpy
|
||||
|
||||
ADB
|
||||
|
||||
adb kill-server
|
||||
70
content/drafts/linux-encrypt/index.md
Normal file
70
content/drafts/linux-encrypt/index.md
Normal file
@@ -0,0 +1,70 @@
|
||||
---
|
||||
tags:
|
||||
- notes
|
||||
- os
|
||||
author: TrudeEH
|
||||
draft: true
|
||||
showToc: true
|
||||
title: Linux
|
||||
---
|
||||
#todo
|
||||
|
||||
## Automatic Updates
|
||||
|
||||
### Debian
|
||||
|
||||
```sh
|
||||
dpkg-reconfigure --priority=low unattended-upgrades
|
||||
```
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
- Read error logs from the last session
|
||||
|
||||
```Shell
|
||||
sudo journalctl -b -1 -r -p err
|
||||
```
|
||||
|
||||
## Disk Encryption
|
||||
|
||||
### Full Disk Encryption
|
||||
|
||||
The exact steps to fully encrypt a disk varies depending on the distribution, and can only be done at the install time.
|
||||
On Ubuntu, for example, you can enable it in the "Advanced Features" toggle, at the disk selection stage.
|
||||

|
||||
|
||||
### Encrypt Home Directory
|
||||
|
||||
```Bash
|
||||
sudo apt install ecryptfs-utils cryptsetup # Install dependencies
|
||||
# Create temporary admin account
|
||||
sudo adduser temp_user # Add new user
|
||||
sudo usermod -aG sudo temp_user # Give the new user sudo perms
|
||||
```
|
||||
|
||||
Now, log out of the current user and switch to `temp_user`.
|
||||
|
||||
> [!important] Do not "switch accounts", the current user cannot be active while its home directory is being encrypted.
|
||||
On the `temp_user` account, run the following command, then, log out and return to your user.
|
||||
|
||||
```Bash
|
||||
sudo ecryptfs-migrate-home -u <username>
|
||||
```
|
||||
|
||||
After logging in, you might be greeted with a pop-up with the title "Update Information". Click on "Run this action now" and provide your password.
|
||||
If you already closed the pop-up, run the command below, and provide your password.
|
||||
|
||||
```Bash
|
||||
encryptfs-unwrap-passphrase
|
||||
```
|
||||
|
||||
You will receive a string of text. This will be your recovery password, needed to mount the home folder from another machine.
|
||||
|
||||
### Encrypt Swap
|
||||
|
||||
```Bash
|
||||
swapon -s # Check if you have a swap partition
|
||||
sudo ecryptfs-setup-swap # Encrypt swap
|
||||
```
|
||||
|
||||
You may get an error: `swapon: cannot open /dev/mapper/cryptswap1: No such file or directory`. If you do, reboot and check if the swap partition is encrypted with the `swapon -s` command.
|
||||
BIN
content/drafts/linux/EXT2.png
Normal file
BIN
content/drafts/linux/EXT2.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 3.2 MiB |
BIN
content/drafts/linux/EXT3.png
Normal file
BIN
content/drafts/linux/EXT3.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.5 MiB |
BIN
content/drafts/linux/Pasted image 20250401143509.png
Normal file
BIN
content/drafts/linux/Pasted image 20250401143509.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 7.7 KiB |
BIN
content/drafts/linux/image5.png
Normal file
BIN
content/drafts/linux/image5.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 43 KiB |
BIN
content/drafts/linux/image6.png
Normal file
BIN
content/drafts/linux/image6.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 60 KiB |
BIN
content/drafts/linux/image7.png
Normal file
BIN
content/drafts/linux/image7.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 155 KiB |
501
content/drafts/linux/index.md
Normal file
501
content/drafts/linux/index.md
Normal file
@@ -0,0 +1,501 @@
|
||||
---
|
||||
title: Linux Architecture
|
||||
description:
|
||||
date: 2025-02-17T08:32:33+00:00
|
||||
draft: true
|
||||
tags:
|
||||
- computer-science
|
||||
- os
|
||||
author: TrudeEH
|
||||
showToc: true
|
||||
---
|
||||
|
||||
## What is Linux
|
||||
|
||||
TODO
|
||||
|
||||
## Disk & Data Storage
|
||||
|
||||
### Disk Partitions
|
||||
|
||||
Except for the `/boot` and `/root` partitions, all partitions are optional and flexible. You can add any partitions to your disk, however, these are the most common ones:
|
||||
|
||||
| Partition | Needed | File System | Recommended Size | Description |
|
||||
| ------------ | -------------------------------------- | ---------------- | --------------------- | ------------------------------------------------------------------------------------------- |
|
||||
| `/boot` | Yes | `ext2` | 512 MB | Store kernels and boot info. This should always be the first partition on the drive. |
|
||||
| `/boot/efi` | Depends | `FAT-32` | 512 MB | Needed to boot with UEFI. |
|
||||
| `/BIOS Boot` | Depends | Not formatted | 1 MB | Only applies to GPT, and is usually a 1 MB partition for GRUB to install the bootloader on. |
|
||||
| `/root` | Yes | `ext4` | 20+ GB | Stores system files. |
|
||||
| `/swap` | No | `swap` (header) | 2x RAM; at least 2 GB | Use the disk as 'slower RAM', if the system RAM is full. |
|
||||
| `/tmp` | No (can be mounted on the `/root` dir) | `tmpfs` / `ext3` | 10 GB | Temporary file system. |
|
||||
| `/home` | No (can be mounted on the `/root` dir) | `ext4` | max | Stores user files. |
|
||||
|
||||
#### Manage Partitions
|
||||
|
||||
One of the most common programs to manage partitions is `fdisk`.
|
||||
|
||||
```sh
|
||||
sudo fdisk /dev/sdX
|
||||
```
|
||||
|
||||
Then, type `m` to read the manual.
|
||||
|
||||
#### Format Partitions
|
||||
|
||||
Use the `mkfs` command to format existing partitions.
|
||||
|
||||
```sh
|
||||
mkfs -v -t ext4 /dev/sdX
|
||||
```
|
||||
|
||||
For initializing `swap`, use `mkswap /dev/sdX` instead.
|
||||
|
||||
#### Mount Partitions
|
||||
|
||||
A partition must be mounted for the host system to be able to access it.
|
||||
|
||||
```sh
|
||||
mkdir -p <dir>
|
||||
mount /dev/sdX <dir>
|
||||
```
|
||||
|
||||
> Use the `-t <fs>` flag to specify the file system if `mount` fails to detect it automatically.
|
||||
|
||||
### File Systems
|
||||
|
||||
> This section is heavily simplified and only covers `EXT2-4`.
|
||||
|
||||
#### Concepts
|
||||
|
||||
- An `inode` provides the following information:
|
||||
- Pointer to the file
|
||||
- Creation date / Modified times
|
||||
- Permissions
|
||||
- A `directory (table)` contains the data before the current directory, the directory itself, and every file inside that directory.
|
||||
- A `block` is the standard data unit for data in a hard drive. (Same size as memory pages. Ex `x86` CPU would use `4 KB` as the block size.)
|
||||
|
||||
|
||||
#### EXT2
|
||||
|
||||
Uses linked lists to store and lookup data, to keep the implementation of the filesystem itself as simple as possible. A simple filesystem makes it easier to repair (or skip) broken sectors on the hard drive.
|
||||
|
||||
#### Partition Layout
|
||||
|
||||

|
||||
|
||||
#### EXT3
|
||||
|
||||
##### Journal
|
||||
|
||||
`EXT3` implements a journal to act as a buffer after a crash. If any operation in the journal fails, because it was logged, the filesystem is able to recover and finish any pending operations quickly, and not lose data. `EXT2` had another issue, where if an opened directory were deleted, its `inode` wouldn't be deleted, leaving an orphaned, empty `inode` on the filesystem. If the program holding it was to be closed, the `inode` would be deleted, but in the event of a crash, the `inode` would be left in the filesystem, with no way to be freed.
|
||||
|
||||
##### HTrees
|
||||
|
||||
`EXT3` can also use a [index](notes/algorithms_and_data/index.md) instead of a linked list to store directory entries, making lookup times much faster. To build the HTree, all filenames are hashed and ordered, making the implementation more complex. This feature is disabled by default.
|
||||
|
||||
##### Scalability
|
||||
|
||||
Before, only one core could write to the *superblock* at a time, but `EXT3` updates `inode` and `block` count at the *block group descrip_t_or* level. The superblock is only updated through a system call: `statfs()`, or if the filesystem is unmounted.
|
||||
`EXT3` also removed *big kernel locks* (deprecated feature that added the ability to freeze the kernel), and `sleep_on()`, which was replaced with `wait_event()`, preventing infinite loops.
|
||||
These patches improved multicore performance by over 10x.
|
||||
|
||||
##### Preallocation / Reservation
|
||||
|
||||
Writing two files simultaneously can create noncontinuous space.
|
||||

|
||||
Because `EXT3` was designed to be used with HDDs, and separate portions of a file would slow down read speeds, `EXT3` implemented a preallocation/reservation system.
|
||||
Inside the block bitmap, a few extra blocks were preallocated, storing both files in separate locations.
|
||||

|
||||
Instead of `EXT2`, where errors were corrected directly in the hard drive, `EXT3` reserves space for each specific `inode` in memory. In the event of a crash, all data would be stored in memory, and thus, not corrupting the HDD itself.
|
||||
|
||||
##### Online Resizer
|
||||
|
||||
`EXT3` also implements a protocol to support `LVM`, which allows for many disks to be used as a single partition. The largest possible space `EXT3` supports without patches is 16 GB.
|
||||
|
||||
##### Partition Layout
|
||||
|
||||

|
||||
|
||||
#### EXT4
|
||||
|
||||
##### Larger FS
|
||||
|
||||
`EXT3` can only support up to 16 TB. This is why `EXT4` was created.
|
||||
Instead of ==32 bits== capacity to count blocks, `EXT4` divides each entry in the block descriptor table in two parts: An upper, and a lower entry. This lower entry extends the upper one, and since each supports up to ==32 bits==, the total supported block count (in the block descriptor table) rises to ==64 bits== (16 TB → 1,000,000,000 TB).
|
||||
|
||||
##### Extents
|
||||
|
||||
Instead of using block mapping (the filesystem allocates blocks individually for each file), which can lead to fragmentation, `EXT4` uses **extents**, a range of contiguous blocks, allocated to each file.
|
||||
This uses a 48 bit system, which limits the FS capacity to 1 EB (1,000,000 TB).
|
||||
Each extent can point to 128 MB of data, or 1 block group.
|
||||
|
||||
##### Compatibility
|
||||
|
||||
The `EXT4` driver supports `EXT3` as well, and so, the Linux kernel only uses the `EXT2` and `EXT4` drivers. The `EXT3` driver was removed as the new one is more performant.
|
||||
|
||||
##### HTrees
|
||||
|
||||
HTrees are now enabled by default, allowing up to 10 million subdirectories. However, `EXT4` implements a Three Level HTree, which can be enabled using the `large_dir` flag, and extends this limit to 2 Billion subdirectories.
|
||||
|
||||
##### Fast FS Check
|
||||
|
||||
The `big_itable_unused` field was added to the block descriptor table, allowing for fast filesystem checks and error correction, as well as some other improvements.
|
||||
|
||||
##### Multiblock Allocation
|
||||
|
||||
Previously, each block needed one call to be allocated. `EXT4` added support for multi-block allocation, which means that only one call is needed to allocate multiple blocks.
|
||||
|
||||
##### Delayed Allocation
|
||||
|
||||
Every write command is delayed for as long as possible, making it so that changes can be made in memory before they affect (and possible fragment) the actual drive.
|
||||
|
||||
##### Persistent PreAllocaition
|
||||
|
||||
The FS can now be called to preallocate an empty extent, so, once that file is populated, it stays as a contiguous space.
|
||||
|
||||
##### Metadata Checksums
|
||||
|
||||
Metadata is checked often, which helps find any issues with the file system, as each data structure is now properly 'documented'.
|
||||
|
||||
##### Better Times
|
||||
|
||||
- A creation time was added;
|
||||
- The time precision was increased to nanoseconds instead of only seconds;
|
||||
- The maximum supported time was increased as well (the standard UNIX time can only go up to 2038).
|
||||
|
||||
##### Extended Attributes
|
||||
|
||||
The filesystem can also be customized with new entries at the end of each `inode`.
|
||||
|
||||
##### Quotas
|
||||
|
||||
`EXT4` also supports adding limits to the size of a file, or even multiple files spread across the filesystem.
|
||||
|
||||
##### Barriers
|
||||
|
||||
Some hard drives have caches, which impact the journal, sometimes causing it to be written after the cache, which would create conflicts. To fix this issue, `EXT4` creates a 'barrier', preventing the disk from writing data before the journal is written to the drive. This feature impacts performance, but is also very needed.
|
||||
|
||||
##### Flexible Block Groups
|
||||
|
||||
Groups blocks together, isolating chucks to write data on, which helps make data more contiguous.
|
||||
|
||||
##### Meta Block Groups
|
||||
|
||||
If the whole filesystem was only a single block group, it would max out at 256 TB of total data. Using meta block groups, this limit is increased to 32 bits of block group descriptor, which makes the **total capacity of the filesystem** ==**512 PB**==.
|
||||
|
||||
##### Partition Layout
|
||||
|
||||
[https://maplecircuit.dev/linux/fs/ext/ext4.html](https://maplecircuit.dev/linux/fs/ext/ext4.html)
|
||||
|
||||
### File Permissions
|
||||
|
||||
TODO
|
||||
|
||||
```sh
|
||||
chown root:root $LFS
|
||||
chmod 755 $LFS
|
||||
```
|
||||
|
||||
### `/root` Directory Structure
|
||||
|
||||
Root directories might vary slightly between distributions (and other UNIX systems), however, the 'base' is always the same. For example, a Debian root directory would be the following:
|
||||
|
||||
```
|
||||
/
|
||||
├── bin -> /usr/bin
|
||||
├── boot (Mount point for /boot partition)
|
||||
│ ├── grub/grub2 (GRUB configuration and modules)
|
||||
│ ├── efi (EFI System Partition - UEFI systems)
|
||||
│ └── kernels (Kernel images - may be under grub)
|
||||
├── dev
|
||||
│ ├── pts (Pseudo-terminals)
|
||||
│ ├── shm (Shared memory)
|
||||
│ ├── null (Null device)
|
||||
│ ├── zero (Zero device)
|
||||
│ ├── random (Random number generator)
|
||||
│ └── urandom (Non-blocking random number generator)
|
||||
├── etc (Configuration files)
|
||||
│ ├── network (Networking configuration)
|
||||
│ ├── systemd (Systemd configuration)
|
||||
│ ├── default (Default settings for programs)
|
||||
│ ├── init.d (Legacy init scripts / systemd link)
|
||||
│ ├── ssh (SSH server and client configuration)
|
||||
│ ├── X11 (X Window System configuration)
|
||||
│ ├── pam.d (PAM configuration)
|
||||
│ └── security (Security-related configuration)
|
||||
├── home (Home Directories)
|
||||
├── lib -> /usr/lib
|
||||
├── lib64 -> /usr/lib64
|
||||
├── media (Mount Point for Removable Media)
|
||||
├── mnt (Temporary Mount Point)
|
||||
├── opt (Optional Packages)
|
||||
├── proc (Process Information - Virtual Filesystem)
|
||||
│ ├── self (Symbolic link to the current process's directory)
|
||||
│ ├── cpuinfo (CPU information)
|
||||
│ ├── meminfo (Memory information)
|
||||
│ ├── mounts (Mounted file systems)
|
||||
│ ├── cmdline (Kernel command line)
|
||||
│ ├── <PID> (Directories for each process, named by PID)
|
||||
├── root (Root User's Home Directory)
|
||||
├── run
|
||||
│ ├── systemd (Systemd runtime data)
|
||||
│ ├── user (User-specific runtime data)
|
||||
│ └── lock (Lock files)
|
||||
├── sbin -> /usr/sbin
|
||||
├── srv (Service Data)
|
||||
├── sys (System Information - Virtual Filesystem)
|
||||
│ ├── devices (Device tree)
|
||||
│ ├── firmware (Firmware information)
|
||||
│ ├── power (Power management settings)
|
||||
│ ├── kernel (Kernel parameters)
|
||||
│ └── module (Kernel modules)
|
||||
├── tmp (Temporary Files)
|
||||
├── usr (User Programs and Data)
|
||||
│ ├── bin (User binaries)
|
||||
│ ├── sbin (Non-essential system administration commands)
|
||||
│ ├── lib (Libraries for /usr/bin and /usr/sbin)
|
||||
│ ├── include (Header files for C/C++ development)
|
||||
│ ├── share (Architecture-independent data)
|
||||
│ └── local (Locally installed software)
|
||||
└── var (Variable Data)
|
||||
├── log (System log files)
|
||||
├── tmp (Temporary files that persist across reboots)
|
||||
├── lib (Variable data for installed programs)
|
||||
├── cache (Cached data for applications)
|
||||
└── spool (Spool directories)
|
||||
```
|
||||
|
||||
## Kernel
|
||||
|
||||
Linux is a kernel: the core of an operative system. OSes that use the Linux kernel are called Linux Distros (Distributions).
|
||||
A Kernel does the following:
|
||||
- Executes first when the computer boots up and has full access to the hardware.
|
||||
- Implements drivers to control peripherals, network devices and other resources.
|
||||
- Runs other programs (userland software) and allows them to communicate with each other and with the hardware.
|
||||
|
||||
### Compiling
|
||||
|
||||
First, install the necessary dependencies. For example, on Debian:
|
||||
|
||||
```sh
|
||||
sudo apt install build-essential
|
||||
sudo apt build-dep linux
|
||||
```
|
||||
|
||||
#### Compile the Kernel from Scratch
|
||||
|
||||
```sh
|
||||
git clone --depth 1 https://github.com/torvalds/linux # GitHub mirror
|
||||
cd linux
|
||||
make tinyconfig # OPTIONAL: Creates the most minimal configuration possible.
|
||||
make nconfig # Configure the kernel before compiling
|
||||
make -j$(nproc) # Compile the kernel
|
||||
```
|
||||
|
||||
The kernel binary compiles to `arch/x86/boot/bzImage` on `x64` systems.
|
||||
|
||||
#### Rebuild the Kernel for an Existing Installation
|
||||
|
||||
```sh
|
||||
mkdir linux-parent && cd linux-parent
|
||||
git clone --depth 1 https://github.com/torvalds/linux
|
||||
cd linux
|
||||
|
||||
cp /boot/config-$(uname -r) .config # Copy current kernel config
|
||||
make nconfig # Edit the current kernel configuration
|
||||
diff /boot/config-$(uname -r) .config # Check your changes
|
||||
|
||||
# Do not include debugging symbols. Alternatively, use `strip` to remove them. (these configs are working as of 6.14)
|
||||
scripts/config --undefine GDB_SCRIPTS
|
||||
scripts/config --undefine DEBUG_INFO
|
||||
scripts/config --undefine DEBUG_INFO_SPLIT
|
||||
scripts/config --undefine DEBUG_INFO_REDUCED
|
||||
scripts/config --undefine DEBUG_INFO_COMPRESSED
|
||||
scripts/config --set-val DEBUG_INFO_NONE y
|
||||
scripts/config --set-val DEBUG_INFO_DWARF5 n
|
||||
scripts/config --disable DEBUG_INFO_DWARF_TOOLCHAIN_DEFAULT
|
||||
```
|
||||
|
||||
##### Install Manually
|
||||
|
||||
```sh
|
||||
make -j$(nproc)
|
||||
sudo make modules_install
|
||||
sudo make install # Copies the kernel image to /boot and updates initrd and grub.
|
||||
```
|
||||
|
||||
Before rebooting, run `uname -mrs`, and note the version. On your next reboot, use the same command to verify that you are using the new kernel.
|
||||
|
||||
To remove the kernel after installing it manually, run the following commands:
|
||||
|
||||
```sh
|
||||
sudo rm /boot/*-<version>*
|
||||
sudo update-grub
|
||||
```
|
||||
|
||||
##### Install Using `dpkg` (Debian)
|
||||
|
||||
```sh
|
||||
make -j$(nproc) deb-pkg LOCALVERSION=-custom
|
||||
sudo dpkg -i ../linux-headers*-custom*.deb
|
||||
sudo dpkg -i ../linux-image*-custom*.deb
|
||||
```
|
||||
|
||||
### Modules
|
||||
|
||||
A kernel module is a snippet of code that can be added to the kernel and managed without having to recompile the kernel.
|
||||
|
||||
For example, this `example.c` file would be a valid kernel module, that prints a message to the log once it is enabled or disabled:
|
||||
|
||||
```c
|
||||
#include <linux/init.h>
|
||||
#include <linux/kernel.h>
|
||||
#include <linux/module.h>
|
||||
|
||||
MODULE_LICENSE("GPL");
|
||||
MODULE_AUTHOR("Trude");
|
||||
MODULE_DESCRIPTION("A simple kernel module to print a log message");
|
||||
MODULE_VERSION("0.1");
|
||||
|
||||
static int __init kernel_message_init(void) {
|
||||
printk(KERN_INFO "Example module loaded!\n");
|
||||
return 0; // Non-zero return means module couldn't be loaded.
|
||||
}
|
||||
|
||||
static void __exit kernel_message_exit(void) {
|
||||
printk(KERN_INFO "Example module unloaded!\n");
|
||||
}
|
||||
|
||||
module_init(kernel_message_init);
|
||||
module_exit(kernel_message_exit);
|
||||
```
|
||||
|
||||
To install the module, it first must be compiled.
|
||||
The following `Makefile` can compile, clean, and test the module:
|
||||
|
||||
```Makefile
|
||||
obj-m := example.o
|
||||
|
||||
all:
|
||||
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
|
||||
clean:
|
||||
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
|
||||
|
||||
test:
|
||||
sudo dmesg -C # Clear the kernel log
|
||||
sudo insmod example.ko # Load the module
|
||||
sudo rmmod example.ko # Unload the module
|
||||
sudo dmesg # Read the kernel log
|
||||
```
|
||||
|
||||
Then, to run the module:
|
||||
|
||||
```sh
|
||||
make # Compile the module
|
||||
make test # Load and unload the module
|
||||
make clean # Delete the object files created at compile time
|
||||
```
|
||||
|
||||

|
||||
|
||||
### Syscalls
|
||||
|
||||
To "ask" the kernel to perform a hardware task, to access the file system or access other resources, a program executes a `syscall`.
|
||||
|
||||
For example, in x64 assembly:
|
||||
|
||||
```Assembly
|
||||
// sys_write (Print to stdout)
|
||||
mov rax, 1
|
||||
mov rdi, 1
|
||||
lea rsi, [hello_world]
|
||||
// Buffer length
|
||||
mov rdx, 14
|
||||
syscall
|
||||
```
|
||||
|
||||
This snippet implements a syscall that prints text to `stdout`, usually a terminal window.
|
||||
A C program generally uses libraries, which then implement system calls to perform actions.
|
||||
|
||||
### Processes
|
||||
|
||||
TODO - How does the kernel execute programs? What are processes?
|
||||
Executables can either be ELF files, or a script starting with `#!` (`script_format`).
|
||||
|
||||
#### Execute Programs
|
||||
|
||||
When executing a program, the kernel first parses the executable to detect whether it is a script or ELF executable, then, CPU registers are updated (the `instruction pointer` -> `ELF entry point`, etc). Finally, the program is executed, instruction by instruction, on the CPU.
|
||||
|
||||
### Devices
|
||||
|
||||
#### Device Drivers
|
||||
|
||||
Once a new device is plugged in, it attempts to call the Kernel, which identifies that device. The Kernel then loads the appropriate driver. Drivers are Kernel Modules used to interact with hardware. Once the required module is loaded, the Kernel calls the `udevadm` program, that creates a device file in the `/dev` directory.
|
||||
Whenever there is a read/write operation to that device, the Kernel intercepts the request and calls the driver function instead, which implements that operation, and this is why a driver must implement all possible file operations.
|
||||
Not all devices control hardware. For example, `/dev/random` generates a random number.
|
||||
In the Linux Kernel, there are **Character Type Devices** and **Block Type Devices.**
|
||||
A Character Device provides an endless stream or characters that can be read one at a time. This includes a keyboard and sensors, for example. Block Devices provide data as blocks of a defined size. These include disks and USB drives.
|
||||
The only exceptions are Network Devices. Network data cannot be manipulated with file operations, so they are handled differently in the kernel.
|
||||
|
||||
#### Network Devices
|
||||
|
||||
If a network card is plugged in, like any other device, it communicates with the Kernel, and the Kernel loads the appropriate Kernel Module that contains the driver. After that, the driver adds new entries to the Kernel's `NICs`, which are data structures stored in memory. The driver then creates configuration files in the `/sys/class/net/` directory. These are not device files, just configuration files. Different network cards can have multiple network interfaces, and even multiple ports to connect to an Ethernet cable, for example. Each interface gets its own folder, as they can have different configurations. Editing these files can change device configurations, but the only way to actually use these interfaces is by calling the Kernel directly. The Kernel can, in turn, create a virtual file in memory, which is then passed as an ID to the application. The application still receives it as a regular file ID (File Descriptor), like with any other device, but it works differently from the Kernel's perspective.
|
||||
A GPU works similarly, and although the Kernel exposes these devices as files, they are not real files in the filesystem, as doing so would slow them down immensely.
|
||||
|
||||
#### GPUs
|
||||
|
||||
At the startup time, a device file is generated for the GPU, like any other device.
|
||||
When an application attempts to use the GPU, it first searches for a **Graphics API** to use with it. Examples include Vulkan, OpenGL and DirectX.
|
||||
These APIs are code functions that forward the requests to a library such as `Mesa`.
|
||||
|
||||
`Mesa` then calls the Kernel's DRM (Direct Rendering Engine), which then calls the GPU driver to provide GPU capabilities.
|
||||
The GPU still has a device file, which is used by the Kernel's DRM and Mesa, but most commands are delivered to the GPU driver directly.
|
||||
|
||||
## Shell ([bash](bash.md))
|
||||
|
||||
The kernel by itself isn't intractable, so a shell is needed for the user to be able to execute programs and run commands. Bash is not only a prompt, but also an interpreter for its own programming language, which can be used to write scripts and automate tasks.
|
||||
|
||||
### Compiling
|
||||
|
||||
TODO
|
||||
|
||||
## Programs
|
||||
|
||||
### `coreutils`
|
||||
|
||||
### `util-linux`
|
||||
|
||||
### `vi/nano`
|
||||
|
||||
## Libraries
|
||||
|
||||
### `glibc`
|
||||
|
||||
### `ncurses`
|
||||
|
||||
## Init System
|
||||
|
||||
## GRUB
|
||||
|
||||
## Networking
|
||||
|
||||
## Compilers
|
||||
|
||||
## Desktop
|
||||
|
||||
### Wayland
|
||||
|
||||
### Window Managers
|
||||
|
||||
### Desktop Environments
|
||||
|
||||
## INC =-------
|
||||
|
||||
## Processes
|
||||
|
||||
TODO
|
||||
A program is an executable containing machine code. When a computer executes a program, it is first loaded into memory.
|
||||

|
||||
A program loaded in memory is a process.
|
||||
|
||||
> Note: For interpreted languages, the interpreter creates a process that executes code directly.
|
||||
191
content/drafts/lua.md
Normal file
191
content/drafts/lua.md
Normal file
@@ -0,0 +1,191 @@
|
||||
---
|
||||
Status: Planned
|
||||
Created by: Trude EH
|
||||
tags:
|
||||
- notes
|
||||
- programming
|
||||
author: TrudeEH
|
||||
draft: true
|
||||
searchHidden: false
|
||||
showToc: true
|
||||
title: Lua
|
||||
---
|
||||
Embedded language.
|
||||
#todo
|
||||
|
||||
## Comments
|
||||
|
||||
```Lua
|
||||
-- One-Line comment
|
||||
--[[ Multi-line
|
||||
comment
|
||||
--]]
|
||||
```
|
||||
|
||||
## Simple Literals
|
||||
|
||||
```Lua
|
||||
local number = 5
|
||||
local string = "hello, world"
|
||||
local single = 'same as using \"'
|
||||
local multiline = [[ Multi
|
||||
line
|
||||
string ]]
|
||||
local yes, no = true, false
|
||||
local nothing = nil
|
||||
```
|
||||
|
||||
## Functions
|
||||
|
||||
Functions can be stored as values.
|
||||
|
||||
```Lua
|
||||
local function hello(name)
|
||||
print("Hello", name)
|
||||
end
|
||||
local greet = function(name)
|
||||
-- .. is string concaternation
|
||||
print("Hello " .. name .. "!")
|
||||
end
|
||||
-- Calling a function
|
||||
greet("Trude")
|
||||
greet "Trude"
|
||||
```
|
||||
|
||||
Functions can also return other functions.
|
||||
|
||||
```Lua
|
||||
local high = function(value)
|
||||
return function(value2)
|
||||
return value + value2
|
||||
end
|
||||
end
|
||||
local add_one = high(1)
|
||||
print("1 + 2 -> ", add_one(2))
|
||||
```
|
||||
|
||||
Functions can return multiple values.
|
||||
|
||||
```Lua
|
||||
local return_four_values = function()
|
||||
return 1, 2, 3, 4
|
||||
end
|
||||
first, second, third = return_four_values() -- "4" was discarded.
|
||||
```
|
||||
|
||||
Variable arguments.
|
||||
|
||||
```Lua
|
||||
local variable_arguments = function( ... )
|
||||
local arguments = { ... }
|
||||
for i, v in ipairs({ ... }) do print(i, v) end
|
||||
end
|
||||
print(variable_arguments("this", "is", "a", "test"))
|
||||
```
|
||||
|
||||
## Tables
|
||||
|
||||
Lua's only data structure. (Also used for maps and lists)
|
||||
|
||||
### As lists…
|
||||
|
||||
```Lua
|
||||
local list = { "first", 2, false, function() print("Fourth") end }
|
||||
print("Lua is 1-indexed:", list[1])
|
||||
```
|
||||
|
||||
### As maps…
|
||||
|
||||
```Lua
|
||||
local t = {
|
||||
literal_key = "a string",
|
||||
["an expression"] = "also works",
|
||||
[function() end] = true
|
||||
}
|
||||
print("literal_key : ", t.literal_key)
|
||||
print("an expression : ", t["an expression"]
|
||||
print("function() end : ", t[function() end]) -- nothing will be printed here, because each function definition points to a different memory address.
|
||||
```
|
||||
|
||||
### Colon Functions
|
||||
|
||||
```Lua
|
||||
local T = {}
|
||||
function T.something(self, ... ) end
|
||||
function T:something( ... ) end -- Same as the previous line
|
||||
```
|
||||
|
||||
### Metatables
|
||||
|
||||
```Lua
|
||||
local vector_mt = {}
|
||||
vector_mt.__add = function(left, right)
|
||||
return setmetatable({
|
||||
left[1] + right[1],
|
||||
left[2] + right[2],
|
||||
left[3] + right[3]
|
||||
}, vector_mt)
|
||||
end
|
||||
local v1 = setmetatable({ 3, 1, 5 }, vector_mt)
|
||||
local v2 = setmetatable({ -3, 2, 2}, vector_mt)
|
||||
local v3 = v1 + v2 -- normally would produce an error, but doesn't, since the __add keyword was used.
|
||||
print(v3[1], v3[2], v3[3])
|
||||
```
|
||||
|
||||
## Control Flow
|
||||
|
||||
### For
|
||||
|
||||
#### Lists
|
||||
|
||||
```Lua
|
||||
local favs = { "ThePrimeagen", "ctt", "NoBoilerplate" }
|
||||
for index = 1, \#favs do -- # is the length operator (does not work on maps!)
|
||||
print(index, favs[index])
|
||||
end
|
||||
for index, value in ipairs(favs) do -- ipairs return the index and values of that index
|
||||
print(index, value)
|
||||
end
|
||||
```
|
||||
|
||||
#### Maps
|
||||
|
||||
```Lua
|
||||
local favs = { ThePrimeagen = 9.5, NoBoilerplate = "N/A" }
|
||||
for key, value in pairs(favs) do -- pairs return the index and values of that key
|
||||
print(key, value)
|
||||
end
|
||||
```
|
||||
|
||||
### If
|
||||
|
||||
```Lua
|
||||
local function action(visit_page)
|
||||
if visit_page then
|
||||
print("Opening new tab...")
|
||||
else
|
||||
print("Skipping...")
|
||||
end
|
||||
end
|
||||
-- "falsey": nil, false
|
||||
action() -- Same as: action(nil)
|
||||
action(false)
|
||||
-- Everything else is "truthy"
|
||||
action(true)
|
||||
action(0)
|
||||
action({})
|
||||
```
|
||||
|
||||
## Modules
|
||||
|
||||
Modules are just files.
|
||||
|
||||
```Lua
|
||||
-- module.lua
|
||||
local M = {}
|
||||
M.some_function = function() end
|
||||
return M
|
||||
-- main.lua
|
||||
local something = require("module")
|
||||
something.some_function()
|
||||
```
|
||||
63
content/drafts/macOS.md
Normal file
63
content/drafts/macOS.md
Normal file
@@ -0,0 +1,63 @@
|
||||
---
|
||||
Status: In progress
|
||||
Created by: Trude EH
|
||||
tags:
|
||||
- notes
|
||||
- os
|
||||
author: TrudeEH
|
||||
draft: true
|
||||
searchHidden: false
|
||||
showToc: true
|
||||
title: macOS
|
||||
---
|
||||
#todo
|
||||
|
||||
## System Data
|
||||
|
||||
System data is anything in the following locations:
|
||||
- `/Library`
|
||||
- `/System`
|
||||
- `~Library`
|
||||
- `/usr`
|
||||
- `.hidden_files`
|
||||
|
||||
## Memory
|
||||
|
||||
- Physical Memory - Total system memory
|
||||
- Memory Used
|
||||
- App Memory - Memory used for apps
|
||||
- Wired Memory - System memory
|
||||
- Compressed - Memory used by apps, but compressed as it is not immediately needed
|
||||
- Cached Files - Files saved in memory for faster launching
|
||||
- Swap Used - Memory stored in the SSD
|
||||
|
||||
### Memory Pressure
|
||||
|
||||
A measurement of how well macOS is managing the available memory.
|
||||
- Green/Low: Normal function; No tricks needed.
|
||||
- Yellow/Medium: There is not enough memory as-is, so macOS is actively compressing and decompressing memory as needed.
|
||||
- Red/High: Aside from compression, macOS is using the swap heavily.
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### System and Apps
|
||||
|
||||
|Description|Fix|
|
||||
|---|---|
|
||||
|Fix "App X is damaged and can't be opened."|`xattr -c <path/to/application.app>`|
|
||||
|Disable the Dock autohide delay|Fix: <br> <br>`defaults write com.apple.dock autohide-delay -float 0; defaults write com.apple.dock autohide-time-modifier -int 0;killall Dock` <br> <br>Restore defaults: <br> <br>`defaults delete com.apple.Dock autohide-delay; killall Dock`|
|
||||
|||
|
||||
|||
|
||||
|
||||
### Gaming
|
||||
|
||||
|Issue / Error / Description|Fix|
|
||||
|---|---|
|
||||
|Improve Terraria performance (and some other games)|Add `/gldevice:Vulkan` as a launch argument (on Steam).|
|
||||
|||
|
||||
|
||||
### Flash IMG File to Disk
|
||||
|
||||
1. `diskutil list` → Find the path to the device (`/dev/XXX`)
|
||||
2. `diskutil unmount /dev/XXX` → Unmount the disk
|
||||
3. `sudo dd if=image.img of=/dev/XXX bs=1M oflag=direct,sync status=progress`
|
||||
18
content/drafts/nvim.md
Normal file
18
content/drafts/nvim.md
Normal file
@@ -0,0 +1,18 @@
|
||||
---
|
||||
title: nvim
|
||||
description:
|
||||
draft: true
|
||||
tags:
|
||||
- tools
|
||||
- nvim
|
||||
- vim
|
||||
author: TrudeEH
|
||||
showToc: true
|
||||
---
|
||||
|
||||
## VIM Bindings
|
||||
|
||||
|
||||
## Using Neovim
|
||||
|
||||
## Configuring Neovim
|
||||
315
content/drafts/rust.md
Normal file
315
content/drafts/rust.md
Normal file
@@ -0,0 +1,315 @@
|
||||
---
|
||||
title: "rust"
|
||||
description:
|
||||
draft: true
|
||||
tags:
|
||||
author: TrudeEH
|
||||
showToc: true
|
||||
---
|
||||
|
||||
## 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`
|
||||
|
||||
## Hello World!
|
||||
|
||||
```rust
|
||||
fn main() {
|
||||
println!("Hello world!");
|
||||
}
|
||||
```
|
||||
|
||||
## Variables
|
||||
|
||||
```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
|
||||
};
|
||||
```
|
||||
|
||||
## Data Types
|
||||
|
||||
### Integer Types
|
||||
|
||||
|Length|Signed|Unsigned|Unsigned Decimal Length|
|
||||
|---|---|---|---|
|
||||
|8-bit|`i8`|`u8`|`0..=255`|
|
||||
|16-bit|`i16`|`u16`|`0..=65535`|
|
||||
|32-bit (default)|`i32`|`u32`|`0..=4294967295`|
|
||||
|64-bit|`i64`|`u64`|`0..=18446744073709551615`|
|
||||
|128-bit|`i128`|`u128`|`0..=340282366920938463463374607431768211455`|
|
||||
|arch||||
|
||||
|(Size of CPU architecture)|`isize`|`usize`|The size of a memory address.|
|
||||
|
||||
```rust
|
||||
let v: u16 = 32_u8 as u16; // Convert an u8 type to u16.
|
||||
println!("{}", i8::MAX); // Print the largest possible value a data type can hold.
|
||||
let a = 10_000; // _ is ignored, and is only used to help with readability.
|
||||
let b = 1 + 0xff + 0o77 + 0b1111_1111; // Various numerical bases are supported.
|
||||
|
||||
println!("{}", type_of(&v));
|
||||
```
|
||||
|
||||
### Floating Point Values
|
||||
|
||||
|Length|Signed|Unsigned|Unsigned Decimal Length|
|
||||
|---|---|---|---|
|
||||
|8-bit|`f8`|`u8`|`0..=255`|
|
||||
|16-bit|`f16`|`u16`|`0..=65535`|
|
||||
|32-bit (default)|`f32`|`u32`|`0..=4294967295`|
|
||||
|64-bit|`f64`|`u64`|`0..=18446744073709551615`|
|
||||
|128-bit|`f128`|`u128`|`0..=340282366920938463463374607431768211455`|
|
||||
|
||||
```rust
|
||||
assert!(0.1 + 0.2 == 0.3); // False, floating point numbers are subject to imprecision.
|
||||
assert!(0.1_f32 + 0.2 as f32 == 0.3_f32); // True. f32 is less precise. (Note: Remember that _ are optional and are ignored.)
|
||||
```
|
||||
|
||||
### Boolean Logic
|
||||
|
||||
|True|False|
|
||||
|---|---|
|
||||
|`true`|`false`|
|
||||
|`1`|`0`|
|
||||
|
||||
```rust
|
||||
let _f: bool = false; // 1 byte
|
||||
|
||||
let t = false;
|
||||
if !t { println!("t became true") }
|
||||
```
|
||||
|
||||
#### Boolean Operators
|
||||
|
||||
- `AND`
|
||||
- `OR`
|
||||
- `NOT`
|
||||
|
||||
#### Bitwise Operations
|
||||
|
||||
Each bit is considered a unit.
|
||||
|
||||
|AND|`&`|
|
||||
|---|---|
|
||||
|OR|`|
|
||||
|XOR|`^`|
|
||||
|LEFT SHIFT|`<<`|
|
||||
|RIGHT SHIFT|`>>`|
|
||||
|
||||
### Characters
|
||||
|
||||
```rust
|
||||
let c1: char = 'a'; // 4 bytes
|
||||
let c2: char = 'µ'; // Unicode is supported
|
||||
```
|
||||
|
||||
### Unit Type
|
||||
|
||||
```rust
|
||||
let _v: () = (); // () is similar to null. It means nothing. Takes up 0 bytes.
|
||||
```
|
||||
|
||||
## Range
|
||||
|
||||
```rust
|
||||
-3..2 // -3 to 1. 2 is excluded.
|
||||
'a'..='z' // a to z. z is included.
|
||||
```
|
||||
|
||||
## Scope
|
||||
|
||||
A scope can be created anywhere in the program.
|
||||
|
||||
```rust
|
||||
// Global Scope
|
||||
let y = 2;
|
||||
|
||||
{
|
||||
// Local Scope. x is not accessible outside this scope.
|
||||
let x = 1;
|
||||
println!("{} and {}", x, y);
|
||||
}
|
||||
```
|
||||
|
||||
If a variable inside the inner scope has the same name as one outside, the latter is shadowed.
|
||||
|
||||
## Functions
|
||||
|
||||
```rust
|
||||
fn main() { // No output; Implicit "-> ()".
|
||||
sum(3, 2);
|
||||
}
|
||||
|
||||
fn sum(x: i32, y: i32) -> i32 { // Takes 2 numbers as input, and outputs another.
|
||||
x + y;
|
||||
}
|
||||
|
||||
fn never_return() -> ! { // "-> !" A function that never returns to the caller. Either panics, or loops forever.
|
||||
panic!() // Error.
|
||||
unimplemented!() // Use if a function is not implemented yet.
|
||||
todo!() // Incomplete.
|
||||
}
|
||||
```
|
||||
|
||||
Type annotation is required in function definitions.
|
||||
|
||||
## Ownership
|
||||
|
||||
- Each value has an owner.
|
||||
- There can only be one owner at a time.
|
||||
- When the owner goes out of scope, the value will be dropped.
|
||||
|
||||
```rust
|
||||
{
|
||||
let s = "example";
|
||||
| |
|
||||
Owner Value
|
||||
}
|
||||
// Outside this scope, s is dropped from memory.
|
||||
```
|
||||
|
||||
## Borrowing
|
||||
|
||||
- Access data **without taking ownership** of it.
|
||||
- When borrowing, you are taking a **reference** (pointer) to the data, not the value itself.
|
||||
|
||||
**Rules**
|
||||
|
||||
- At any given time, you can have either **one mutable reference** or **any number** of **immutable references**.
|
||||
- References must **always be valid**.
|
||||
|
||||
```rust
|
||||
fn main() {
|
||||
let s1 = String::from("hello");
|
||||
let len = calculate_length(&s1);
|
||||
|
||||
println!("The length of '{}' is {}.", s1, len);
|
||||
}
|
||||
|
||||
fn calculate_length(s: &String) -> usize {
|
||||
s.len() // s is a pointer to s1
|
||||
}
|
||||
```
|
||||
|
||||
Example mutable reference:
|
||||
|
||||
```rust
|
||||
fn main() {
|
||||
let mut s = String::from("hello");
|
||||
change(&mut s);
|
||||
}
|
||||
|
||||
fn change(some_string: &mut String) {
|
||||
some_string.push_str(", world");
|
||||
}
|
||||
```
|
||||
|
||||
Get the address in memory:
|
||||
|
||||
```rust
|
||||
let x = 5;
|
||||
let p: &i32 = &x; // Reference to x, reads 5 by println!.
|
||||
|
||||
println!("The memory address of x is {:p}", p); // :p reads the raw reference value.
|
||||
```
|
||||
|
||||
Dereference:
|
||||
|
||||
```rust
|
||||
let x = 5;
|
||||
let p: &i32 = &x; // Reference to x
|
||||
assert_eq!(5, *p) // Go to the value p points to and read it.
|
||||
```
|
||||
|
||||
The `ref` keyword is an alternate syntax to create a reference:
|
||||
|
||||
```rust
|
||||
let c = 'T';
|
||||
let r1 = &c;
|
||||
let ref r2 = c;
|
||||
```
|
||||
|
||||
## Compound Types
|
||||
|
||||
Data types made of other types.
|
||||
|
||||
### Strings
|
||||
|
||||
A `String` is mutable, and is stored on the *stack* with a pointer to the *heap,* where the value is stored.
|
||||
|
||||
```rust
|
||||
let s1 = String::from("hello");
|
||||
| |
|
||||
Pointer Array stored on the heap
|
||||
(usize)
|
||||
```
|
||||
|
||||
#### Copy vs. Move
|
||||
|
||||
```rust
|
||||
// Copy a value
|
||||
let x = 1;
|
||||
let y = x;
|
||||
|
||||
// Move the pointer value.
|
||||
let s1 = String::from("hello");
|
||||
let s2 = s1;
|
||||
```
|
||||
|
||||
Now `s2` also points to the same string as `s1`. This [is not allowed in rust](https://www.notion.so/Rust-14149cf14b4c80ed8f7be5c63490aac2?pvs=21), so `s1` will be dropped. (Passing a string pointer to a function makes the function the new owner of the string).
|
||||
|
||||
```rust
|
||||
// Copy a string (Deep Copy)
|
||||
let s1 = String::from("hello");
|
||||
let s2 = s1.clone();
|
||||
```
|
||||
|
||||
In this example, the value in the heap is copied, so both `s1` and `s2` have their own values, and only own their own instance of the string.
|
||||
|
||||
#### `String` Vs. `&str`
|
||||
|
||||
|Type|Mutability|Ownership|Efficiency|
|
||||
|---|---|---|---|
|
||||
|`String`|Mutable; heap|Owns its contents|`-`|
|
||||
|`&str` (String Slice)|Immutable; stack|Does not own data|`+`|
|
||||
|`"..."` (String Literal)|Immutable; static storage (Stored inside the compiled program)|Does not own data|`+`; Same as `&str`|
|
||||
|
||||
```rust
|
||||
let s1: String = String::from("hello");
|
||||
let s2: &str = "Hello";
|
||||
|
||||
// Read String Slice
|
||||
let read_string_slice = &s2[0..1]; // "he"
|
||||
|
||||
// Move str to heap to make it mutable.
|
||||
let s: Box<str> = "hello, world".into(); // .into() converts to the variable type.
|
||||
let str_again = &s;
|
||||
```
|
||||
|
||||
### Tuples
|
||||
|
||||
Store different data types.
|
||||
|
||||
```rust
|
||||
let t: (String, Int) = (String::from("hello"), 14);
|
||||
```
|
||||
|
||||
Reference: [](https://youtu.be/BpPEoZW5IiY?si=1ri40iKdXR4zwp0J&t=8461)[https://youtu.be/BpPEoZW5IiY?si=WiJX41VB55S7Tx17&t=5607](https://youtu.be/BpPEoZW5IiY?si=WiJX41VB55S7Tx17&t=5607)
|
||||
491
content/drafts/swift.md
Normal file
491
content/drafts/swift.md
Normal file
@@ -0,0 +1,491 @@
|
||||
---
|
||||
title: "swift"
|
||||
description:
|
||||
draft: true
|
||||
tags:
|
||||
author: TrudeEH
|
||||
showToc: true
|
||||
---
|
||||
|
||||
## Tools
|
||||
|
||||
- Xcode & Xcode Command-line tools
|
||||
|
||||
## Hello World
|
||||
|
||||
In Swift, this line of code is a complete program.
|
||||
|
||||
```swift
|
||||
print("Hello, world!")
|
||||
```
|
||||
|
||||
Unlike C, you don't need to import a separate library for functionality like outputting text or handling strings.
|
||||
|
||||
Code written at global scope is used as the entry point for the program, so you don't need a `main()` function.
|
||||
|
||||
Swift does not require a `;` at the end of every line.
|
||||
|
||||
## Simple Values
|
||||
|
||||
Use `let` to make a constant and `var` to make a variable.
|
||||
|
||||
The value of a constant doesn't need to be known at compile time, but you must assign it a value exactly once.
|
||||
|
||||
This means you can use constants to name a value that you determine once but use in many places.
|
||||
|
||||
```swift
|
||||
var myVariable = 42
|
||||
myVariable = 50
|
||||
|
||||
let myConstant = 42
|
||||
```
|
||||
|
||||
Data types don't always have to write the type explicitly. Providing a value when you create a constant or variable lets the compiler infer its type. In the example above, the compiler infers that `myVariable` is an integer because its initial value is an integer.
|
||||
|
||||
If the initial value doesn't provide enough information (or if there isn't an initial value), specify the type by writing it after the variable, separated by a colon.
|
||||
|
||||
```swift
|
||||
let implicitInteger = 70
|
||||
let implicitDouble = 70.0
|
||||
|
||||
let explicitDouble: Double = 70
|
||||
```
|
||||
|
||||
Values are never implicitly converted to another type. If you need to convert a value to a different type, explicitly make an instance of the desired type.
|
||||
|
||||
```swift
|
||||
let label = "The width is "
|
||||
let width = 94
|
||||
let widthLabel = label + String(width)
|
||||
```
|
||||
|
||||
If the conversion to `String` from the last line is removed, the compiler throws an error:
|
||||
|
||||
```
|
||||
Binary operator '+' cannot be applied to operands of type 'String' and 'Int'
|
||||
```
|
||||
|
||||
There's also a simpler way to include values in strings:
|
||||
|
||||
```swift
|
||||
let apples = 3
|
||||
let oranges = 5
|
||||
let appleSummary = "I have \\(apples) apples."
|
||||
let fruitSummary = "I have \\(apples + oranges) pieces of fruit."
|
||||
```
|
||||
|
||||
Use three double quotation marks (`"""`) for strings that take up multiple lines. Indentation at the start of each quoted line is removed, as long as it matches the indentation of the closing quotation marks. For example:
|
||||
|
||||
```swift
|
||||
let quotation = """
|
||||
Even though there's whitespace to the left,
|
||||
the actual lines aren't indented.
|
||||
Except for this line.
|
||||
Double quotes (") can appear without being escaped.
|
||||
I still have \\(apples + oranges) pieces of fruit.
|
||||
"""
|
||||
```
|
||||
|
||||
Create arrays and dictionaries using brackets (`[]`), and access their elements by writing the index or key in brackets. A comma is allowed after the last element.
|
||||
|
||||
```swift
|
||||
var fruits = ["strawberries", "limes", "tangerines"]
|
||||
fruits[1] = "grapes"
|
||||
|
||||
var occupations = [
|
||||
"Malcolm": "Captain",
|
||||
"Kaylee": "Mechanic",
|
||||
]
|
||||
occupations["Jayne"] = "Public Relations"
|
||||
```
|
||||
|
||||
Arrays automatically grow as you add elements.
|
||||
|
||||
```swift
|
||||
fruits.append("blueberries")
|
||||
print(fruits)
|
||||
// Prints "["strawberries", "grapes", "tangerines", "blueberries"]"
|
||||
```
|
||||
|
||||
You also use brackets to write an empty array or dictionary. For an array, write `[]`, and for a dictionary, write `[:]`.
|
||||
|
||||
```swift
|
||||
fruits = []
|
||||
occupations = [:]
|
||||
```
|
||||
|
||||
If you're assigning an empty array or dictionary to a new variable, or another place where there isn't any type information, you need to specify the type.
|
||||
|
||||
```swift
|
||||
let emptyArray: [String] = []
|
||||
let emptyDictionary: [String: Float] = [:]
|
||||
```
|
||||
|
||||
## Control Flow
|
||||
|
||||
Use `if` and `switch` to make conditionals, and use `for`-`in`, `while`, and `repeat`-`while` to make loops.
|
||||
|
||||
Parentheses around the condition or loop variable are optional. Braces around the body are required.
|
||||
|
||||
```swift
|
||||
let individualScores = [75, 43, 103, 87, 12]
|
||||
var teamScore = 0
|
||||
|
||||
for score in individualScores {
|
||||
if score > 50 {
|
||||
teamScore += 3
|
||||
} else {
|
||||
teamScore += 1
|
||||
}
|
||||
}
|
||||
|
||||
print(teamScore)// Prints "11"
|
||||
```
|
||||
|
||||
In an `if` statement, the conditional must be a Boolean expression — this means that code such as `if score { ... }` is an error, not an implicit comparison to zero.
|
||||
|
||||
You can write `if` or `switch` after the equal sign (`=`) of an assignment or after `return`, to choose a value based on the condition.
|
||||
|
||||
```swift
|
||||
let scoreDecoration = if teamScore > 10 { "🎉" } else {""}
|
||||
|
||||
print("Score:", teamScore, scoreDecoration)// Prints "Score: 11 🎉"
|
||||
```
|
||||
|
||||
You can use `if` and `let` together to work with values that might be missing. These values are represented as optionals. An optional value either contains a value or contains `nil` to indicate that a value is missing. Write a question mark (`?`) after the type of a value to mark the value as optional.
|
||||
|
||||
```swift
|
||||
var optionalString: String? = "Hello"
|
||||
print(optionalString == nil)// Prints "false"
|
||||
|
||||
var optionalName: String? = "John Appleseed"
|
||||
var greeting = "Hello!"
|
||||
|
||||
if let name = optionalName {
|
||||
greeting = "Hello, \\(name)"
|
||||
}
|
||||
```
|
||||
|
||||
If the optional value is `nil`, the conditional is `false` and the code in braces is skipped. Otherwise, the optional value is unwrapped and assigned to the constant after `let`, which makes the unwrapped value available inside the block of code.
|
||||
|
||||
Another way to handle optional values is to provide a default value using the `??` operator. If the optional value is missing, the default value is used instead.
|
||||
|
||||
```swift
|
||||
let nickname: String? = nil
|
||||
let fullName: String = "John Appleseed"
|
||||
|
||||
let informalGreeting = "Hi \\(nickname ?? fullName)"
|
||||
```
|
||||
|
||||
You can use a shorter spelling to unwrap a value, using the same name for that unwrapped value.
|
||||
|
||||
```swift
|
||||
if let nickname {
|
||||
print("Hey, \\(nickname)")
|
||||
}// Doesn't print anything, because nickname is nil.
|
||||
```
|
||||
|
||||
Switches support any kind of data and a wide variety of comparison operations — they aren't limited to integers and tests for equality.
|
||||
|
||||
```swift
|
||||
let vegetable = "red pepper"
|
||||
|
||||
switch vegetable {
|
||||
case "celery":
|
||||
print("Add some raisins and make ants on a log.")
|
||||
case "cucumber", "watercress":
|
||||
print("That would make a good tea sandwich.")
|
||||
case let x where x.hasSuffix("pepper"):
|
||||
print("Is it a spicy \\(x)?")
|
||||
default:
|
||||
print("Everything tastes good in soup.")
|
||||
}// Prints "Is it a spicy red pepper?"
|
||||
```
|
||||
|
||||
Notice how `let` can be used in a pattern to assign the value that matched the pattern to a constant.
|
||||
|
||||
After executing the code inside the switch case that matched, the program exits from the switch statement. Execution doesn't continue to the next case, so you don't need to explicitly break out of the switch at the end of each case's code.
|
||||
|
||||
You use `for`-`in` to iterate over items in a dictionary by providing a pair of names to use for each key-value pair. Dictionaries are an unordered collection, so their keys and values are iterated over in an arbitrary order.
|
||||
|
||||
```swift
|
||||
let interestingNumbers = [
|
||||
"Prime": [2, 3, 5, 7, 11, 13],
|
||||
"Fibonacci": [1, 1, 2, 3, 5, 8],
|
||||
"Square": [1, 4, 9, 16, 25],
|
||||
]
|
||||
|
||||
var largest = 0
|
||||
for (_, numbers) in interestingNumbers {
|
||||
for number in numbers {
|
||||
if number > largest { largest = number }
|
||||
}
|
||||
}
|
||||
print(largest)// Prints "25"
|
||||
```
|
||||
|
||||
The `_` character is a placeholder; It can be useful when it is not necessary to keep track of the loop iterations. It could be omitted in this example.
|
||||
|
||||
Use `while` to repeat a block of code until a condition changes. The condition of a loop can be at the end instead, ensuring that the loop is run at least once.
|
||||
|
||||
```swift
|
||||
var n = 2
|
||||
while n < 100 {
|
||||
n *= 2
|
||||
}
|
||||
print(n)// Prints "128"
|
||||
|
||||
var m = 2
|
||||
repeat {
|
||||
m *= 2
|
||||
} while m < 100
|
||||
print(m)// Prints "128"
|
||||
```
|
||||
|
||||
If you change the condition from `m < 100` to `m < 0`, `while` and `repeat`-`while` behave differently, as `repeat`-`while` would still execute once, even if the condition was already false.
|
||||
|
||||
You can keep an index in a loop by using `..<` to make a range of indexes.
|
||||
|
||||
```swift
|
||||
var total = 0
|
||||
for i in 0..<4 { total += i}
|
||||
print(total)// Prints "6"
|
||||
```
|
||||
|
||||
Use `..<` to make a range that omits its upper value, and use `...` to make a range that includes both values.
|
||||
|
||||
---
|
||||
|
||||
### [Functions and Closures](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/guidedtour/#Functions-and-Closures)
|
||||
|
||||
Use `func` to declare a function. Call a function by following its name with a list of arguments in parentheses. Use `->` to separate the parameter names and types from the function's return type.
|
||||
|
||||
`func greet(person: String, day: String) -> String { return "Hello \\(person), today is \\(day)."}greet(person: "Bob", day: "Tuesday")`
|
||||
|
||||
**Experiment** Remove the `day` parameter. Add a parameter to include today's lunch special in the greeting.
|
||||
|
||||
By default, functions use their parameter names as labels for their arguments. Write a custom argument label before the parameter name, or write `_` to use no argument label.
|
||||
|
||||
`func greet(_ person: String, on day: String) -> String { return "Hello \\(person), today is \\(day)."}greet("John", on: "Wednesday")`
|
||||
|
||||
Use a tuple to make a compound value — for example, to return multiple values from a function. The elements of a tuple can be referred to either by name or by number.
|
||||
|
||||
`func calculateStatistics(scores: [Int]) -> (min: Int, max: Int, sum: Int) { var min = scores[0] var max = scores[0] var sum = 0 for score in scores { if score > max { max = score } else if score < min { min = score } sum += score } return (min, max, sum)}let statistics = calculateStatistics(scores: [5, 3, 100, 3, 9])print(statistics.sum)// Prints "120"print(statistics.2)// Prints "120"`
|
||||
|
||||
Functions can be nested. Nested functions have access to variables that were declared in the outer function. You can use nested functions to organize the code in a function that's long or complex.
|
||||
|
||||
`func returnFifteen() -> Int { var y = 10 func add() { y += 5 } add() return y}returnFifteen()`
|
||||
|
||||
Functions are a first-class type. This means that a function can return another function as its value.
|
||||
|
||||
`func makeIncrementer() -> ((Int) -> Int) { func addOne(number: Int) -> Int { return 1 + number } return addOne}var increment = makeIncrementer()increment(7)`
|
||||
|
||||
A function can take another function as one of its arguments.
|
||||
|
||||
`func hasAnyMatches(list: [Int], condition: (Int) -> Bool) -> Bool { for item in list { if condition(item) { return true } } return false}func lessThanTen(number: Int) -> Bool { return number < 10}var numbers = [20, 19, 7, 12]hasAnyMatches(list: numbers, condition: lessThanTen)`
|
||||
|
||||
Functions are actually a special case of closures: blocks of code that can be called later. The code in a closure has access to things like variables and functions that were available in the scope where the closure was created, even if the closure is in a different scope when it's executed — you saw an example of this already with nested functions. You can write a closure without a name by surrounding code with braces (`{}`). Use `in` to separate the arguments and return type from the body.
|
||||
|
||||
`numbers.map({ (number: Int) -> Int in let result = 3 * number return result})`
|
||||
|
||||
**Experiment** Rewrite the closure to return zero for all odd numbers.
|
||||
|
||||
You have several options for writing closures more concisely. When a closure's type is already known, such as the callback for a delegate, you can omit the type of its parameters, its return type, or both. Single statement closures implicitly return the value of their only statement.
|
||||
|
||||
`let mappedNumbers = numbers.map({ number in 3 * number })print(mappedNumbers)// Prints "[60, 57, 21, 36]"`
|
||||
|
||||
You can refer to parameters by number instead of by name — this approach is especially useful in very short closures. A closure passed as the last argument to a function can appear immediately after the parentheses. When a closure is the only argument to a function, you can omit the parentheses entirely.
|
||||
|
||||
`let sortedNumbers = numbers.sorted { $0 > $1 }print(sortedNumbers)// Prints "[20, 19, 12, 7]"`
|
||||
|
||||
### [Objects and Classes](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/guidedtour/#Objects-and-Classes)
|
||||
|
||||
Use `class` followed by the class's name to create a class. A property declaration in a class is written the same way as a constant or variable declaration, except that it's in the context of a class. Likewise, method and function declarations are written the same way.
|
||||
|
||||
`class Shape { var numberOfSides = 0 func simpleDescription() -> String { return "A shape with \\(numberOfSides) sides." }}`
|
||||
|
||||
**Experiment** Add a constant property with `let`, and add another method that takes an argument.
|
||||
|
||||
Create an instance of a class by putting parentheses after the class name. Use dot syntax to access the properties and methods of the instance.
|
||||
|
||||
`var shape = Shape()shape.numberOfSides = 7var shapeDescription = shape.simpleDescription()`
|
||||
|
||||
This version of the `Shape` class is missing something important: an initializer to set up the class when an instance is created. Use `init` to create one.
|
||||
|
||||
`class NamedShape { var numberOfSides: Int = 0 var name: String init(name: String) { self.name = name } func simpleDescription() -> String { return "A shape with \\(numberOfSides) sides." }}`
|
||||
|
||||
Notice how `self` is used to distinguish the `name` property from the `name` argument to the initializer. The arguments to the initializer are passed like a function call when you create an instance of the class. Every property needs a value assigned — either in its declaration (as with `numberOfSides`) or in the initializer (as with `name`).
|
||||
|
||||
Use `deinit` to create a deinitializer if you need to perform some cleanup before the object is deallocated.
|
||||
|
||||
Subclasses include their superclass name after their class name, separated by a colon. There's no requirement for classes to subclass any standard root class, so you can include or omit a superclass as needed.
|
||||
|
||||
Methods on a subclass that override the superclass's implementation are marked with `override` — overriding a method by accident, without `override`, is detected by the compiler as an error. The compiler also detects methods with `override` that don't actually override any method in the superclass.
|
||||
|
||||
`class Square: NamedShape { var sideLength: Double init(sideLength: Double, name: String) { self.sideLength = sideLength super.init(name: name) numberOfSides = 4 } func area() -> Double { return sideLength * sideLength } override func simpleDescription() -> String { return "A square with sides of length \\(sideLength)." }}let test = Square(sideLength: 5.2, name: "my test square")test.area()test.simpleDescription()`
|
||||
|
||||
**Experiment** Make another subclass of `NamedShape` called `Circle` that takes a radius and a name as arguments to its initializer. Implement an `area()` and a `simpleDescription()` method on the `Circle` class.
|
||||
|
||||
In addition to simple properties that are stored, properties can have a getter and a setter.
|
||||
|
||||
`class EquilateralTriangle: NamedShape { var sideLength: Double = 0.0 init(sideLength: Double, name: String) { self.sideLength = sideLength super.init(name: name) numberOfSides = 3 } var perimeter: Double { get { return 3.0 * sideLength } set { sideLength = newValue / 3.0 } } override func simpleDescription() -> String { return "An equilateral triangle with sides of length \\(sideLength)." }}var triangle = EquilateralTriangle(sideLength: 3.1, name: "a triangle")print(triangle.perimeter)// Prints "9.3"triangle.perimeter = 9.9print(triangle.sideLength)// Prints "3.3000000000000003"`
|
||||
|
||||
In the setter for `perimeter`, the new value has the implicit name `newValue`. You can provide an explicit name in parentheses after `set`.
|
||||
|
||||
Notice that the initializer for the `EquilateralTriangle` class has three different steps:
|
||||
|
||||
1. Setting the value of properties that the subclass declares.
|
||||
2. Calling the superclass's initializer.
|
||||
3. Changing the value of properties defined by the superclass. Any additional setup work that uses methods, getters, or setters can also be done at this point.
|
||||
|
||||
If you don't need to compute the property but still need to provide code that's run before and after setting a new value, use `willSet` and `didSet`. The code you provide is run any time the value changes outside of an initializer. For example, the class below ensures that the side length of its triangle is always the same as the side length of its square.
|
||||
|
||||
`class TriangleAndSquare { var triangle: EquilateralTriangle { willSet { square.sideLength = newValue.sideLength } } var square: Square { willSet { triangle.sideLength = newValue.sideLength } } init(size: Double, name: String) { square = Square(sideLength: size, name: name) triangle = EquilateralTriangle(sideLength: size, name: name) }}var triangleAndSquare = TriangleAndSquare(size: 10, name: "another test shape")print(triangleAndSquare.square.sideLength)// Prints "10.0"print(triangleAndSquare.triangle.sideLength)// Prints "10.0"triangleAndSquare.square = Square(sideLength: 50, name: "larger square")print(triangleAndSquare.triangle.sideLength)// Prints "50.0"`
|
||||
|
||||
When working with optional values, you can write `?` before operations like methods, properties, and subscripting. If the value before the `?` is `nil`, everything after the `?` is ignored and the value of the whole expression is `nil`. Otherwise, the optional value is unwrapped, and everything after the `?` acts on the unwrapped value. In both cases, the value of the whole expression is an optional value.
|
||||
|
||||
`let optionalSquare: Square? = Square(sideLength: 2.5, name: "optional square")let sideLength = optionalSquare?.sideLength`
|
||||
|
||||
### [Enumerations and Structures](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/guidedtour/#Enumerations-and-Structures)
|
||||
|
||||
Use `enum` to create an enumeration. Like classes and all other named types, enumerations can have methods associated with them.
|
||||
|
||||
`enum Rank: Int { case ace = 1 case two, three, four, five, six, seven, eight, nine, ten case jack, queen, king func simpleDescription() -> String { switch self { case .ace: return "ace" case .jack: return "jack" case .queen: return "queen" case .king: return "king" default: return String(self.rawValue) } }}let ace = Rank.acelet aceRawValue = ace.rawValue`
|
||||
|
||||
**Experiment** Write a function that compares two `Rank` values by comparing their raw values.
|
||||
|
||||
By default, Swift assigns the raw values starting at zero and incrementing by one each time, but you can change this behavior by explicitly specifying values. In the example above, `Ace` is explicitly given a raw value of `1`, and the rest of the raw values are assigned in order. You can also use strings or floating-point numbers as the raw type of an enumeration. Use the `rawValue` property to access the raw value of an enumeration case.
|
||||
|
||||
Use the `init?(rawValue:)` initializer to make an instance of an enumeration from a raw value. It returns either the enumeration case matching the raw value or `nil` if there's no matching `Rank`.
|
||||
|
||||
`if let convertedRank = Rank(rawValue: 3) { let threeDescription = convertedRank.simpleDescription()}`
|
||||
|
||||
The case values of an enumeration are actual values, not just another way of writing their raw values. In fact, in cases where there isn't a meaningful raw value, you don't have to provide one.
|
||||
|
||||
`enum Suit { case spades, hearts, diamonds, clubs func simpleDescription() -> String { switch self { case .spades: return "spades" case .hearts: return "hearts" case .diamonds: return "diamonds" case .clubs: return "clubs" } }}let hearts = Suit.heartslet heartsDescription = hearts.simpleDescription()`
|
||||
|
||||
**Experiment** Add a `color()` method to `Suit` that returns "black" for spades and clubs, and returns "red" for hearts and diamonds.
|
||||
|
||||
Notice the two ways that the `hearts` case of the enumeration is referred to above: When assigning a value to the `hearts` constant, the enumeration case `Suit.hearts` is referred to by its full name because the constant doesn't have an explicit type specified. Inside the switch, the enumeration case is referred to by the abbreviated form `.hearts` because the value of `self` is already known to be a suit. You can use the abbreviated form anytime the value's type is already known.
|
||||
|
||||
If an enumeration has raw values, those values are determined as part of the declaration, which means every instance of a particular enumeration case always has the same raw value. Another choice for enumeration cases is to have values associated with the case — these values are determined when you make the instance, and they can be different for each instance of an enumeration case. You can think of the associated values as behaving like stored properties of the enumeration case instance. For example, consider the case of requesting the sunrise and sunset times from a server. The server either responds with the requested information, or it responds with a description of what went wrong.
|
||||
|
||||
`enum ServerResponse { case result(String, String) case failure(String)} let success = ServerResponse.result("6:00 am", "8:09 pm")let failure = ServerResponse.failure("Out of cheese.") switch success {case let .result(sunrise, sunset): print("Sunrise is at \\(sunrise) and sunset is at \\(sunset).")case let .failure(message): print("Failure... \\(message)")}// Prints "Sunrise is at 6:00 am and sunset is at 8:09 pm."`
|
||||
|
||||
**Experiment** Add a third case to `ServerResponse` and to the switch.
|
||||
|
||||
Notice how the sunrise and sunset times are extracted from the `ServerResponse` value as part of matching the value against the switch cases.
|
||||
|
||||
Use `struct` to create a structure. Structures support many of the same behaviors as classes, including methods and initializers. One of the most important differences between structures and classes is that structures are always copied when they're passed around in your code, but classes are passed by reference.
|
||||
|
||||
`struct Card { var rank: Rank var suit: Suit func simpleDescription() -> String { return "The \\(rank.simpleDescription()) of \\(suit.simpleDescription())" }}let threeOfSpades = Card(rank: .three, suit: .spades)let threeOfSpadesDescription = threeOfSpades.simpleDescription()`
|
||||
|
||||
**Experiment** Write a function that returns an array containing a full deck of cards, with one card of each combination of rank and suit.
|
||||
|
||||
### [Concurrency](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/guidedtour/#Concurrency)
|
||||
|
||||
Use `async` to mark a function that runs asynchronously.
|
||||
|
||||
`func fetchUserID(from server: String) async -> Int { if server == "primary" { return 97 } return 501}`
|
||||
|
||||
You mark a call to an asynchronous function by writing `await` in front of it.
|
||||
|
||||
`func fetchUsername(from server: String) async -> String { let userID = await fetchUserID(from: server) if userID == 501 { return "John Appleseed" } return "Guest"}`
|
||||
|
||||
Use `async let` to call an asynchronous function, letting it run in parallel with other asynchronous code. When you use the value it returns, write `await`.
|
||||
|
||||
`func connectUser(to server: String) async { async let userID = fetchUserID(from: server) async let username = fetchUsername(from: server) let greeting = await "Hello \\(username), user ID \\(userID)" print(greeting)}`
|
||||
|
||||
Use `Task` to call asynchronous functions from synchronous code, without waiting for them to return.
|
||||
|
||||
`Task { await connectUser(to: "primary")}// Prints "Hello Guest, user ID 97"`
|
||||
|
||||
Use task groups to structure concurrent code.
|
||||
|
||||
`let userIDs = await withTaskGroup(of: Int.self) { group in for server in ["primary", "secondary", "development"] { group.addTask { return await fetchUserID(from: server) } } var results: [Int] = [] for await result in group { results.append(result) } return results}`
|
||||
|
||||
Actors are similar to classes, except they ensure that different asynchronous functions can safely interact with an instance of the same actor at the same time.
|
||||
|
||||
`actor ServerConnection { var server: String = "primary" private var activeUsers: [Int] = [] func connect() async -> Int { let userID = await fetchUserID(from: server) // ... communicate with server ... activeUsers.append(userID) return userID }}`
|
||||
|
||||
When you call a method on an actor or access one of its properties, you mark that code with `await` to indicate that it might have to wait for other code that's already running on the actor to finish.
|
||||
|
||||
`let server = ServerConnection()let userID = await server.connect()`
|
||||
|
||||
### [Protocols and Extensions](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/guidedtour/#Protocols-and-Extensions)
|
||||
|
||||
Use `protocol` to declare a protocol.
|
||||
|
||||
`protocol ExampleProtocol { var simpleDescription: String { get } mutating func adjust()}`
|
||||
|
||||
Classes, enumerations, and structures can all adopt protocols.
|
||||
|
||||
`class SimpleClass: ExampleProtocol { var simpleDescription: String = "A very simple class." var anotherProperty: Int = 69105 func adjust() { simpleDescription += " Now 100% adjusted." }}var a = SimpleClass()a.adjust()let aDescription = a.simpleDescription struct SimpleStructure: ExampleProtocol { var simpleDescription: String = "A simple structure" mutating func adjust() { simpleDescription += " (adjusted)" }}var b = SimpleStructure()b.adjust()let bDescription = b.simpleDescription`
|
||||
|
||||
**Experiment** Add another requirement to `ExampleProtocol`. What changes do you need to make to `SimpleClass` and `SimpleStructure` so that they still conform to the protocol?
|
||||
|
||||
Notice the use of the `mutating` keyword in the declaration of `SimpleStructure` to mark a method that modifies the structure. The declaration of `SimpleClass` doesn't need any of its methods marked as mutating because methods on a class can always modify the class.
|
||||
|
||||
Use `extension` to add functionality to an existing type, such as new methods and computed properties. You can use an extension to add protocol conformance to a type that's declared elsewhere, or even to a type that you imported from a library or framework.
|
||||
|
||||
`extension Int: ExampleProtocol { var simpleDescription: String { return "The number \\(self)" } mutating func adjust() { self += 42 } }print(7.simpleDescription)// Prints "The number 7"`
|
||||
|
||||
**Experiment** Write an extension for the `Double` type that adds an `absoluteValue` property.
|
||||
|
||||
You can use a protocol name just like any other named type — for example, to create a collection of objects that have different types but that all conform to a single protocol. When you work with values whose type is a boxed protocol type, methods outside the protocol definition aren't available.
|
||||
|
||||
`let protocolValue: any ExampleProtocol = aprint(protocolValue.simpleDescription)// Prints "A very simple class. Now 100% adjusted."// print(protocolValue.anotherProperty) // Uncomment to see the error`
|
||||
|
||||
Even though the variable `protocolValue` has a runtime type of `SimpleClass`, the compiler treats it as the given type of `ExampleProtocol`. This means that you can't accidentally access methods or properties that the class implements in addition to its protocol conformance.
|
||||
|
||||
### [Error Handling](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/guidedtour/#Error-Handling)
|
||||
|
||||
You represent errors using any type that adopts the `Error` protocol.
|
||||
|
||||
`enum PrinterError: Error { case outOfPaper case noToner case onFire}`
|
||||
|
||||
Use `throw` to throw an error and `throws` to mark a function that can throw an error. If you throw an error in a function, the function returns immediately and the code that called the function handles the error.
|
||||
|
||||
`func send(job: Int, toPrinter printerName: String) throws -> String { if printerName == "Never Has Toner" { throw PrinterError.noToner } return "Job sent"}`
|
||||
|
||||
There are several ways to handle errors. One way is to use `do`-`catch`. Inside the `do` block, you mark code that can throw an error by writing `try` in front of it. Inside the `catch` block, the error is automatically given the name `error` unless you give it a different name.
|
||||
|
||||
`do { let printerResponse = try send(job: 1040, toPrinter: "Bi Sheng") print(printerResponse)} catch { print(error)}// Prints "Job sent"`
|
||||
|
||||
**Experiment** Change the printer name to `"Never Has Toner"`, so that the `send(job:toPrinter:)` function throws an error.
|
||||
|
||||
You can provide multiple `catch` blocks that handle specific errors. You write a pattern after `catch` just as you do after `case` in a switch.
|
||||
|
||||
`do { let printerResponse = try send(job: 1440, toPrinter: "Gutenberg") print(printerResponse)} catch PrinterError.onFire { print("I'll just put this over here, with the rest of the fire.")} catch let printerError as PrinterError { print("Printer error: \\(printerError).")} catch { print(error)}// Prints "Job sent"`
|
||||
|
||||
**Experiment** Add code to throw an error inside the `do` block. What kind of error do you need to throw so that the error is handled by the first `catch` block? What about the second and third blocks?
|
||||
|
||||
Another way to handle errors is to use `try?` to convert the result to an optional. If the function throws an error, the specific error is discarded and the result is `nil`. Otherwise, the result is an optional containing the value that the function returned.
|
||||
|
||||
`let printerSuccess = try? send(job: 1884, toPrinter: "Mergenthaler")let printerFailure = try? send(job: 1885, toPrinter: "Never Has Toner")`
|
||||
|
||||
Use `defer` to write a block of code that's executed after all other code in the function, just before the function returns. The code is executed regardless of whether the function throws an error. You can use `defer` to write setup and cleanup code next to each other, even though they need to be executed at different times.
|
||||
|
||||
`var fridgeIsOpen = falselet fridgeContent = ["milk", "eggs", "leftovers"] func fridgeContains(_ food: String) -> Bool { fridgeIsOpen = true defer { fridgeIsOpen = false } let result = fridgeContent.contains(food) return result}if fridgeContains("banana") { print("Found a banana")}print(fridgeIsOpen)// Prints "false"`
|
||||
|
||||
### [Generics](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/guidedtour/#Generics)
|
||||
|
||||
Write a name inside angle brackets to make a generic function or type.
|
||||
|
||||
`func makeArray<Item>(repeating item: Item, numberOfTimes: Int) -> [Item] { var result: [Item] = [] for _ in 0..<numberOfTimes { result.append(item) } return result}makeArray(repeating: "knock", numberOfTimes: 4)`
|
||||
|
||||
You can make generic forms of functions and methods, as well as classes, enumerations, and structures.
|
||||
|
||||
`// Reimplement the Swift standard library's optional typeenum OptionalValue<Wrapped> { case none case some(Wrapped)}var possibleInteger: OptionalValue<Int> = .nonepossibleInteger = .some(100)`
|
||||
|
||||
Use `where` right before the body to specify a list of requirements — for example, to require the type to implement a protocol, to require two types to be the same, or to require a class to have a particular superclass.
|
||||
|
||||
`func anyCommonElements<T: Sequence, U: Sequence>(_ lhs: T, _ rhs: U) -> Bool where T.Element: Equatable, T.Element == U.Element{ for lhsItem in lhs { for rhsItem in rhs { if lhsItem == rhsItem { return true } } } return false}anyCommonElements([1, 2, 3], [3])`
|
||||
|
||||
**Experiment** Modify the `anyCommonElements(_:_:)` function to make a function that returns an array of the elements that any two sequences have in common.
|
||||
|
||||
Writing `<T: Equatable>` is the same as writing `<T> ... where T: Equatable`.
|
||||
|
||||
## Sources
|
||||
|
||||
- [A Swift Tour | Documentation](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/guidedtour/)
|
||||
Reference in New Issue
Block a user