mirror of
https://gitlab.com/djdietrick/docs
synced 2026-05-03 00:20:54 -04:00
Started rust basics
This commit is contained in:
@@ -1,35 +1,3 @@
|
||||
const pythonSidebar = [
|
||||
{
|
||||
text: 'Python Basics',
|
||||
items: [
|
||||
{text: 'Introduction', link: '/python/'},
|
||||
{text: 'Types', link: '/python/basics/types'},
|
||||
{text: 'Classes', link: '/python/basics/classes'},
|
||||
{text: 'Functions', link: '/python/basics/functions'},
|
||||
{text: 'Control Flow', link: '/python/basics/controlflow'},
|
||||
{text: 'Decorators', link: '/python/basics/decorators'},
|
||||
{text: 'Exceptions', link: '/python/basics/exceptions'},
|
||||
{text: 'Enums', link: '/python/basics/enums'},
|
||||
{text: 'Generators', link: '/python/basics/generators'},
|
||||
{text: 'Virtual Environment', link: '/python/basics/venv'}
|
||||
]
|
||||
},
|
||||
{
|
||||
text: 'Advanced',
|
||||
items: [
|
||||
{text: 'Collections', link: '/python/advanced/collections'},
|
||||
{text: 'Dates', link: '/python/advanced/dates'},
|
||||
{text: 'Debugging', link: '/python/advanced/debugging'},
|
||||
{text: 'Files', link: '/python/advanced/files'},
|
||||
{text: 'Math', link: '/python/advanced/math'},
|
||||
{text: 'Regex', link: '/python/advanced/regex'},
|
||||
{text: 'Testing', link: '/python/advanced/tests'},
|
||||
{text: 'Timing', link: '/python/advanced/timing'},
|
||||
{text: 'Zipping', link: '/python/advanced/zip'}
|
||||
]
|
||||
}
|
||||
]
|
||||
|
||||
export default {
|
||||
title: 'Docs.Dietrick.Dev',
|
||||
descript: 'A collection of notes and snippets',
|
||||
@@ -38,7 +6,8 @@ export default {
|
||||
{ icon: 'github', link: 'https://gitlab.com/djdietrick/docs'}
|
||||
],
|
||||
sidebar: {
|
||||
'/python/': pythonSidebar,
|
||||
'/python/': require('../python/sidebar.json'),
|
||||
'/rust/': require('../rust/sidebar.json'),
|
||||
'/': [
|
||||
{
|
||||
text: 'Home',
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
# Languages
|
||||
|
||||
[Python](/python/)
|
||||
[Python](/python/)
|
||||
|
||||
[Rust](/rust/)
|
||||
31
docs/python/sidebar.json
Normal file
31
docs/python/sidebar.json
Normal file
@@ -0,0 +1,31 @@
|
||||
[
|
||||
{
|
||||
"text": "Python Basics",
|
||||
"items": [
|
||||
{"text": "Introduction", "link": "/python/"},
|
||||
{"text": "Types", "link": "/python/basics/types"},
|
||||
{"text": "Classes", "link": "/python/basics/classes"},
|
||||
{"text": "Functions", "link": "/python/basics/functions"},
|
||||
{"text": "Control Flow", "link": "/python/basics/controlflow"},
|
||||
{"text": "Decorators", "link": "/python/basics/decorators"},
|
||||
{"text": "Exceptions", "link": "/python/basics/exceptions"},
|
||||
{"text": "Enums", "link": "/python/basics/enums"},
|
||||
{"text": "Generators", "link": "/python/basics/generators"},
|
||||
{"text": "Virtual Environment", "link": "/python/basics/venv"}
|
||||
]
|
||||
},
|
||||
{
|
||||
"text": "Advanced",
|
||||
"items": [
|
||||
{"text": "Collections", "link": "/python/advanced/collections"},
|
||||
{"text": "Dates", "link": "/python/advanced/dates"},
|
||||
{"text": "Debugging", "link": "/python/advanced/debugging"},
|
||||
{"text": "Files", "link": "/python/advanced/files"},
|
||||
{"text": "Math", "link": "/python/advanced/math"},
|
||||
{"text": "Regex", "link": "/python/advanced/regex"},
|
||||
{"text": "Testing", "link": "/python/advanced/tests"},
|
||||
{"text": "Timing", "link": "/python/advanced/timing"},
|
||||
{"text": "Zipping", "link": "/python/advanced/zip"}
|
||||
]
|
||||
}
|
||||
]
|
||||
75
docs/rust/basics/controlflow.md
Normal file
75
docs/rust/basics/controlflow.md
Normal 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
20
docs/rust/basics/enums.md
Normal 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
44
docs/rust/basics/funcs.md
Normal 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)
|
||||
}
|
||||
```
|
||||
2
docs/rust/basics/structs.md
Normal file
2
docs/rust/basics/structs.md
Normal file
@@ -0,0 +1,2 @@
|
||||
# Structs
|
||||
|
||||
95
docs/rust/basics/types.md
Normal file
95
docs/rust/basics/types.md
Normal 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
2
docs/rust/index.md
Normal file
@@ -0,0 +1,2 @@
|
||||
# Rust
|
||||
|
||||
13
docs/rust/sidebar.json
Normal file
13
docs/rust/sidebar.json
Normal 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"}
|
||||
]
|
||||
}
|
||||
]
|
||||
Reference in New Issue
Block a user