| Rust Omits: | References: |
|---|---|
|
Garbage Collection.
Rust uses C++ style scope-based resource management. Its clever design prevents unsound memory accesses
and data races. Compile-time reference analysis, augmented with occasional run-time checks, achieves that.
Rust does not allow raw pointers. References, pointers wrapped in a strict ownership policy, provide
indirection.
|
Safety model Ownership model |
|
function overloading.
Not required, since constructors do not share the type name. Rust generics and traits let you define
functions that operate over a set of types.
|
internals.rust-lang.org StackOverflow |
|
inheritance of non-static implementation
Rust supports inheriting a trait, which may have function definitions but cannot have data members.
A struct inherits a trait through an impl statement:
|
Traits - The Book playground example |
|
implicit conversion between types. All expressions must have exact type matches.
Rust supports casting with the "as" keyword and provides conversion functions for some
standard types. Application programs may also provide conversion methods.
|
Casting - Rust by example |
|
dereferencing raw pointers outside unsafe blocks.
Our goal: avoid unsafe blocks, and instead defer to std::library facilities where the Rust designers used,
and carefully vetted, unsafe blocks to provide the functionality we need.
|
Unsafe - The Book
Unsafe - how and when |
| Rust Changes: | References: |
|
Copy Types - Only types with contiguous memory are implicitly copied.
That is the set of primitive types and aggregates that have only copy type members.
Move Types - All else are implicitly moved.
Construction, assignment, passing arguments, and returning results from functions by value move resources
from source to destination. That is fast, usually just a pointer copy.
Move invalidates the source of the "moved" operation. Using a moved variable is a compile error.
This is one of two frequent Gotchas for new-comers to Rust.
|
RustBites copy & move playground example |
|
semantics for copy types and move types are fixed.
Unlike C++, you do not need to, and cannot, define implicitly called copy and move constructors and
assignment operators.
Rust's data management removes the need for user defined semantics.
|
RustBites_Data |
|
Rust supports deep clones and defines a Clone trait that cloneable types implement.
Clones make deep copies and are always invoked explicitly.
|
RustBites_Data clone playground example |
|
Rust enumerations have elements that may wrap instances of specified types.
|
RustStory_Data enums RustBites_ErrHnd playground example |
|
Rust enumerations are often used with a matching syntax.
|
RustStory_Data enums RustBites_ErrHnd playground example |
|
Rust iterators are similar to C# iterators. They have many predefined adapters.
Strings have an iterator over their characters, chars().
|
RustStory iterators RustBites iterators std::iter::Iterator |
| Rust Requires: | References: |
|
Explicit declaration of mutability:
|
RustBites_Data playground example |
|
No concurrent aliasing and mutation (with references)
This is the other common GotCha for those new to Rust.
A Rust program cannot read or write memory it does not own. A nearly equivalent statement: Rust will not
change the contents a reference points to unless the reference is mutable and responsible for the change.
This also makes linked data structures with references difficult to build in Rust. You can
build them, but it is much easier to build them with indexes in a vector container.
We will (eventually) illustrate that with a
|
RustBites_Safety playground example |
|
References must not outlive their referends.
The Rust compiler's "borrow-checker" analyzes reference lifetimes and refuses to
compile cases where it cannot prove this invariant.
Usually it does that silently, but on occasion it needs help and asks you to provide lifetime annotations.
For an example, see RustBites_Options.
|
Understanding lifetimes RustBites_Options |
|
Only one owner of data
Owned resources are released when owner goes out of scope, and transferred when owner is moved.
|
Ownership by example |
|
Direct access to heap storage uses the smart pointer Box<T>.
Box<T> places t ε T in the heap and implicitly dereferences the Box pointer, giving code
access to T's interface. Most Rust std::library containers place their contents in the heap, e.g.,
vector, dequeue, map, ...
|
RustBites_SmrtPtrs playground example |
|
Function invocations on a generic type, T, require a type constrait on T using a trait declaring that function.
fn demo<T>(rt: &T) where T:Clone {
let tc = rt.clone();
// use tc
}
Here, clone() function is invoked on generic type rt with Clone constraint.
|
Traits - The Rust Book playground example |
| Rust Supports: | References: |
|
Rust supports error handling with enums Result<T, E> and Option<T>:
|
RustBites Error Handling RustStory Error Handling RustBites Options |
|
Rust Strings are collections of utf-8 characters, which may have sizes from 1 to 4 bytes.
That lets Strings contain texts using diacritics, arabic scripts, Sanskrit, Hanzi, kangi, ...
It also means Strings cannot be indexed. Program code uses the iterator chars() instead, which detects
byte sequences that mark character boundaries.
|
RustBites_Strings RustBites_DataStr RustStory_Data std::String std::str |
| Function and Type declarations and definitions may be ordered without concern for dependencies within a single file. | Functions - The Rust Book |
| Panic on integer underflow or overflow in debug mode. | Rust the Book |
| Rust std::ffi library provides OsString which wraps platform specific strings and OsStr, a literal string with platform enccoding. |
std::ffi::OsString std::ffi::OsStr |
| Rust provides PathBuf and Path which wrap OsString and OsStr, respectively, adding methods for handling paths . | std::path::PathBuf, std::path::Path |
| Rust can generate WebAssembly code and Rust Playground can show both Rust code and WASM it generates. | Rust and WebAssembly |
| Topic | Description | link |
|---|---|---|
| Why Rust? | Quick survey of the main Rust concepts. | Why Rust? |
| Rust Intro | Basic introduction in three articles: Basics, Enums and Matching, and Generics. A bit long in places, but with interesting example codes. | Rust in a NutShell |
| Ownership | Very clear discussion of ownership in five short videos. | intorust |
| Rust Survey | Smart discussion that introduces the most important parts of Rust | Considering Rust video |
| Bite | Selected Rust Bites from this site | Link |
|---|---|---|
| Starting | This page | RustBites_Starting |
| Tooling | Installing Rust, Installing Visual Studio Code, setting up, basic work flow | RustBites_Tooling |
| Safety | Rust's safety model | Rustbites_Safety |
| Facts | Quick summaries of basic Rust language information | Rustbites_Facts |
| Flash Cards | Small discussions of important Rust collections, traits, and types | RustBites_FlashCards |
| Data Structures | References for primitive and common std library types | RustBites_DataStr |
| Topics | Descriptions |
|---|---|
| Ownership by Example | Nicely organized and relatively complete discussion of ownership with simple clear examples. |
| Introducing Structs, traits, and zero-cost abstractions | A YouTube video that builds user-defined types and abstracts out key traits using simple examples. The examples get at the core of what zero-cost abstraction means. |
| Rust playground | Playground compiles and executes Rust code from a web interface. It provides a great way to try out Rust syntax and operations. |
| A half-hour to learn Rust | An unusual tour of Rust using small code fragments and quirky commentary. |
|
RustTourAbbrev.pdf RustTour.pdf |
A presentation summarizing essential features of Rust. Several videos cover specific parts of the tour. |
| RustErrorHandling.pdf | A presentation covering interesting mechanisms that Rust provides for handling program run-time errors. |
| Topic | Description | link |
|---|---|---|
| Survey and Reference | Github site with a well written collection of reference materials in a clear simple style, packed with detail. A collection of tutorial videos accompanies it. Highly recommended once you get up to speed (maybe even before then). | easy rust |
| Code Examples | Large collection of examples across the Rust language by the Rust team. | Rust by Example |
| e-Books | An e-Book surveying the best e-Books for Rust. Lots here I did not know about until I saw this. | Little Book of Rust Books |
| Rust Cheat Sheet | Very comprehensive well-organized collection of code snippets with comments. | Cheats.rs |
| Rust Bite by Byte | Sequence of pages on this site, each of which focuses on a single Rust topic | RustBites |
| Mother of RustBites | Narrative discussion of Rust in six chapters | Rust Story |
| std Rust Libs | Excellent documentation for the Rust std libraries. Very readable with access to source code. | std lib |
| More references | The place to look for specialized topics and blogs | Rust Story References |