From 1db227f54078ad1a32b6e2a0d705456b62f09327 Mon Sep 17 00:00:00 2001 From: finga Date: Mon, 6 Sep 2021 14:07:21 +0200 Subject: [PATCH 01/13] Improve wording Improve wording for intelligibility. --- rust-intro.org | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/rust-intro.org b/rust-intro.org index ea61fa3..a4485df 100644 --- a/rust-intro.org +++ b/rust-intro.org @@ -36,7 +36,7 @@ *** Rust, the language - A bit more than 10 years old (2010) - - Memory safe without gcing, optional ref counting + - Memory safe without gcing, optional reference counting - Ownership, lifetimes, traits, functional paradigms - Variables are immutable by default and can be shadowed - Performance of idiomatic Rust is comparable to the performance @@ -44,7 +44,7 @@ - A well designed language and ecosystem ** Strengths and Weaknesses[fn:2] -*** Things Rust does measurably really well +*** Things Rust does measurably really well (Strengths) - Compiled code [[https://benchmarksgame-team.pages.debian.net/benchmarksgame/which-programs-are-fastest.html][about same performance as C / C++]], and excellent [[https://dl.acm.org/doi/10.1145/3136014.3136031][memory and energy efficiency]]. - Can [[https://www.chromium.org/Home/chromium-security/memory-safety][avoid 70% of all safety issues]] present in C / C++, and most @@ -55,10 +55,10 @@ LLVM).[fn:3] - [[https://insights.stackoverflow.com/survey/2021#technology-most-loved-dreaded-and-wanted]["Most loved language"]] for 6 years in a row. - Modern tooling: ~cargo~ (builds just work), ~clippy~ (450+ code - quality lints), ~rustup~ (easy toolchain mgmt). + quality lints), ~rustup~ (easy toolchain management). ** Strengths and Weaknesses[fn:2] -*** Points you might run into +*** Points you might run into (Weaknesses) - Steep learning curve[fn:4] compiler enforcing (esp. memory) rules that would be "best practices" elsewhere. - Missing Rust-native libs in some domains, target platforms From 32bcca1530ae9e972a9c9e2fbecbfea4aa2724cd Mon Sep 17 00:00:00 2001 From: finga Date: Tue, 21 Sep 2021 10:26:56 +0200 Subject: [PATCH 02/13] Add content --- rust-intro.org | 104 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 96 insertions(+), 8 deletions(-) diff --git a/rust-intro.org b/rust-intro.org index a4485df..f15904b 100644 --- a/rust-intro.org +++ b/rust-intro.org @@ -70,34 +70,122 @@ - Careless (use of ~unsafe~ in) libraries can secretly break safety guarantees. +** Where to start? +*** First Steps + - [[https://tourofrust.com/TOC_en.html][Tour of Rust]] + - [[https://doc.rust-lang.org/book/][The Rust Book]] (~$ rustup docs --book~) + - [[https://dhghomon.github.io/easy_rust/Chapter_1.html][Rust in Easy English]] + - [[https://doc.rust-lang.org/rust-by-example/][Rust by Example]] (~$ rustup docs --rust-by-example~) + - [[https://doc.rust-lang.org/std/][The Rust Standard Library]] (~$ rustup docs --std~) + - [[https://doc.rust-lang.org/nomicon/][The Rustonomicon]]: About unsafe Rust (~$ rustup docs --nomicon~) + - Use '~$ rustup help docs~' to get an overview + +*** Helpful sites + - [[https://cheats.rs][cheats.rs]] + - [[https://programming-idioms.org/cheatsheet/Rust][programming-idioms.org]] + +** Where to find help and stay up to date +*** Chat (Discord) + - [[https://discord.gg/rust-lang][The Rust Programming Language]] + - [[https://discord.com/invite/tcbkpyQ][The Rust Programming Language Community Server]] + +*** Boards/Blogs + - [[https://blog.rust-lang.org/][Rust Blog]] + - [[https://www.reddit.com/r/rust][r/rust]] + - [[https://this-week-in-rust.org/][This Week in Rust]] + * Cargo ** Cargo + #+BEGIN_CENTER + \Huge *Cargo* + #+END_CENTER + #+ATTR_LaTeX: :height 0.4\textwidth [[./img/cargo.png]] ** What is it? - Package manager - Build tool + - A Cargo project contains least one crate + +** Docs + - [[https://doc.rust-lang.org/cargo/index.html][The Cargo Book]] (~$ rustup docs --cargo~) + +** Cargo commands/plugins +*** What I typically use + - ~audit~: Audit ~Cargo.lock~ for security vulnerabilities. + - ~bloat~: Identify which dependency adds how much bloat. + - ~checkmate~: Run a list of checks (~check~, ~format~, ~build~, + ~test~, ~doc~, ~audit~). + - ~clippy~: Linter and static code analysis. + - ~deb~: Automatically create a binary Debian package. + - ~edit~: Modify your ~Cargo.toml~ from the command line. + - ~flamegraph~: Generate flamegraphs about anything. + - ~fmt~: Format Rust code according to style guidelines. + - ~install-update~: Keep all your Cargo commands up to date. * Setup +** Install the Rust-Toolchain +*** Install Rust + From [[https://rustup.rs][rustup.rs]]: + ~$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh~ + +*** Rustup: The Rust toolchain installer + - Check for new versions: ~$ rustup check~ + - Update the toolchain: ~$ rustup update~ + - Update rustup itself: ~$ rustup self update~ + - Install Rust nightly (daily builds): + ~$ rustup toolchain install nightly~ + - Activate nightly globally: ~$ rustup default nightly~ + - Activate nightly locally (in current dir): + ~$ rustup override set nightly~ * Hands on +* Testing + +* Documentation + * IDE support - -* Cargo plugins - -* WebDev - -* Embedded + [[https://areweideyet.com/][Are we IDE yet]] * (Standard?) crates + - [[https://crates.io][crates.io]]/[[https://lib.rs][lib.rs]] + - [[https://github.com/rust-unofficial/awesome-rust][Awesome Rust]] -* Further information + - clap (command line argument parser) + - env_logger, log + - dotenv + - serde + - lazy_static, once_cell (globality, singleton, lazy initialization) + - nom + - tokio + +** Databases + - sqlx + - diesel + +** WebDev + [[https://www.arewewebyet.org/][Are we web yet]] + + - Actix + - Hyper + - Rocket + - reqwest + +** Embedded + [[https://niclashoyer.github.io/areweembeddedyet/][Are we embedded yet]] + [[https://niclashoyer.github.io/areweembeddedyet/][Awesome Embedded Rust]] + [[https://docs.rust-embedded.org/embedonomicon/][Embedonomicon]] + + - avr + - stm + - msp430 + - esp32 * Footnotes [fn:1] [[https://commons.wikimedia.org/wiki/File:Bruine_roest_op_tarwe_(Puccinia_recondita_f.sp._tritici_on_Triticum_aestivum).jpg][Wikimedia]] -[fn:2] Copied list from: https://cheats.rs. +[fn:2] Copied from: https://cheats.rs. [fn:3] Work on GCC: [[https://rust-gcc.github.io/][rust-gcc]] and [[https://news.ycombinator.com/item?id=27775544][gcc rust backend]]. [fn:4] [[https://blog.rust-lang.org/2020/04/17/Rust-survey-2019.html#why-not-use-rust][Rust survey]] [fn:5] [[https://news.ycombinator.com/item?id=23538220][The Rust compiler isn't slow; we are]] From 72b4170f1a39dfa93716dfef0a99f7c214c9ea7d Mon Sep 17 00:00:00 2001 From: finga Date: Thu, 23 Sep 2021 15:00:26 +0200 Subject: [PATCH 03/13] More thorough content --- img/compiler_complaint.png | 3 + rust-intro.org | 274 +++++++++++++++++++++++++++++++++---- 2 files changed, 252 insertions(+), 25 deletions(-) create mode 100644 img/compiler_complaint.png diff --git a/img/compiler_complaint.png b/img/compiler_complaint.png new file mode 100644 index 0000000..7296d07 --- /dev/null +++ b/img/compiler_complaint.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0467e6d0e9c81afea4164d28def71999233b3848868c60425c8838c0998270dd +size 32187 diff --git a/rust-intro.org b/rust-intro.org index f15904b..b1419f7 100644 --- a/rust-intro.org +++ b/rust-intro.org @@ -5,11 +5,11 @@ #+OPTIONS: p:nil pri:nil prop:nil stat:t tags:t tasks:t tex:t #+OPTIONS: timestamp:t title:t toc:t todo:t |:t #+TITLE: \includesvg[height=.25\textheight]{img/rust-logo-blk}\newline An introduction to Rust -#+DATE: \today #+AUTHOR: \href{mailto:finga@onders.org}{finga} #+EMAIL: finga@onders.org #+SUBTITLE: A language empowering everyone to build reliable and efficient software. -#+DESCRIPTION: +#+DATE: \today +#+DESCRIPTION: An introduction into Rust and its ecosystems. #+LANGUAGE: en #+KEYWORDS: rust programming #+SELECT_TAGS: export @@ -18,6 +18,7 @@ #+OPTIONS: H:2 #+LATEX_CLASS: beamer +#+LATEX_CLASS_OPTIONS: [aspectratio=1610] #+COLUMNS: %45ITEM %10BEAMER_env(Env) %10BEAMER_act(Act) %4BEAMER_col(Col) %8BEAMER_opt(Opt) #+LATEX_HEADER: \usepackage{svg}\hypersetup{colorlinks=true,linkcolor=black,urlcolor=gray} #+BEAMER_THEME: Frankfurt @@ -25,7 +26,6 @@ #+BEAMER_FONT_THEME: #+BEAMER_INNER_THEME: #+BEAMER_OUTER_THEME: -#+LaTeX_CLASS_OPTIONS: [smaller] * Rust ** Abstract @@ -35,7 +35,7 @@ #+END_CENTER *** Rust, the language - - A bit more than 10 years old (2010) + - First appearance 2010 - Memory safe without gcing, optional reference counting - Ownership, lifetimes, traits, functional paradigms - Variables are immutable by default and can be shadowed @@ -52,24 +52,37 @@ - Strong type system prevents [[https://doc.rust-lang.org/nomicon/races.html][data races]], brings [[https://blog.rust-lang.org/2015/04/10/Fearless-Concurrency.html]['fearless concurrency']] (amongst others). - Seamless C interop, and [[https://doc.rust-lang.org/rustc/platform-support.html][dozens of supported platforms]] (based on - LLVM).[fn:3] + LLVM).[fn:4] - [[https://insights.stackoverflow.com/survey/2021#technology-most-loved-dreaded-and-wanted]["Most loved language"]] for 6 years in a row. - Modern tooling: ~cargo~ (builds just work), ~clippy~ (450+ code quality lints), ~rustup~ (easy toolchain management). -** Strengths and Weaknesses[fn:2] +** Strengths and Weaknesses[fn:3] *** Points you might run into (Weaknesses) - - Steep learning curve[fn:4] compiler enforcing (esp. memory) + - Steep learning curve[fn:5] compiler enforcing (esp. memory) rules that would be "best practices" elsewhere. - Missing Rust-native libs in some domains, target platforms - (esp. embedded), IDE features.[fn:4] + (esp. embedded), IDE features.[fn:5] - Longer compile times than "similar" code in other - languages.[fn:4][fn:5] + languages.[fn:5][fn:6] - No formal language specification, can prevent legal use in some - domains (aviation, medical, ...).[fn:6] + domains (aviation, medical, ...).[fn:7] - Careless (use of ~unsafe~ in) libraries can secretly break safety guarantees. +** Problems and security Rust tries to conquer + #+BEGIN_CENTER + #+LaTeX:\includegraphics[width = 0.8\textwidth]{img/compiler_complaint.png} + [fn:8] + #+END_CENTER + + - Dangling pointers / memory safety + - Iterator invalidation + - Thread safety / concurreny + - Segfaults + - Error handling + - Zero-cost abstractions + ** Where to start? *** First Steps - [[https://tourofrust.com/TOC_en.html][Tour of Rust]] @@ -77,10 +90,11 @@ - [[https://dhghomon.github.io/easy_rust/Chapter_1.html][Rust in Easy English]] - [[https://doc.rust-lang.org/rust-by-example/][Rust by Example]] (~$ rustup docs --rust-by-example~) - [[https://doc.rust-lang.org/std/][The Rust Standard Library]] (~$ rustup docs --std~) - - [[https://doc.rust-lang.org/nomicon/][The Rustonomicon]]: About unsafe Rust (~$ rustup docs --nomicon~) + - [[https://doc.rust-lang.org/nomicon/][The Rustonomicon]]: The Art of unsafe Rust (~$ rustup docs + --nomicon~) - Use '~$ rustup help docs~' to get an overview -*** Helpful sites +*** Other helpful sites - [[https://cheats.rs][cheats.rs]] - [[https://programming-idioms.org/cheatsheet/Rust][programming-idioms.org]] @@ -94,6 +108,131 @@ - [[https://www.reddit.com/r/rust][r/rust]] - [[https://this-week-in-rust.org/][This Week in Rust]] +** Language + - Semicolons have meaning + - Mutability + - Shadowing + - Functional paradigms + +** Mutability and shadowing +*** Immutable variable + #+BEGIN_SRC rust + let foo = 5; + #+END_SRC + +*** Mutable variable + #+BEGIN_SRC rust + let mut bar = 5; + bar = 6; + #+END_SRC + +*** Shadowing + #+BEGIN_SRC rust + let baz = 5; + let baz = baz + 3; + #+END_SRC + +** Type aliases + +** Tuple structs +*** Definition of a tuple struct + #+BEGIN_SRC rust + struct Rectangle(usize, usize); + #+END_SRC + +*** Instantiation of a tuple struct + #+BEGIN_SRC rust + let rect = Rectangle(3, 5); + #+END_SRC + +*** Access of a tuple struct's field + #+BEGIN_SRC rust + rect.0; + rect.1; + #+END_SRC + +** Structs + - Direct access vs. getter and setter + - ~Something::new(item)~ and ~Something::default()~ + +*** Definition of a struct + #+BEGIN_SRC rust + struct Something { + id: usize, + item: String, + } + #+END_SRC + +** Error related types + - Heavy use of ~Option~ and ~Result~ + +*** Option + #+BEGIN_SRC rust + pub enum Option { + None, + Some(T), + } + #+END_SRC + +*** Result + #+BEGIN_SRC rust + pub enum Result { + Ok(T), + Err(E), + } + #+END_SRC + +** Error handling + +*** Unrecoverable errors \rightarrow panics + #+BEGIN_SRC rust + fn get_data() -> Data { + ... + panic!("Cannot get data"); + } + #+END_SRC + +*** Recoverable error with anyhow + #+BEGIN_SRC rust + fn get_data() -> Result { + ... + Ok(data) // return is only used for "early" returns + } + ... + let data = get_data()?; + ... + #+END_SRC + +** Use functional language features + - ~vec![1, 2, 3].iter()~ + - ~vec![1, 2, 3].iter().sum()~ + - ~vec![1, 2, 3].iter().map(|x| x + 1).collect()~ + - ~foo.into_iter().filter(|i| i.bar == 0).collect()~ + - ~Iterator~ trait + +** Control structures + - ~if~, ~if let~, ~match~ + - ~loop~, ~for~, ~map()~ + +** Tests + - Use ~assert!()~ family + - Unit tests + - Put them into a test module + - Annotate the test module with ~#[cfg(test)]~ + - Annotate the tests with ~#[test]~ + - Documentation testing + - Integration testing + - Integration tests are put into the ~src/tests/~ directory + +** Documentation + - Documentation comments: ~///~ + - Markdown notation + - ~cargo doc~ runns rustdoc which generates html docs + - ~cargo doc --open~ runs rustdoc and opens the result in a browser + - rustdoc documentation: https://doc.rust-lang.org/beta/rustdoc/index.html + +** FFI (Foreign Function Interface) + * Cargo ** Cargo #+BEGIN_CENTER @@ -105,8 +244,36 @@ ** What is it? - Package manager + - Cargo calls ~rustc~, the Rust compiler - Build tool - - A Cargo project contains least one crate + - A Cargo project contains at least one crate + +** Hello, World + Executing ~`cargo new foobar`~ creates a new project... + +*** foobar/Cargo.toml + #+BEGIN_SRC toml + [package] + name = "foo" + version = "0.1.0" + authors = ["finga "] + edition = "2018" + #+END_SRC + \tiny + #+BEGIN_SRC toml + # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + #+END_SRC + \normalsize + #+BEGIN_SRC toml + [dependencies] + #+END_SRC + +*** foobar/src/main.rs + #+BEGIN_SRC rust + fn main() { + println!("Hello, world!"); + } + #+END_SRC ** Docs - [[https://doc.rust-lang.org/cargo/index.html][The Cargo Book]] (~$ rustup docs --cargo~) @@ -118,12 +285,27 @@ - ~checkmate~: Run a list of checks (~check~, ~format~, ~build~, ~test~, ~doc~, ~audit~). - ~clippy~: Linter and static code analysis. - - ~deb~: Automatically create a binary Debian package. - - ~edit~: Modify your ~Cargo.toml~ from the command line. + - ~deb~: Automatically create a Debian package. - ~flamegraph~: Generate flamegraphs about anything. - ~fmt~: Format Rust code according to style guidelines. - ~install-update~: Keep all your Cargo commands up to date. +*** @@comment: comment@@ + Cargo downloads your Rust package's dependencies, compiles your + package, makes distributable packages, and uploads them to + [[https://crates.io][crates.io]], the Rust community's package registry. + +** The Rust (package) registry + +*** Official registry + [[https://crates.io][crates.io]] + +*** Unofficial [[https://crates.io][~crates.io~]] frontend + [[https://lib.rs][lib.rs]] same but different.. + +** Docs + - The [[https://doc.rust-lang.org/cargo/][Cargo Book]]. + * Setup ** Install the Rust-Toolchain *** Install Rust @@ -140,16 +322,55 @@ - Activate nightly locally (in current dir): ~$ rustup override set nightly~ -* Hands on +** Handle the Rust toolchain with ~rustup~ -* Testing +*** 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 -* Documentation +*** 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 - [[https://areweideyet.com/][Are we IDE yet]] +** Are we IDE yet?[fn:9] + \tiny +| | \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] | | | +|---------------------+--------------------------------------------+----------------------------------------------+--------------------------+---------------------------------+-------------------------+---------------------------------+---------------------------------+---------------------------+----------------------------------------| +| 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] | | | | | | | | | -* (Standard?) crates +* Common Crates - [[https://crates.io][crates.io]]/[[https://lib.rs][lib.rs]] - [[https://github.com/rust-unofficial/awesome-rust][Awesome Rust]] @@ -185,8 +406,11 @@ * Footnotes [fn:1] [[https://commons.wikimedia.org/wiki/File:Bruine_roest_op_tarwe_(Puccinia_recondita_f.sp._tritici_on_Triticum_aestivum).jpg][Wikimedia]] -[fn:2] Copied from: https://cheats.rs. -[fn:3] Work on GCC: [[https://rust-gcc.github.io/][rust-gcc]] and [[https://news.ycombinator.com/item?id=27775544][gcc rust backend]]. -[fn:4] [[https://blog.rust-lang.org/2020/04/17/Rust-survey-2019.html#why-not-use-rust][Rust survey]] -[fn:5] [[https://news.ycombinator.com/item?id=23538220][The Rust compiler isn't slow; we are]] -[fn:6] [[https://people.mpi-sws.org/%7Ejung/thesis.html][Understanding and Evolving the Rust Programming Language]] +[fn:2] Copied list from: [[https://cheats.rs/#tab-hello-3][cheats.rs]]. +[fn:3] Copied list from: [[https://cheats.rs/#tab-hello-4][cheats.rs]]. +[fn:4] Work on GCC: [[https://rust-gcc.github.io/][rust-gcc]] and [[https://news.ycombinator.com/item?id=27775544][gcc rust backend]]. +[fn:5] [[https://blog.rust-lang.org/2020/04/17/Rust-survey-2019.html#why-not-use-rust][Rust survey]] +[fn:6] [[https://news.ycombinator.com/item?id=23538220][The Rust compiler isn't slow; we are]] +[fn:7] [[https://people.mpi-sws.org/%7Ejung/thesis.html][Understanding and Evolving the Rust Programming Language]] +[fn:8] https://xkcd.com/371/ +[fn:9] [[https://areweideyet.com/][areweideyet.com]] From dbd8ffed35d3524192f55a49e8a464119416c921 Mon Sep 17 00:00:00 2001 From: finga Date: Tue, 28 Sep 2021 15:47:16 +0200 Subject: [PATCH 04/13] Use named footnotes --- rust-intro.org | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/rust-intro.org b/rust-intro.org index b1419f7..ef3b0e3 100644 --- a/rust-intro.org +++ b/rust-intro.org @@ -31,7 +31,7 @@ ** Abstract #+BEGIN_CENTER #+LaTeX:\includegraphics[width = 0.5\textwidth]{img/Bruine_roest_op_tarwe_(Puccinia_recondita_f.sp._tritici_on_Triticum_aestivum).jpg} - [fn:1] + [fn:rust_fungus] #+END_CENTER *** Rust, the language @@ -43,7 +43,7 @@ of idiomatic C++ - A well designed language and ecosystem -** Strengths and Weaknesses[fn:2] +** Strengths and Weaknesses[fn:strengths] *** Things Rust does measurably really well (Strengths) - Compiled code [[https://benchmarksgame-team.pages.debian.net/benchmarksgame/which-programs-are-fastest.html][about same performance as C / C++]], and excellent [[https://dl.acm.org/doi/10.1145/3136014.3136031][memory and energy efficiency]]. @@ -52,28 +52,28 @@ - Strong type system prevents [[https://doc.rust-lang.org/nomicon/races.html][data races]], brings [[https://blog.rust-lang.org/2015/04/10/Fearless-Concurrency.html]['fearless concurrency']] (amongst others). - Seamless C interop, and [[https://doc.rust-lang.org/rustc/platform-support.html][dozens of supported platforms]] (based on - LLVM).[fn:4] + LLVM).[fn:gcc] - [[https://insights.stackoverflow.com/survey/2021#technology-most-loved-dreaded-and-wanted]["Most loved language"]] for 6 years in a row. - Modern tooling: ~cargo~ (builds just work), ~clippy~ (450+ code quality lints), ~rustup~ (easy toolchain management). -** Strengths and Weaknesses[fn:3] +** Strengths and Weaknesses[fn:weaknesses] *** Points you might run into (Weaknesses) - - Steep learning curve[fn:5] compiler enforcing (esp. memory) + - Steep learning curve[fn:survey] compiler enforcing (esp. memory) rules that would be "best practices" elsewhere. - Missing Rust-native libs in some domains, target platforms - (esp. embedded), IDE features.[fn:5] + (esp. embedded), IDE features.[fn:survey] - Longer compile times than "similar" code in other - languages.[fn:5][fn:6] + languages.[fn:survey][fn:compiler_speed] - No formal language specification, can prevent legal use in some - domains (aviation, medical, ...).[fn:7] + domains (aviation, medical, ...).[fn:specification] - Careless (use of ~unsafe~ in) libraries can secretly break safety guarantees. ** Problems and security Rust tries to conquer #+BEGIN_CENTER #+LaTeX:\includegraphics[width = 0.8\textwidth]{img/compiler_complaint.png} - [fn:8] + [fn:xkcd_dangling_pointer] #+END_CENTER - Dangling pointers / memory safety @@ -405,12 +405,12 @@ - esp32 * Footnotes -[fn:1] [[https://commons.wikimedia.org/wiki/File:Bruine_roest_op_tarwe_(Puccinia_recondita_f.sp._tritici_on_Triticum_aestivum).jpg][Wikimedia]] -[fn:2] Copied list from: [[https://cheats.rs/#tab-hello-3][cheats.rs]]. -[fn:3] Copied list from: [[https://cheats.rs/#tab-hello-4][cheats.rs]]. -[fn:4] Work on GCC: [[https://rust-gcc.github.io/][rust-gcc]] and [[https://news.ycombinator.com/item?id=27775544][gcc rust backend]]. -[fn:5] [[https://blog.rust-lang.org/2020/04/17/Rust-survey-2019.html#why-not-use-rust][Rust survey]] -[fn:6] [[https://news.ycombinator.com/item?id=23538220][The Rust compiler isn't slow; we are]] -[fn:7] [[https://people.mpi-sws.org/%7Ejung/thesis.html][Understanding and Evolving the Rust Programming Language]] -[fn:8] https://xkcd.com/371/ -[fn:9] [[https://areweideyet.com/][areweideyet.com]] +[fn:rust_fungus] [[https://commons.wikimedia.org/wiki/File:Bruine_roest_op_tarwe_(Puccinia_recondita_f.sp._tritici_on_Triticum_aestivum).jpg][Wikimedia]] +[fn:strengths] Copied list from: [[https://cheats.rs/#tab-hello-3][cheats.rs]]. +[fn:weaknesses] Copied list from: [[https://cheats.rs/#tab-hello-4][cheats.rs]]. +[fn:gcc] Work on GCC: [[https://rust-gcc.github.io/][rust-gcc]] and [[https://news.ycombinator.com/item?id=27775544][gcc rust backend]]. +[fn:survey] [[https://blog.rust-lang.org/2020/04/17/Rust-survey-2019.html#why-not-use-rust][Rust survey]] +[fn:compiler_speed] [[https://news.ycombinator.com/item?id=23538220][The Rust compiler isn't slow; we are]] +[fn:specification] [[https://people.mpi-sws.org/%7Ejung/thesis.html][Understanding and Evolving the Rust Programming Language]] +[fn:xkcd_dangling_pointer] [[https://xkcd.com/371/][xkcd.com/371]] +[fn:areweideyet] [[https://areweideyet.com/][areweideyet.com]] From 46b0c0e1338d70cfa34c938404d73c3106cf5446 Mon Sep 17 00:00:00 2001 From: finga Date: Tue, 28 Sep 2021 15:48:10 +0200 Subject: [PATCH 05/13] Add further information and rework some sections --- rust-intro.org | 250 ++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 216 insertions(+), 34 deletions(-) diff --git a/rust-intro.org b/rust-intro.org index ef3b0e3..a53354e 100644 --- a/rust-intro.org +++ b/rust-intro.org @@ -36,11 +36,10 @@ *** Rust, the language - First appearance 2010 + - Rust is the coating closest to the bare metal - Memory safe without gcing, optional reference counting - - Ownership, lifetimes, traits, functional paradigms - - Variables are immutable by default and can be shadowed - - Performance of idiomatic Rust is comparable to the performance - of idiomatic C++ + - Ownership, lifetimes, traits, functional paradigms, zero-cost + and zero-size abstractions - A well designed language and ecosystem ** Strengths and Weaknesses[fn:strengths] @@ -81,7 +80,7 @@ - Thread safety / concurreny - Segfaults - Error handling - - Zero-cost abstractions + - Zero-cost and zero-size abstractions ** Where to start? *** First Steps @@ -98,21 +97,46 @@ - [[https://cheats.rs][cheats.rs]] - [[https://programming-idioms.org/cheatsheet/Rust][programming-idioms.org]] -** Where to find help and stay up to date +** 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) + *** Chat (Discord) - [[https://discord.gg/rust-lang][The Rust Programming Language]] - [[https://discord.com/invite/tcbkpyQ][The Rust Programming Language Community Server]] -*** Boards/Blogs +*** Boards and Blogs - [[https://blog.rust-lang.org/][Rust Blog]] - [[https://www.reddit.com/r/rust][r/rust]] - [[https://this-week-in-rust.org/][This Week in Rust]] ** Language - - Semicolons have meaning + - Semicolons have meaning (statements vs. expressions) - Mutability - Shadowing - Functional paradigms + - Pattern matching + - Memory safety + - Meaningful compiler warnings and errors + +** Statements vs. expressions +*** Statements + #+BEGIN_SRC rust + let something = true; + fn_call(); + #+END_SRC +*** Expressions + ~false~, ~x + y~, ~fn_call()~ +*** Example + #+BEGIN_SRC rust + let bool_value = true; + let value = if bool_value { + "true".to_string() + } else { + "false".to_string() + }; + #+END_SRC ** Mutability and shadowing *** Immutable variable @@ -133,6 +157,15 @@ #+END_SRC ** Type aliases +*** 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 @@ -152,7 +185,7 @@ #+END_SRC ** Structs - - Direct access vs. getter and setter + - Direct access vs. getter and setter (~pub~ keyword) - ~Something::new(item)~ and ~Something::default()~ *** Definition of a struct @@ -163,6 +196,53 @@ } #+END_SRC +** Enums +*** Enum example + #+BEGIN_SRC rust + enum Event { + Receive, + Send(String), + Reset { address: u64, hard: bool }, + } + + let new_event = Event::Send("A message".to_owned()); + #+END_SRC + +** ~if let~ + +** Pattern matching and some macros +*** Simple ~match~ + #+BEGIN_SRC rust + match an_enum { + MyEnum::Something => todo!("This has to be done"), + MyEnum::SomethingElse(_) => unimplemented!("This is not implemented"), + MyEnum::AnotherThing(e) => println!("It is another thing: {}", e), + _ => unreachable!(), + }; + #+END_SRC + +*** Assigning ~match~ + #+BEGIN_SRC rust + let maturity = match age { + 0 | 1 => "infant", + 2..=12 => "child", + 13..=19 => "teen", + _ => "...", + }; + #+END_SRC + +** Loops[fn:reference_loops] +*** Loops + - ~loop~: infinite loops + - ~while~: loops until a predicate is false + - ~while let~: loops until a pattern is matched + - ~for~: loop over an iterator until it is empty + +*** Loop labels + - ~break~: terminate/exit a loop + - ~continue~: exit the current iteration and jump to the loop + header + ** Error related types - Heavy use of ~Option~ and ~Result~ @@ -203,17 +283,57 @@ ... #+END_SRC -** Use functional language features +** Printing + When printing something, ~{}~ uses the ~std::fmt::Display~ trait + and ~{:?}~ uses the ~std::fmt::Debug~ trait. + +*** ~print!()~ and ~println!()~ + #+BEGIN_SRC rust + let amount = 3; + let fields = vec![0,2,5]; + + println!("We have {} of {:?}", amount, fields); + #+END_SRC + Prints: ~We have 3 of [0, 2, 5]~ + +** Deriving ~Debug~ + To enable printing of structs or enums when their fields implement + ~Debug~ they can be derived with following annotation. + +*** Deriving ~Debug~ + #+BEGIN_SRC rust + #[derive(Debug)] + struct Something { + id: usize, + item: String, + } + #+END_SRC + +** Implementing ~Display~ +*** Implementing ~Display~ + #+BEGIN_SRC rust + use std::fmt; + + struct Something { + id: usize, + item: String, + } + + impl fmt::Display for Something { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}: {}", self.id, self.item) + } + } + #+END_SRC + + +** Use more functional language features - ~vec![1, 2, 3].iter()~ - ~vec![1, 2, 3].iter().sum()~ - ~vec![1, 2, 3].iter().map(|x| x + 1).collect()~ - ~foo.into_iter().filter(|i| i.bar == 0).collect()~ - ~Iterator~ trait -** Control structures - - ~if~, ~if let~, ~match~ - - ~loop~, ~for~, ~map()~ - ** Tests - Use ~assert!()~ family - Unit tests @@ -223,15 +343,21 @@ - Documentation testing - Integration testing - Integration tests are put into the ~src/tests/~ directory + - [[https://doc.rust-lang.org/book/ch11-00-testing.html][Rust Book: Writing Automated Tests]] + - Alastair Reid: [[https://alastairreid.github.io/rust-testability/][Rust Design-for-Testability: a survey]] ** Documentation - Documentation comments: ~///~ - Markdown notation - ~cargo doc~ runns rustdoc which generates html docs - ~cargo doc --open~ runs rustdoc and opens the result in a browser - - rustdoc documentation: https://doc.rust-lang.org/beta/rustdoc/index.html + - [[https://doc.rust-lang.org/rustdoc][rustdoc documentation]] ** FFI (Foreign Function Interface) + - [[https://doc.rust-lang.org/nomicon/ffi.html][Rustonomicon: FFI]] + - [[https://docs.rs/jni/][jni]]: Java bindings for Rust + - [[https://bheisler.github.io/post/calling-rust-in-python/][Calling Rust From Python]] + - [[https://github.com/rusterlium/rustler][Rustler]]: Write Erlang NIFs in Rust * Cargo ** Cargo @@ -242,10 +368,10 @@ #+ATTR_LaTeX: :height 0.4\textwidth [[./img/cargo.png]] -** What is it? +** What is it and how does it work? - Package manager + - Build chain/tool - Cargo calls ~rustc~, the Rust compiler - - Build tool - A Cargo project contains at least one crate ** Hello, World @@ -277,6 +403,8 @@ ** Docs - [[https://doc.rust-lang.org/cargo/index.html][The Cargo Book]] (~$ rustup docs --cargo~) + - Guillaume Gomez: [[https://blog.guillaume-gomez.fr/articles/2020-03-12+Guide+on+how+to+write+documentation+for+a+Rust+crate][Guide on how to write documentation for a Rust + crate]] ** Cargo commands/plugins *** What I typically use @@ -288,7 +416,8 @@ - ~deb~: Automatically create a Debian package. - ~flamegraph~: Generate flamegraphs about anything. - ~fmt~: Format Rust code according to style guidelines. - - ~install-update~: Keep all your Cargo commands up to date. + - ~install-update~: Keep all your Cargo commands/plugins up to + date. *** @@comment: comment@@ Cargo downloads your Rust package's dependencies, compiles your @@ -303,10 +432,13 @@ *** Unofficial [[https://crates.io][~crates.io~]] frontend [[https://lib.rs][lib.rs]] same but different.. -** Docs - - The [[https://doc.rust-lang.org/cargo/][Cargo Book]]. - * Setup +** Setup + \Huge + #+BEGIN_CENTER + How to get all that stuff? + #+END_CENTER + ** Install the Rust-Toolchain *** Install Rust From [[https://rustup.rs][rustup.rs]]: @@ -345,8 +477,7 @@ $ rustup update #+END_SRC -* IDE support -** Are we IDE yet?[fn:9] +** IDE support aka are we IDE yet?[fn:areweideyet] \tiny | | \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} | |---------------------+--------------------------------------------+----------------------------------------------+--------------------------+---------------------------------+-------------------------+---------------------------------+---------------------------------+---------------------------+----------------------------------------| @@ -371,24 +502,67 @@ | Ride | [X] | | | | | | | | | * Common Crates +** General - [[https://crates.io][crates.io]]/[[https://lib.rs][lib.rs]] - [[https://github.com/rust-unofficial/awesome-rust][Awesome Rust]] - - clap (command line argument parser) - - env_logger, log - dotenv - - serde - - lazy_static, once_cell (globality, singleton, lazy initialization) + - [[https://tokio.rs/][tokio]] + +** Parsing/Encoding + - clap: The command line argument parser (if starting a new project + I would use v3.0.0-beta) + - [[https://serde.rs/][serde]] - nom - - tokio + - [[https://pest.rs/][pest]] + +** Error handling + - anyhow + - thiserror + - snafu + - easy-error + +** Logging + - log: a lightweight logging facade + + - env_logger + - simple_logger + - simplelog + - pretty_env_logger + - stderrlog + - flexi_logger + - log4rs + - fern + - and many many more (checkout the log readme[fn:log_readme]) + +** Testing + - quickcheck + - proptest + +** Data handling + - lazy_static + - once_cell (globality, singleton, lazy initialization) + - [[http://plv.mpi-sws.org/rustbelt/ghostcell/][GhostCell]] ** Databases + - [[https://indradb.github.io/][IndraDB]] + - [[https://diesel.rs/][diesel]]: orm and query builder supporting - sqlx - - diesel + - refinery + - barrel + +** Gui ([[https://www.areweguiyet.com/][areweguiyet.com]]) + A small selection of crates I used/tried so far: + - SixtyFPS + - egui + - imgui + - GTK + - relm + - and many many more + - bevy_ui ** WebDev [[https://www.arewewebyet.org/][Are we web yet]] - - Actix - Hyper - Rocket @@ -399,10 +573,16 @@ [[https://niclashoyer.github.io/areweembeddedyet/][Awesome Embedded Rust]] [[https://docs.rust-embedded.org/embedonomicon/][Embedonomicon]] - - avr - - stm - - msp430 - - esp32 + # - Microchip (Atmel) avr + # - stm + # - msp430 + # - esp32 + # - arm + +** Unikernel + https://unikernel.org + + - [[https://github.com/hermitcore/rusty-hermit][RustyHermit]] * Footnotes [fn:rust_fungus] [[https://commons.wikimedia.org/wiki/File:Bruine_roest_op_tarwe_(Puccinia_recondita_f.sp._tritici_on_Triticum_aestivum).jpg][Wikimedia]] @@ -413,4 +593,6 @@ [fn:compiler_speed] [[https://news.ycombinator.com/item?id=23538220][The Rust compiler isn't slow; we are]] [fn:specification] [[https://people.mpi-sws.org/%7Ejung/thesis.html][Understanding and Evolving the Rust Programming Language]] [fn:xkcd_dangling_pointer] [[https://xkcd.com/371/][xkcd.com/371]] +[fn:reference_loops] [[https://doc.rust-lang.org/reference/expressions/loop-expr.html][Rust Reference: Loops]] [fn:areweideyet] [[https://areweideyet.com/][areweideyet.com]] +[fn:log_readme] https://github.com/rust-lang/log#in-executables From 1ab6ecd53e5157307f02c9726d43a2c35149485c Mon Sep 17 00:00:00 2001 From: finga Date: Tue, 28 Sep 2021 16:03:16 +0200 Subject: [PATCH 06/13] Rework IDE table and add missing crates --- rust-intro.org | 60 ++++++++++++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 26 deletions(-) diff --git a/rust-intro.org b/rust-intro.org index a53354e..44090ae 100644 --- a/rust-intro.org +++ b/rust-intro.org @@ -481,25 +481,25 @@ \tiny | | \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 @@ -521,6 +521,8 @@ - thiserror - snafu - easy-error + - eyre + - color-eyre ** Logging - log: a lightweight logging facade @@ -538,34 +540,40 @@ ** Testing - quickcheck - proptest + - rutenspitz + - [[https://rust-fuzz.github.io/book/cargo-fuzz.html][cargo-fuzz]] + - [[https://rust-fuzz.github.io/book/afl.html][afl.rs]] + - [[https://honggfuzz.dev/][honggfuzz-rs]] + - arbitrary ** Data handling - lazy_static - once_cell (globality, singleton, lazy initialization) + - static_assertions - [[http://plv.mpi-sws.org/rustbelt/ghostcell/][GhostCell]] ** Databases - [[https://indradb.github.io/][IndraDB]] - [[https://diesel.rs/][diesel]]: orm and query builder supporting - - sqlx + - [[https://jmoiron.github.io/sqlx/][sqlx]] - refinery - barrel ** Gui ([[https://www.areweguiyet.com/][areweguiyet.com]]) A small selection of crates I used/tried so far: - - SixtyFPS - - egui + - [[https://sixtyfps.io/][SixtyFPS]] (WASM support) + - [[https://emilk.github.io/egui/][egui]] (WASM support) - imgui - - GTK + - [[https://gtk-rs.org/][gtk-rs]] - relm + - [[https://bevyengine.org/][bevy_ui]] - and many many more - - bevy_ui ** WebDev [[https://www.arewewebyet.org/][Are we web yet]] - - Actix - - Hyper - - Rocket + - [[https://actix.rs/][Actix]] + - [[https://hyper.rs/][Hyper]] + - [[https://rocket.rs/][Rocket]] - reqwest ** Embedded From 24f1ec76d865ca5de08026621addd63911f12f11 Mon Sep 17 00:00:00 2001 From: finga Date: Tue, 28 Sep 2021 16:22:44 +0200 Subject: [PATCH 07/13] Fix underscores, listing and add ending --- rust-intro.org | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/rust-intro.org b/rust-intro.org index 44090ae..2bd0a81 100644 --- a/rust-intro.org +++ b/rust-intro.org @@ -1,5 +1,5 @@ #+STARTUP: beamer -#+OPTIONS: ':nil *:t -:t ::t <:t H:3 \n:nil ^:t arch:headline +#+OPTIONS: ':nil *:t -:t ::t <:t H:3 \n:nil ^:nil arch:headline #+OPTIONS: author:t broken-links:nil c:nil creator:nil #+OPTIONS: d:(not "LOGBOOK") date:t e:t email:nil f:t inline:t num:t #+OPTIONS: p:nil pri:nil prop:nil stat:t tags:t tasks:t tex:t @@ -577,9 +577,9 @@ - reqwest ** Embedded - [[https://niclashoyer.github.io/areweembeddedyet/][Are we embedded yet]] - [[https://niclashoyer.github.io/areweembeddedyet/][Awesome Embedded Rust]] - [[https://docs.rust-embedded.org/embedonomicon/][Embedonomicon]] + - [[https://niclashoyer.github.io/areweembeddedyet/][Are we embedded yet]] + - [[https://niclashoyer.github.io/areweembeddedyet/][Awesome Embedded Rust]] + - [[https://docs.rust-embedded.org/embedonomicon/][Embedonomicon]] # - Microchip (Atmel) avr # - stm @@ -592,6 +592,24 @@ - [[https://github.com/hermitcore/rusty-hermit][RustyHermit]] +** Thats it for now + \Huge + #+BEGIN_CENTER + Thank you for your attention! + + Questions? + #+END_CENTER + + \normalsize + +*** To be continued + - Lifetimes + - Borrowing and Arrowing + - Traits + - Generics + - ~macro_rules!~ + - ~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]] [fn:strengths] Copied list from: [[https://cheats.rs/#tab-hello-3][cheats.rs]]. From 094d2546e9ff30c13122adc52ae971664322a6e5 Mon Sep 17 00:00:00 2001 From: finga Date: Mon, 4 Oct 2021 16:13:03 +0200 Subject: [PATCH 08/13] Fixes and content --- rust-intro.org | 208 +++++++++++++++++++++++++++++++------------------ 1 file changed, 133 insertions(+), 75 deletions(-) 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]] From 779d740a42346d6da316c2e3dcc85feb6549c363 Mon Sep 17 00:00:00 2001 From: finga Date: Mon, 11 Oct 2021 15:05:42 +0200 Subject: [PATCH 09/13] Improve wordings and small additions --- rust-intro.org | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/rust-intro.org b/rust-intro.org index fe2d2aa..2d62d94 100644 --- a/rust-intro.org +++ b/rust-intro.org @@ -100,7 +100,7 @@ ** 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 Rust is supported) + - [[https://godbolt.org/][godbolt.org]] (Supports not only Rust) *** Chat (Discord) - [[https://discord.gg/rust-lang][The Rust Programming Language]] @@ -426,6 +426,29 @@ - Build chain/tool - Cargo calls ~rustc~, the Rust compiler - A Cargo project contains at least one crate + - Custom build scripts with ~build.rs~ + - Builds just work + +** Cargo commands +*** Create a new binary crate (~init~ does so in an existing directory) + #+BEGIN_SRC sh + $ cargo new [--bin] + #+END_SRC + +*** Create a new library crate (~init~ does so in an existing directory) + #+BEGIN_SRC sh + $ cargo new --lib + #+END_SRC + +*** Update build dependencies + #+BEGIN_SRC sh + $ cargo update + #+END_SRC + +*** Install crates + #+BEGIN_SRC sh + $ cargo install [crate] + #+END_SRC ** Hello, World Executing ~`cargo new foobar`~ creates a new project... @@ -455,6 +478,7 @@ #+END_SRC ** Docs + - Cargo help: ~cargo --help~ and ~cargo [SUBCOMMAND] --help~ - [[https://doc.rust-lang.org/cargo/index.html][The Cargo Book]] (~$ rustup docs --cargo~) - Guillaume Gomez: [[https://blog.guillaume-gomez.fr/articles/2020-03-12+Guide+on+how+to+write+documentation+for+a+Rust+crate][Guide on how to write documentation for a Rust crate]] @@ -474,8 +498,8 @@ *** @@comment: comment@@ Cargo downloads your Rust package's dependencies, compiles your - package, makes distributable packages, and uploads them to - [[https://crates.io][crates.io]], the Rust community's package registry. + package, makes distributable packages, and is able to upload them + to [[https://crates.io][crates.io]], the Rust community's package registry. ** The Rust (package) registry From ffabae22cb63e35e8205c9b1f7d1c0a233973060 Mon Sep 17 00:00:00 2001 From: finga Date: Wed, 13 Oct 2021 11:26:51 +0200 Subject: [PATCH 10/13] Add footnote to lwn gcc article --- rust-intro.org | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rust-intro.org b/rust-intro.org index 2d62d94..26dd7cf 100644 --- a/rust-intro.org +++ b/rust-intro.org @@ -51,7 +51,7 @@ - Strong type system prevents [[https://doc.rust-lang.org/nomicon/races.html][data races]], brings [[https://blog.rust-lang.org/2015/04/10/Fearless-Concurrency.html]['fearless concurrency']] (amongst others). - Seamless C interop, and [[https://doc.rust-lang.org/rustc/platform-support.html][dozens of supported platforms]] (based on - LLVM).[fn:gcc] + LLVM).[fn:lwn_gcc][fn:gcc] - [[https://insights.stackoverflow.com/survey/2021#technology-most-loved-dreaded-and-wanted]["Most loved language"]] for 6 years in a row. - Modern tooling: ~cargo~ (builds just work), ~clippy~ (450+ code quality lints), ~rustup~ (easy toolchain management). @@ -697,6 +697,7 @@ [fn:strengths] Copied list from: [[https://cheats.rs/#tab-hello-3][cheats.rs]]. [fn:weaknesses] Copied list from: [[https://cheats.rs/#tab-hello-4][cheats.rs]]. [fn:gcc] Work on GCC: [[https://rust-gcc.github.io/][rust-gcc]] and [[https://news.ycombinator.com/item?id=27775544][gcc rust backend]]. +[fn:lwn_gcc] [[https://lwn.net/Articles/871283/][Rust and GCC, two different ways]] [fn:survey] [[https://blog.rust-lang.org/2020/04/17/Rust-survey-2019.html#why-not-use-rust][Rust survey]] [fn:compiler_speed] [[https://news.ycombinator.com/item?id=23538220][The Rust compiler isn't slow; we are]] [fn:specification] [[https://people.mpi-sws.org/%7Ejung/thesis.html][Understanding and Evolving the Rust Programming Language]] From bf86282d1aa8594469922ba1a94f358992d42d33 Mon Sep 17 00:00:00 2001 From: finga Date: Thu, 14 Oct 2021 12:07:01 +0200 Subject: [PATCH 11/13] Fix order of footnote Fix the order of footnotes to reflect their appearance in the document. --- rust-intro.org | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust-intro.org b/rust-intro.org index 26dd7cf..f86794b 100644 --- a/rust-intro.org +++ b/rust-intro.org @@ -696,8 +696,8 @@ [fn:rust_fungus] [[https://commons.wikimedia.org/wiki/File:Bruine_roest_op_tarwe_(Puccinia_recondita_f.sp._tritici_on_Triticum_aestivum).jpg][Wikimedia]] [fn:strengths] Copied list from: [[https://cheats.rs/#tab-hello-3][cheats.rs]]. [fn:weaknesses] Copied list from: [[https://cheats.rs/#tab-hello-4][cheats.rs]]. -[fn:gcc] Work on GCC: [[https://rust-gcc.github.io/][rust-gcc]] and [[https://news.ycombinator.com/item?id=27775544][gcc rust backend]]. [fn:lwn_gcc] [[https://lwn.net/Articles/871283/][Rust and GCC, two different ways]] +[fn:gcc] Work on GCC: [[https://rust-gcc.github.io/][rust-gcc]] and [[https://news.ycombinator.com/item?id=27775544][gcc rust backend]]. [fn:survey] [[https://blog.rust-lang.org/2020/04/17/Rust-survey-2019.html#why-not-use-rust][Rust survey]] [fn:compiler_speed] [[https://news.ycombinator.com/item?id=23538220][The Rust compiler isn't slow; we are]] [fn:specification] [[https://people.mpi-sws.org/%7Ejung/thesis.html][Understanding and Evolving the Rust Programming Language]] From 431c1c85a7514cd9dd83dd663cb8923b4eb473de Mon Sep 17 00:00:00 2001 From: finga Date: Thu, 14 Oct 2021 12:08:50 +0200 Subject: [PATCH 12/13] Add Cargo links --- rust-intro.org | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rust-intro.org b/rust-intro.org index f86794b..69037e1 100644 --- a/rust-intro.org +++ b/rust-intro.org @@ -482,6 +482,8 @@ - [[https://doc.rust-lang.org/cargo/index.html][The Cargo Book]] (~$ rustup docs --cargo~) - Guillaume Gomez: [[https://blog.guillaume-gomez.fr/articles/2020-03-12+Guide+on+how+to+write+documentation+for+a+Rust+crate][Guide on how to write documentation for a Rust crate]] + - [[https://crates.io/policies][Policies]] + - [[https://crates.io/category_slugs][Category Slugs]] ** Cargo commands/plugins *** What I typically use From 4d8936b6dfc0e9678c9022fd2d4a9b91948509d3 Mon Sep 17 00:00:00 2001 From: finga Date: Fri, 15 Oct 2021 13:19:33 +0200 Subject: [PATCH 13/13] Furbishing and small additions --- rust-intro.org | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/rust-intro.org b/rust-intro.org index 69037e1..21a4887 100644 --- a/rust-intro.org +++ b/rust-intro.org @@ -35,7 +35,7 @@ #+END_CENTER *** Rust, the language - - First appearance 2010 + - First public appearance 2010 - Rust is the coating closest to the bare metal - Memory safe without gcing, optional reference counting - Ownership, lifetimes, traits, functional paradigms, zero-cost @@ -97,6 +97,11 @@ - [[https://cheats.rs][cheats.rs]] - [[https://programming-idioms.org/cheatsheet/Rust][programming-idioms.org]] +** Where to continue? +*** Further Steps + - [[https://www.youtube.com/channel/UC_iD0xppBwwsrM9DegC5cQQ][Jon Gjengset]] (Considering Rust, Crust of Rust, Advanced topics + in Rust) + ** Where to share, find help and stay up to date *** Online sharing/testing - [[https://play.rust-lang.org/][play.rust-lang.org]] @@ -129,7 +134,7 @@ fn_call(); #+END_SRC *** Expressions - ~false~, ~x + y~, ~fn_call()~, ~if~, ~match~ + ~()~, ~false~, ~x + y~, ~fn_call()~, ~if~, ~match~ *** Example #+BEGIN_SRC rust let bool_value = true; @@ -478,7 +483,7 @@ #+END_SRC ** Docs - - Cargo help: ~cargo --help~ and ~cargo [SUBCOMMAND] --help~ + - Cargo help: ~$ cargo --help~ and ~$ cargo [SUBCOMMAND] --help~ - [[https://doc.rust-lang.org/cargo/index.html][The Cargo Book]] (~$ rustup docs --cargo~) - Guillaume Gomez: [[https://blog.guillaume-gomez.fr/articles/2020-03-12+Guide+on+how+to+write+documentation+for+a+Rust+crate][Guide on how to write documentation for a Rust crate]] @@ -571,8 +576,8 @@ - [[https://tokio.rs/][tokio]] ** Parsing/Encoding - - clap: The command line argument parser (if starting a new project - I would use v3.0.0-beta) + - clap: The command line argument parser (use v3.0.0-beta when + starting a new project) - [[https://serde.rs/][serde]] - nom - [[https://pest.rs/][pest]] @@ -587,7 +592,6 @@ ** Logging - log: a lightweight logging facade - - env_logger - simple_logger - simplelog @@ -695,14 +699,15 @@ - ~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]] +[fn:rust_fungus] [[https://commons.wikimedia.org/wiki/File:Bruine_roest_op_tarwe_(Puccinia_recondita_f.sp._tritici_on_Triticum_aestivum).jpg][Wikimedia]] (Puccinia recondita f.sp. tritici) [fn:strengths] Copied list from: [[https://cheats.rs/#tab-hello-3][cheats.rs]]. [fn:weaknesses] Copied list from: [[https://cheats.rs/#tab-hello-4][cheats.rs]]. [fn:lwn_gcc] [[https://lwn.net/Articles/871283/][Rust and GCC, two different ways]] [fn:gcc] Work on GCC: [[https://rust-gcc.github.io/][rust-gcc]] and [[https://news.ycombinator.com/item?id=27775544][gcc rust backend]]. [fn:survey] [[https://blog.rust-lang.org/2020/04/17/Rust-survey-2019.html#why-not-use-rust][Rust survey]] [fn:compiler_speed] [[https://news.ycombinator.com/item?id=23538220][The Rust compiler isn't slow; we are]] -[fn:specification] [[https://people.mpi-sws.org/%7Ejung/thesis.html][Understanding and Evolving the Rust Programming Language]] +[fn:specification] [[https://people.mpi-sws.org/%7Ejung/thesis.html][Understanding and Evolving the Rust Programming +Language]] [fn:xkcd_dangling_pointer] [[https://xkcd.com/371/][xkcd.com/371]] [fn:reference_loops] [[https://doc.rust-lang.org/reference/expressions/loop-expr.html][Rust Reference: Loops]] [fn:areweideyet] [[https://areweideyet.com/][areweideyet.com]]