Started rust basics

This commit is contained in:
2022-09-16 14:46:05 -04:00
parent c424c56ece
commit 5a79cab7ae
10 changed files with 287 additions and 34 deletions

View File

@@ -0,0 +1,75 @@
# Control Flow
## If/Else
```rust
let num = 5;
if num < 3 {
println!("Less than 3");
} else if num <= 3 && num < 7 {
println!("In the sweet spot");
} else {
println!("Greater!");
}
```
### With Options and Results
## Loops
### Loop
The `loop` keyword is essentially a `while(true)` loop, it continues to execute the block until you explicitly tell it to stop.
You can also return values from loops by using `break` followed by the return value, and setting that equal to a variable.
```rust
loop {
println!("again!");
}
let mut counter = 0;
let result = loop {
counter += 1;
if counter == 10 {
break counter * 2;
}
};
println!("The result is {result}");
```
### While loops
Pretty much the same as while loops in any other language.
```rust
let mut number = 3;
while number != 0 {
println!("{number}!");
number -= 1;
}
println!("LIFTOFF!!!");
```
### For loops
For loops in Rust are always for in loops.
```rust
let a = [10, 20, 30, 40, 50];
for element in a {
println!("the value is: {element}");
}
for number in (1..4).rev() {
println!("{number}!");
}
println!("LIFTOFF!!!");
```

20
docs/rust/basics/enums.md Normal file
View File

@@ -0,0 +1,20 @@
# Enums
```rust
// Simple enum
enum Difficulty {
Easy,
Medium,
Hard
}
let _diff = Difficulty::Easy;
// Store values in enums
enum Message {
Quit,
Move { x: i32, y: i32 },
Write(String),
ChangeColor(i32, i32, i32),
}
let _mess = Message::Write(String::from("message"));
```

44
docs/rust/basics/funcs.md Normal file
View File

@@ -0,0 +1,44 @@
# Functions
```rust
// Basic void function, no arguments
#[allow(dead_code)]
pub fn basic() {
println!("Basic test!");
}
// Return value
#[allow(dead_code)]
fn five() -> i32 {
5
}
#[allow(dead_code)]
fn take_ownership(x: i32) {
println!("I own {} now", x);
}
#[allow(dead_code)]
fn borrow(x: &i32) {
println!("I'm just borrowing {}, you can use it again later", x);
}
#[allow(dead_code)]
fn generic<T: PartialOrd + Copy>(list: &[T]) -> T {
let mut largest = list[0];
for &item in list {
if item > largest {
largest = item;
}
}
largest
}
// Fnctions as arguments
#[allow(dead_code)]
fn do_twice(f: fn(i32) -> i32, arg: i32) -> i32 {
f(arg) + f(arg)
}
```

View File

@@ -0,0 +1,2 @@
# Structs

95
docs/rust/basics/types.md Normal file
View File

@@ -0,0 +1,95 @@
# Types
## Scalar Types
```rust
// Integer, either signed or unsigned, bits in (8,16,32,64,128)
let _num: i32 = -5;
let _unsigned_num: u32 = 5;
// Floats, either 32 or 64 bit
let _f = 5.0; // Defaults to f64
let _f2: f32 = 3.9;
// Boolean
let _b: bool = true;
// Char, specified by single-quotes
let _c: char = 'c';
// Options
// enum Option<T> {
// Some(T),
// None
// }
let _absent_option: Option<i32> = None;
let _present_option: Option<i32> = Some(5);
// Results
// enum Result<T,E> {
// Ok(T),
// Err(E)
// }
```
## Slices
A slice is a reference to a sequence of elements in a collection. Since it is a reference, it does not have ownership.
```rust
// Slice
let s = String::from("hello world");
// String slice &str
let _hello: &str = &s[0..5];
let _world = &s[6..11];
let _world2 = &s[6..];
let _whole = &s[..];
// Works with both &str and String
fn first_word(s: &str) -> &str {
s
}
// Can also slice arrays
let a = [1, 2, 3, 4, 5];
let _slice = &a[1..3];
```
## Tuples
Tuples have a fixed length, and are immutable.
```rust
// Tuples have a fixed length, once declared you can't change
let tup: (i32, f64, u8) = (500, 6.4, 1);
// Destructuring
let (_x,_y,_z) = tup;
//Accessing
let _five_hundred = tup.0;
let _six_point_four = tup.1;
```
## Arrays
Arrays in Rust have a fixed length. They allocate on teh stack instead of on the heap.
```rust
let a = [1, 2, 3, 4, 5];
// Specify type and length
let b: [i32; 5] = [1, 2, 3, 4, 5];
// Create with initial value
let _c = [3; 5]; // [3,3,3,3,3]
// Accessing
let _one = a[0];
let _two = b[1];
// Won't compile because we know the size at compile time
// let n = a[10];
```

2
docs/rust/index.md Normal file
View File

@@ -0,0 +1,2 @@
# Rust

13
docs/rust/sidebar.json Normal file
View File

@@ -0,0 +1,13 @@
[
{
"text": "Rust Basics",
"items": [
{"text": "Introduction", "link": "/rust/"},
{"text": "Types", "link": "/rust/basics/types"},
{"text": "Enums", "link": "/rust/basics/enums"},
{"text": "Functions", "link": "/rust/basics/funcs"},
{"text": "Control Flow", "link": "/rust/basics/controlflow"},
{"text": "Structs", "link": "/rust/basics/structs"}
]
}
]