Rust links¶
Rust book experiment (brown.edu) - includes quizes! [2]
Related to this, there is a Jane Street talk by Will Critchton from Brown Univ. that mentions aquascope. I believe it is this link here: https://cel.cs.brown.edu/aquascope/
-
See more Cargo.toml keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Cargo recognises semantic versioning
crates.io “Crates.io is where people in the Rust ecosystem post their open source Rust projects for others to use.”
The Rustonomicon, book that goes into the awful detail that you need to understand when writing unsafe rust and it assumes considerable prior knowledge.
Be warned of UNLEASHING INDESCRIBABLE HORRORS THAT SHATTER YOUR PSYCHE AND SET YOUR MIND ADRIFT IN THE UNKNOWABLY INFINITE COSMOS.
Many of the books about rust use ‘mdbook’.
The rust programming language book¶
Chapter 1 is an introduction to the basic tools, including cargo and the installation of the tools.
Chapter 2 starts a ‘guessing game’ program. This starts by using simple input/output with the std::io package.
There is a few examples of ‘cargo’ when one starts using the rand::Rng package, including the commands ‘cargo doc’, ‘cargo build’, ‘cargo update’.
The documentation example:
cargo doc --open
Chapter 4 talks about ownership, the very special rust concept:
Each value in rust has an owner
There can only be one owner at a time
When the owner goes out of scope, the value will be dropped.
Other things discussed are cloning, the Copy trait of a type, the drop method (that implements the Drop trait).
For function calls, passing a variable to a function will move or copy, just as assignment does.
The section on references explains the method as a way to avoid the (protocol) of getting ownership when entering a function and returning ownership on function return.
Normal references are not mutable. Adding mut changes the reference to mutable reference. Mutable references have one big restriction: if you have a mutable reference to a value, you can have no other references to that value.
Rust prevents the problem of data races by refusing to compile code with data races!
Rules of references:
you can have either one mutable reference (read/write) or any number of immutable references. (Swimmer, single write, multiple read).
references must be always valid - rust checks this with lifetimes [3] and ownership.
The chapter about fixing unsafe programs revealed to me a lack of understanding and the use of several methods:
references
indirection
static storage
borrow checker
ownership
read/write/ownership
lifetime of referred data
Methods to extend the lifetime (for the example of a function returning a string reference):
Move ownership by returning a String ( -> String )
Return a string literal ( -> &’static str )
Defer borrow checking to runtime using reference counted pointer ( -> Rc<String> )
Have the caller provide a “slot” (signature includes “output: &mut String”)
The topics are: returning a reference, enough permissions, aliasing and mutating a data structure
Only now after reading chapter 5, I realise that there are these properties on a variable:
readable (yes/no)
writeable (yes/no)
ownership (yes, no)
The mut keyword is needed for the writeable property.
The readable property is available on the variable and its references, unless the ownership is gone and the access would be undefined.
Chapter 6 introduces the Option<T> enum. Its documentation is useful.
The Rust Cookbook¶
There is an abandoned version here and a better updated version by jamesgraves at this location: link [4]
Rust training from microsoft¶
Take your first steps in rust [5]
A char in rust is a 21-bit integer that is padded to be 32-bits wide Includes link to documentation for char.
Old video link¶
Emily Dunham “Should you rewrite in Rust?” (youtube1) (45 min) LinuxConfAu 2018, Sydney
http://talks.edunham.net/lca2018/should-you-rewrite-in-rust/
Aaron Turon (Mozilla) “The Rust Programming Language” (youtube2) (65 min) Stanford Seminar, 12 March 2015
Some more rust-related talks from Jane Street’s tech-talks https://www.janestreet.com/tech-talks/
Conferences¶
Treating options and results¶
Option<T> is the rust enum that has two variants: Some(T) or None.
Result<T,E> is an enum that has two variants: Ok(T) and Err(E). It is used for reporting and propagating errors.
There are idiomatic rust ways of dealing with transformations between Result::Errs and Option::None
There are things like the mapping of errors, unwrapping of values and the question mark syntax.
The question mark syntax is explained in the rust reference
Ending the expression with ? will result in the Some’s unwrapped
value, unless the result is None, in which case None is returned
early from the enclosing function.
? can be used in functions that return Option because of the early
return of None that it provides.
This is very similar for the result case:
Ending the expression with ? will result in the Ok’s unwrapped
value, unless the result is Err, in which case Err is returned early
from the enclosing function.
? can be used in functions that return Result because of the early
return of Err that it provides.
The library documentation for Option shows the transformations to convert from Option to Result for some cases as well as many convenience methods.
Footnotes