From 5a79cab7ae371e3694278cf83a1ecd156658bf68 Mon Sep 17 00:00:00 2001 From: Dave Dietrick Date: Fri, 16 Sep 2022 14:46:05 -0400 Subject: [PATCH] Started rust basics --- docs/.vitepress/config.js | 35 +----------- docs/languages.md | 4 +- docs/python/sidebar.json | 31 +++++++++++ docs/rust/basics/controlflow.md | 75 ++++++++++++++++++++++++++ docs/rust/basics/enums.md | 20 +++++++ docs/rust/basics/funcs.md | 44 +++++++++++++++ docs/rust/basics/structs.md | 2 + docs/rust/basics/types.md | 95 +++++++++++++++++++++++++++++++++ docs/rust/index.md | 2 + docs/rust/sidebar.json | 13 +++++ 10 files changed, 287 insertions(+), 34 deletions(-) create mode 100644 docs/python/sidebar.json create mode 100644 docs/rust/basics/controlflow.md create mode 100644 docs/rust/basics/enums.md create mode 100644 docs/rust/basics/funcs.md create mode 100644 docs/rust/basics/structs.md create mode 100644 docs/rust/basics/types.md create mode 100644 docs/rust/index.md create mode 100644 docs/rust/sidebar.json diff --git a/docs/.vitepress/config.js b/docs/.vitepress/config.js index 114e694..ced5625 100644 --- a/docs/.vitepress/config.js +++ b/docs/.vitepress/config.js @@ -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', diff --git a/docs/languages.md b/docs/languages.md index 255b1d0..63c4265 100644 --- a/docs/languages.md +++ b/docs/languages.md @@ -1,3 +1,5 @@ # Languages -[Python](/python/) \ No newline at end of file +[Python](/python/) + +[Rust](/rust/) \ No newline at end of file diff --git a/docs/python/sidebar.json b/docs/python/sidebar.json new file mode 100644 index 0000000..b0eacb4 --- /dev/null +++ b/docs/python/sidebar.json @@ -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"} + ] + } +] \ No newline at end of file diff --git a/docs/rust/basics/controlflow.md b/docs/rust/basics/controlflow.md new file mode 100644 index 0000000..5a9a0e0 --- /dev/null +++ b/docs/rust/basics/controlflow.md @@ -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!!!"); +``` \ No newline at end of file diff --git a/docs/rust/basics/enums.md b/docs/rust/basics/enums.md new file mode 100644 index 0000000..ba5cdd2 --- /dev/null +++ b/docs/rust/basics/enums.md @@ -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")); +``` \ No newline at end of file diff --git a/docs/rust/basics/funcs.md b/docs/rust/basics/funcs.md new file mode 100644 index 0000000..fe67d87 --- /dev/null +++ b/docs/rust/basics/funcs.md @@ -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(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) +} +``` \ No newline at end of file diff --git a/docs/rust/basics/structs.md b/docs/rust/basics/structs.md new file mode 100644 index 0000000..76a390a --- /dev/null +++ b/docs/rust/basics/structs.md @@ -0,0 +1,2 @@ +# Structs + diff --git a/docs/rust/basics/types.md b/docs/rust/basics/types.md new file mode 100644 index 0000000..cec8872 --- /dev/null +++ b/docs/rust/basics/types.md @@ -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 { +// Some(T), +// None +// } +let _absent_option: Option = None; +let _present_option: Option = Some(5); + +// Results +// enum Result { +// 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]; +``` diff --git a/docs/rust/index.md b/docs/rust/index.md new file mode 100644 index 0000000..3b4f611 --- /dev/null +++ b/docs/rust/index.md @@ -0,0 +1,2 @@ +# Rust + diff --git a/docs/rust/sidebar.json b/docs/rust/sidebar.json new file mode 100644 index 0000000..d8a16bf --- /dev/null +++ b/docs/rust/sidebar.json @@ -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"} + ] + } +] \ No newline at end of file