diff --git a/rust-intro.org b/rust-intro.org index 2bd0a81..fe2d2aa 100644 --- a/rust-intro.org +++ b/rust-intro.org @@ -69,7 +69,7 @@ - Careless (use of ~unsafe~ in) libraries can secretly break safety guarantees. -** Problems and security Rust tries to conquer +** Problems Rust tries to conquer #+BEGIN_CENTER #+LaTeX:\includegraphics[width = 0.8\textwidth]{img/compiler_complaint.png} [fn:xkcd_dangling_pointer] @@ -93,14 +93,14 @@ --nomicon~) - Use '~$ rustup help docs~' to get an overview -*** Other helpful sites +*** Compact "cheat sheets" - [[https://cheats.rs][cheats.rs]] - [[https://programming-idioms.org/cheatsheet/Rust][programming-idioms.org]] ** Where to share, find help and stay up to date *** Online sharing/testing - [[https://play.rust-lang.org/][play.rust-lang.org]] - - [[https://godbolt.org/][godbolt.org]] (Not only for Rust, but it is also supported) + - [[https://godbolt.org/][godbolt.org]] (Not only Rust is supported) *** Chat (Discord) - [[https://discord.gg/rust-lang][The Rust Programming Language]] @@ -115,9 +115,11 @@ - Semicolons have meaning (statements vs. expressions) - Mutability - Shadowing + - Memory safety + - Ownership, references and lifetimes - Functional paradigms - Pattern matching - - Memory safety + - Metaprogramming (declarative and procedural macros) - Meaningful compiler warnings and errors ** Statements vs. expressions @@ -127,7 +129,7 @@ fn_call(); #+END_SRC *** Expressions - ~false~, ~x + y~, ~fn_call()~ + ~false~, ~x + y~, ~fn_call()~, ~if~, ~match~ *** Example #+BEGIN_SRC rust let bool_value = true; @@ -156,16 +158,18 @@ let baz = baz + 3; #+END_SRC -** Type aliases -*** Type aliases example - #+BEGIN_SRC rust - type Frequency = u64; - let clock: Frequency = 8_000_000 +# ** Type aliases +# The ~type~ keywoard declares an alias of another type. - #[allow(non_camel_case_types)] - type u64_t = u64; - let another_clock: Frequency = 16_000_000 as u64_t; - #+END_SRC +# *** Type aliases example +# #+BEGIN_SRC rust +# type Frequency = u64; +# let clock: Frequency = 8_000_000 + +# #[allow(non_camel_case_types)] +# type u64_t = u64; +# let another_clock: Frequency = 16_000_000 as u64_t; +# #+END_SRC ** Tuple structs *** Definition of a tuple struct @@ -208,14 +212,15 @@ let new_event = Event::Send("A message".to_owned()); #+END_SRC -** ~if let~ - ** Pattern matching and some macros + + @@latex:{\color{red}@@All match arms must return the same type!!1!@@latex:}@@ + *** Simple ~match~ #+BEGIN_SRC rust match an_enum { MyEnum::Something => todo!("This has to be done"), - MyEnum::SomethingElse(_) => unimplemented!("This is not implemented"), + MyEnum::SmthngElse(_) => unimplemented!("This is unimplemented"), MyEnum::AnotherThing(e) => println!("It is another thing: {}", e), _ => unreachable!(), }; @@ -231,6 +236,16 @@ }; #+END_SRC +** ~if let~ +*** Simpler ~match~ + #+BEGIN_SRC rust + if let Some(inner_value) = foo { + println!("Value: {}", inner_value); + } else { + println!("Nothing found"); + } + #+END_SRC + ** Loops[fn:reference_loops] *** Loops - ~loop~: infinite loops @@ -243,6 +258,44 @@ - ~continue~: exit the current iteration and jump to the loop header +** Ownership + /Ownership/ is one of Rusts central concepts to achieve memory + safety without garbage collection. + +*** Ownership Rules + - Each value has a variable that is called its /owner/. + - There can only be one owner at a time. + - When the owner goes out of scope, the value will be dropped. + +** References + - A value can temporarily borrowed from its owner without + transferring ownership. + - There can be multiple immutable references. + - There can only be one mutable reference. + - The ~&~ symbol is the reference operator. + - The ~*~ symbol is the dereference operator. + - References are immutable by default, use the ~mut~ keyword to + make them mutable. + +** Lifetimes + - The Rust compiler (borrow checker) keeps track of how long + references (borrows) are valid. + - To ensure valid borrows and therefor prevent dangling borrows + /Lifetimes/ are used. + - /Lifetimes/ are annotations which tell the borrow checker how + long a borrow needs to be valid. + - /Lifetimes/ are implicit and inferred, that means the compiler + infers /lifetimes/ wherever possible. + - /Lifetimes/ must be annotated when the borrow checker cannot + infer them on its own. + +*** Struct with /lifetimes/ + #+BEGIN_SRC rust + struct SomeApp<'a> { + config: &'a Config, + } + #+END_SRC + ** Error related types - Heavy use of ~Option~ and ~Result~ @@ -327,7 +380,7 @@ #+END_SRC -** Use more functional language features +** Use functional paradigms - ~vec![1, 2, 3].iter()~ - ~vec![1, 2, 3].iter().sum()~ - ~vec![1, 2, 3].iter().map(|x| x + 1).collect()~ @@ -440,9 +493,11 @@ #+END_CENTER ** Install the Rust-Toolchain -*** Install Rust - From [[https://rustup.rs][rustup.rs]]: - ~$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh~ +*** Install the Rust toolchain ([[https://rustup.rs][~rustup.rs~]]) + #+BEGIN_SRC sh + $ curl --proto '=https' --tlsv1.2 -sSf \ + https://sh.rustup.rs | sh + #+END_SRC *** Rustup: The Rust toolchain installer - Check for new versions: ~$ rustup check~ @@ -454,60 +509,40 @@ - Activate nightly locally (in current dir): ~$ rustup override set nightly~ -** Handle the Rust toolchain with ~rustup~ - -*** Install the Rust toolchain ([[https://rustup.rs][~rustup.rs~]]) - #+BEGIN_SRC sh - $ curl --proto '=https' --tlsv1.2 -sSf \ - https://sh.rustup.rs | sh - #+END_SRC - -*** Update ~rustup~ - #+BEGIN_SRC sh - $ rustup self update - #+END_SRC - -*** Check for updates for installed toolchains - #+BEGIN_SRC sh - $ rustup check - #+END_SRC - -*** Update toolchains if outdated - #+BEGIN_SRC sh - $ rustup update - #+END_SRC - ** IDE support aka are we IDE yet?[fn:areweideyet] \tiny +#+attr_latex: :align c|ccccccccc | | \rotatebox{90}{Syntax highlightning (.rs)} | \rotatebox{90}{Syntax highlightning (.toml)} | \rotatebox{90}{Snippets} | \rotatebox{90}{Code Completion} | \rotatebox{90}{Linting} | \rotatebox{90}{Code Formatting} | \rotatebox{90}{Go-to Definiton} | \rotatebox{90}{Debugging} | \rotatebox{90}{Documentation Tooltips} | |---------------------+--------------------------------------------+----------------------------------------------+--------------------------+---------------------------------+-------------------------+---------------------------------+---------------------------------+---------------------------+----------------------------------------| -| Atom | X | X | X | X | X | X | X | | X | -| Emacs | X | X | X | X | X | X | X | | X | -| Sublime | X | X | X | X | X | X | X | | | -| Vim/Neovim | X | X | X | X | X | X | X | | X | -| VS Code | X | X | X | X | X | X | X | X | X | -| BBedit | X | X | X | | | X | X | | | -| Geany | X | | | | | | | | | -| gedit | X | | | X | | | X | | | -| Kakoune | X | X | X | X | X | X | X | | X | -| Kate | X | X | | X | X | X | X | | | -| Micro | X | X | | | X | X | | | | -| Midnight Commander | X | | | | | | | | | -| Textadept | X | X | X | X | X | | X | | | +| Atom | X | X | X | X | X | X | X | | X | +| Emacs | X | X | X | X | X | X | X | | X | +| Sublime | X | X | X | X | X | X | X | | | +| Vim/Neovim | X | X | X | X | X | X | X | | X | +| VS Code | X | X | X | X | X | X | X | X | X | +| BBedit | X | X | X | | | X | X | | | +| Geany | X | | | | | | | | | +| gedit | X | | | X | | | X | | | +| Kakoune | X | X | X | X | X | X | X | | X | +| Kate | X | X | | X | X | X | X | | | +| Micro | X | X | | | X | X | | | | +| Midnight Commander | X | | | | | | | | | +| Textadept | X | X | X | X | X | | X | | | |---------------------+--------------------------------------------+----------------------------------------------+--------------------------+---------------------------------+-------------------------+---------------------------------+---------------------------------+---------------------------+----------------------------------------| -| Eclipse | X | | X | X | X | X | X | X | X | -| IntelliJ-based IDEs | X | X | X | X | X | X | X | X | X | -| Visual Studio | X | | | X | | | X | X | | -| GNOME Builder | X | | X | X | X | X | X | | | -| Ride | X | | | | | | | | | +| Eclipse | X | | X | X | X | X | X | X | X | +| IntelliJ-based IDEs | X | X | X | X | X | X | X | X | X | +| Visual Studio | X | | | X | | | X | X | | +| GNOME Builder | X | | X | X | X | X | X | | | +| Ride | X | | | | | | | | | * Common Crates ** General - - [[https://crates.io][crates.io]]/[[https://lib.rs][lib.rs]] - - [[https://github.com/rust-unofficial/awesome-rust][Awesome Rust]] +*** General + - [[https://crates.io][crates.io]]/[[https://lib.rs][lib.rs]] + - [[https://github.com/rust-unofficial/awesome-rust][Awesome Rust]] - - dotenv - - [[https://tokio.rs/][tokio]] +*** Misc. Crates + - dotenv + - [[https://tokio.rs/][tokio]] ** Parsing/Encoding - clap: The command line argument parser (if starting a new project @@ -559,6 +594,11 @@ - refinery - barrel +** Tui + - rustyline: Line editing library + - tui: Library for ncurses like things + - crossterm: Cross-platform terminal manipulation library + ** Gui ([[https://www.areweguiyet.com/][areweguiyet.com]]) A small selection of crates I used/tried so far: - [[https://sixtyfps.io/][SixtyFPS]] (WASM support) @@ -588,12 +628,11 @@ # - arm ** Unikernel - https://unikernel.org - + For general information about unikernels visit [[https://unikernel.org][unikernel.org]]. - [[https://github.com/hermitcore/rusty-hermit][RustyHermit]] ** Thats it for now - \Huge + \Large #+BEGIN_CENTER Thank you for your attention! @@ -603,12 +642,31 @@ \normalsize *** To be continued - - Lifetimes - - Borrowing and Arrowing - - Traits - - Generics - - ~macro_rules!~ - - ~unsafe~ Rust + +**** Column left + :PROPERTIES: + :BEAMER_col: 0.45 + :END: + - Traits + - Generics + - Type Aliases vs. New Type Idioms + - Ownership + - References and Slices + - Lifetimes + - Iterators + - Functional paradigms + +**** Column right + :PROPERTIES: + :BEAMER_col: 0.45 + :END: + - Metaprogramming (macros) + - (Smart) Pointers + - Concurrency + - Object Oriented Programming + - Closures + - Async-Await + - ~unsafe~ Rust * Footnotes [fn:rust_fungus] [[https://commons.wikimedia.org/wiki/File:Bruine_roest_op_tarwe_(Puccinia_recondita_f.sp._tritici_on_Triticum_aestivum).jpg][Wikimedia]]