More thorough content

This commit is contained in:
finga 2021-09-23 15:00:26 +02:00
parent 32bcca1530
commit 72b4170f1a
2 changed files with 252 additions and 25 deletions

BIN
img/compiler_complaint.png (Stored with Git LFS) Normal file

Binary file not shown.

View file

@ -5,11 +5,11 @@
#+OPTIONS: p:nil pri:nil prop:nil stat:t tags:t tasks:t tex:t #+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 #+OPTIONS: timestamp:t title:t toc:t todo:t |:t
#+TITLE: \includesvg[height=.25\textheight]{img/rust-logo-blk}\newline An introduction to Rust #+TITLE: \includesvg[height=.25\textheight]{img/rust-logo-blk}\newline An introduction to Rust
#+DATE: \today
#+AUTHOR: \href{mailto:finga@onders.org}{finga} #+AUTHOR: \href{mailto:finga@onders.org}{finga}
#+EMAIL: finga@onders.org #+EMAIL: finga@onders.org
#+SUBTITLE: A language empowering everyone to build reliable and efficient software. #+SUBTITLE: A language empowering everyone to build reliable and efficient software.
#+DESCRIPTION: #+DATE: \today
#+DESCRIPTION: An introduction into Rust and its ecosystems.
#+LANGUAGE: en #+LANGUAGE: en
#+KEYWORDS: rust programming #+KEYWORDS: rust programming
#+SELECT_TAGS: export #+SELECT_TAGS: export
@ -18,6 +18,7 @@
#+OPTIONS: H:2 #+OPTIONS: H:2
#+LATEX_CLASS: beamer #+LATEX_CLASS: beamer
#+LATEX_CLASS_OPTIONS: [aspectratio=1610]
#+COLUMNS: %45ITEM %10BEAMER_env(Env) %10BEAMER_act(Act) %4BEAMER_col(Col) %8BEAMER_opt(Opt) #+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} #+LATEX_HEADER: \usepackage{svg}\hypersetup{colorlinks=true,linkcolor=black,urlcolor=gray}
#+BEAMER_THEME: Frankfurt #+BEAMER_THEME: Frankfurt
@ -25,7 +26,6 @@
#+BEAMER_FONT_THEME: #+BEAMER_FONT_THEME:
#+BEAMER_INNER_THEME: #+BEAMER_INNER_THEME:
#+BEAMER_OUTER_THEME: #+BEAMER_OUTER_THEME:
#+LaTeX_CLASS_OPTIONS: [smaller]
* Rust * Rust
** Abstract ** Abstract
@ -35,7 +35,7 @@
#+END_CENTER #+END_CENTER
*** Rust, the language *** Rust, the language
- A bit more than 10 years old (2010) - First appearance 2010
- Memory safe without gcing, optional reference counting - Memory safe without gcing, optional reference counting
- Ownership, lifetimes, traits, functional paradigms - Ownership, lifetimes, traits, functional paradigms
- Variables are immutable by default and can be shadowed - 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 - 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). concurrency']] (amongst others).
- Seamless C interop, and [[https://doc.rust-lang.org/rustc/platform-support.html][dozens of supported platforms]] (based on - 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. - [[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 - Modern tooling: ~cargo~ (builds just work), ~clippy~ (450+ code
quality lints), ~rustup~ (easy toolchain management). quality lints), ~rustup~ (easy toolchain management).
** Strengths and Weaknesses[fn:2] ** Strengths and Weaknesses[fn:3]
*** Points you might run into (Weaknesses) *** 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. rules that would be "best practices" elsewhere.
- Missing Rust-native libs in some domains, target platforms - 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 - 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 - 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 - Careless (use of ~unsafe~ in) libraries can secretly break
safety guarantees. 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? ** Where to start?
*** First Steps *** First Steps
- [[https://tourofrust.com/TOC_en.html][Tour of Rust]] - [[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://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/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/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 - Use '~$ rustup help docs~' to get an overview
*** Helpful sites *** Other helpful sites
- [[https://cheats.rs][cheats.rs]] - [[https://cheats.rs][cheats.rs]]
- [[https://programming-idioms.org/cheatsheet/Rust][programming-idioms.org]] - [[https://programming-idioms.org/cheatsheet/Rust][programming-idioms.org]]
@ -94,6 +108,131 @@
- [[https://www.reddit.com/r/rust][r/rust]] - [[https://www.reddit.com/r/rust][r/rust]]
- [[https://this-week-in-rust.org/][This Week in 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<T>~ and ~Result<T, E>~
*** Option
#+BEGIN_SRC rust
pub enum Option<T> {
None,
Some(T),
}
#+END_SRC
*** Result
#+BEGIN_SRC rust
pub enum Result<T, E> {
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<Data> {
...
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
** Cargo ** Cargo
#+BEGIN_CENTER #+BEGIN_CENTER
@ -105,8 +244,36 @@
** What is it? ** What is it?
- Package manager - Package manager
- Cargo calls ~rustc~, the Rust compiler
- Build tool - 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 <finga@onders.org>"]
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 ** Docs
- [[https://doc.rust-lang.org/cargo/index.html][The Cargo Book]] (~$ rustup docs --cargo~) - [[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~, - ~checkmate~: Run a list of checks (~check~, ~format~, ~build~,
~test~, ~doc~, ~audit~). ~test~, ~doc~, ~audit~).
- ~clippy~: Linter and static code analysis. - ~clippy~: Linter and static code analysis.
- ~deb~: Automatically create a binary Debian package. - ~deb~: Automatically create a Debian package.
- ~edit~: Modify your ~Cargo.toml~ from the command line.
- ~flamegraph~: Generate flamegraphs about anything. - ~flamegraph~: Generate flamegraphs about anything.
- ~fmt~: Format Rust code according to style guidelines. - ~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 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 * Setup
** Install the Rust-Toolchain ** Install the Rust-Toolchain
*** Install Rust *** Install Rust
@ -140,16 +322,55 @@
- Activate nightly locally (in current dir): - Activate nightly locally (in current dir):
~$ rustup override set nightly~ ~$ 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 * 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://crates.io][crates.io]]/[[https://lib.rs][lib.rs]]
- [[https://github.com/rust-unofficial/awesome-rust][Awesome Rust]] - [[https://github.com/rust-unofficial/awesome-rust][Awesome Rust]]
@ -185,8 +406,11 @@
* Footnotes * Footnotes
[fn:1] [[https://commons.wikimedia.org/wiki/File:Bruine_roest_op_tarwe_(Puccinia_recondita_f.sp._tritici_on_Triticum_aestivum).jpg][Wikimedia]] [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:2] Copied list from: [[https://cheats.rs/#tab-hello-3][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:3] Copied list from: [[https://cheats.rs/#tab-hello-4][cheats.rs]].
[fn:4] [[https://blog.rust-lang.org/2020/04/17/Rust-survey-2019.html#why-not-use-rust][Rust survey]] [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://news.ycombinator.com/item?id=23538220][The Rust compiler isn't slow; we are]] [fn:5] [[https://blog.rust-lang.org/2020/04/17/Rust-survey-2019.html#why-not-use-rust][Rust survey]]
[fn:6] [[https://people.mpi-sws.org/%7Ejung/thesis.html][Understanding and Evolving the Rust Programming Language]] [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]]