Rust Flash Cards

Flash Cards

Flash cards are brief descriptions of a Rust type, trait, or keyword. Each has a description with a small useage example and diagram. The purpose is to provide a quick way to grasp an important language model or feature. This could grow to be quite large, but for now it will be relatively small, trying to help people to start quickly with the Rust language.
TopicDescriptionDiagram
Copy type Construction and assignment implicitly copies contents of source to destination.
  • contiguous memory, satisfies Copy trait
  • examples: primitive types like i8, i16, .., u8, u16, .., f32, f64, &str, immut refs, ..
  • constr: let x = y;  assign: x = y;   // copies
  • Code demo: Copy
Move type Construction and assignment implicitly moves resources of source to destination.
  • non-contiguous memory, does not implement Copy trait
  • move transfers ownership of resources from source to destination
  • examples:
    Vec<T>, String, aggregate types holding at least one move type, mut refs, many user defined types
  • constr: let x = y;  assign: x = y;   // moves y invalid after operation
  • Code demo: move
Clone type Construction and assignment explicitly copies resources of source to destination.
  • examples: Vec<T>, String, many user defined types
  • constr: let x = y.clone();  assign: x = y.clone(); y valid after operation
  • Code demo: clone
Reference type Pointer to instance of some type with special rules:
  • must be initialized when declared: let r = &u, let mr = &mut v
  • references cannot concurrently share mutation of referend
  • lifetime is scoped based, from the point of declaration to the end of that scope
  • The rules above are basis for Rust's memory safety. There are a few more details explained in Safety
  • All other pointer types must reside in unsafe { ... } Goal: never use unsafe in code you write. Let the std library types do any required unsafe processing. They have been written by the Rust team and are thoroughly vetted and wrapped in a safe interface.
  • Code demo: Reference
Vec<T> Collection of instances of type T residing in contiguous heap memory.
  • consists of control block in stack holding pointer to array of T in heap
  • reallocates heap memory to accept new instance when capacity is full
  • create vector: let v = Vec::<int>::new();
    let w: Vec<T> = vec![t1, t2, t3];
  • v and w are dropped, returning resources, when they go out of scope.
  • Code demo: create and display vectors
String Collection of utf-8 characters residing in contiguous heap memory.
  • consists of control block in stack holding ptr to contiguous heap memory allocation.
  • a utf-8 character may occupy from 1 to 4 bytes, allowing a large collection of language sets, e.g., ASCII, Unicode, Kanji, Arabic, ...
  • The item above means that Rust std::String instances cannot be indexed. There is a string iterator, called chars(), that understands byte sequences that define utf-8 character boundaries. let s = String::from("a literal string");
    let c2 = s.chars().nth(4).unwrap();
  • reallocates heap memory to accept new character(s) when capacity is full
  • create String: let s = String::new();
    let t = String::from("a string");
  • s and t are dropped, returning resources, when they go out of scope.
  • Code demo: create and manipulate String and str
str str is a copy type that represents a literal string in contiguous block of memory
  • converting between str and String:
      let s = "an ordered collection of utf-8 characters";
      let t = String::from(s);
      let u = &S;
  • Literal strings are almost always used via a reference, e.g., &s
  • Sample code in Rust Playground: copy str demo
Box<T> Box is a smart pointer to an instance of type T on the heap.
  • Box is the only safe way to allocate an instance in the heap
  • let s = String::from("string in heap");
    let x = Box::new(s); // moves s into Box
  • a Box is dropped to release its resource when it goes out of scope
  • a Box instance is implicitly dereferenced to provide the interface of its inner instance
  • Code demo: store, modify, and read value in heap
 

Getting Started References

Table 1., below, provides a few references suitable for Rust beginners. They all assume competence in some modern programming language, but not with Rust. You will find references specific to the first BuildOn step in Table 2., further down this page.
 

Table 1. - Beginning Rust References

Topic Description Link
Hello World A quick taste of how C++, Rust, and C# are similar and different - the infamous HelloWorld program. Hello Worlds
Survey This blog post walks through most of Rust, explaining, with code fragments and short simple language, the core ideas. A half-hour to learn Rust
Survey This is an extensive github Readme.md file with table of contents and most of the ideas expressed in simple language with lots of details. easy-rust
Syntax This is is a github Gist that presents much of the Rust syntax without much drill-down. This is a good starting place, but you will find, as you learn Rust, that a lot is missing here. But starting out, that is probably a good thing. Almost all the missing details you can find in the previous easy-rust link. syntax guide
Language Cheat Sheet This is a very condensed illustration of almost all of the Rust ideas - little tiny statements, code fragments, and a lot of diagrams if you scroll down far enough. Rust Lanaguage Cheat Sheet
Safety Rust definitions, invariants, syntax checking rules, and types that make Rust code safe by construction. Rust Bites Safety
Ownership Discussion of the ownership rules with several small code examples. Rust Bites Ownership
Rust Story A narrative walk through of the Rust Language, provided by this site. Rust Story
Rust Bite by Byte A sequence of small bites from the Rust language with examples. Rust Bites
The first four references are the things to look at forst. The rest you might skim over, just to see what is there, and come back later for details as you need them.
 
 toggle menu