about
Rust Bite Repo
07/02/2024
Rust Bite code

Consuming Rust bite by byte  Repository

Short bites about Rust features

Quick Status Many short demos no known defects Demonstration code yes Documentation yes Test cases NA Static library NA Build requires Rust installed Planned design changes Add many short stories
Bite Index Introduction Motivation Starting Samples and ideas Tooling VS Code, dev flow Safety Compiler enforcements Undef Behavior Causes Data Types, instance semantics FlashCards Type diagrams Facts Summary of types and basic ideas Strings String, str, other string types Data Structs Data structures Smart Pointers Box, Rc, Arc, RefCell LifeCycle Construction to Destruction Ownership Single ownership, borrowing Generics & Traits Definitions, examples Functions Functions and closures Structs Methods & Traits LifeTime Guarantee no dangling refs Abstractions Domain specific types Enumerations Collecting related types, matching Error Handling Result, bubbling Options Representing Some or None Conversions Converting between compatible types Collections Type diagrams Iterators Iterating over collections Idiomatic Code Clippy & API guidelines Macros Declarative macros Threads Threads, sharing, interior mutability Sync Thread synchronization with locks Channels Sending messages between threads Regular Expressions Using regex crate, string matching Hacks & Helpers Thread helpers, Date & Time Code Experiments Experimenting in playground Playground Examples Motivation Glossary Glossary of Rust terms    
"Try to learn something about everything and everything about something."
- Thomas Huxley

Contents:

Code List, Bite List, First Bite The Rust ecosystem has many great documents, like those linked in References. But, it can be a bit daunting to start with a book or large document, or even try to figure out which one to start. Here we try another approach. This repository is growing a sequence of short stories (Bites) about Rust, the programming language. They are intended to provide easy entry into Rust by biting off small pieces, each discussed in a single page. Many of the bites have a few simple exercises - not intended to build mental muscle with work, but rather to move toward familiarity by writing a few simple lines of code. Some Bites may eventually have accompanying videos if that seems worthwhile. The goal is to make each Bite small and clear enough to digest in a half hour before lunch. The effort needs to be low enough that you will actually read them and write a few lines of code. Many of the bites are extracts taken from chapters in the Rust Story or one of its presentations. At the end of each bite you may find a link to an expanded discussion there.
I think of these Bites as a work book for the Rust Story. The story provides a fairly complete narrative description of the Rust programming language. Each Bite takes an important feature or capability of the language, provides a consise statement, an example of its operation, and, for most of the Bites, a short set of exercises designed for newcomers to the language.

Bites:

Here are the currently implemented and planned Bites. I expect the few missing will be completed by the end of 2022. Ordering of some of these Bites may change. Ordering so that each Bite depends only on its predecessors doesn't necessary provide a good logical ordering. I'll be thinking about this as the list gets completed.

  1. Introduction What are RustBites?

  2. Starting Quick references to get the idea, and longer references to learn how to write the code.

  3. Tooling Editing and building Rust - first simple projects, then multi-package projects

  4. Safety Invariants, Ownership, Interior Mutability

  5. Undefined Behavior Examples of undefined behavior using C++

  6. Data Bind, Copy, Move, Clone, and Mutate

  7. Rust Flash Cards Quick view of Rust type system

  8. Rust Facts More type system details

  9. std Data Structures array, tuple, struct, String, Vec, Set, Map

  10. smart pointers Box<T>, Rc<T>, Arc<T>, RefCell<T>

  11. LifeCycle new, Bind, Drop

  12. ownership Mutation, single ownership, borrowing and transfer, RwLock semantics

  13. generics and traits Generic types, contracts, methods, implementing traits, derived traits.

  14. functions Pass by value and reference, accepting and returning other functions.

  15. structs Declarations, members, implementing methods.

  16. lifetime annotations Generic annotations that help compiler ensure that references don't outlive referents.

  17. Abstractions Defining application abstractions, e.g., user defined types, with traits and structs.

  18. error handling Returning results that may fail, matching, bubbling up error events.

  19. Collections std::collections types and user-defined collections.

  20. Iterators Declarative access and mutation of collections.

  21. Idiomatic Rust Declarative access and mutation of collections.

  22. Threads Creation of threads, thread-safe sharing.

  23. Synchronization Serializing access with Mutex, Convar, and Arc.

  24. Channels Moving objects between threads.

  25. async-await Concurrency without creating threads.

  26. macros Defining macros, matching.

  27. RegEx Regular expressions using regex crate from crates.io

  28. Hacks and Helpers Useful ideas and techniques

  29. Glossary of Terms Term, definition, links
These bites present a lot of small simple demonstration codes, and a few that are larger. All of the code fragments shown in the Bites are included, as complete programs, below.
 

Bite Demonstration Code:

  1. _Experiments
    Demonstration of trait specialization requiring nightly build, e.g., not yet stable Rust.
  2. bite_data_ex1
    Verifies copy operation using pointers.
  3. closure_param
    Example of function accepting a closure parameter.
  4. closures
    Demonstrates closures with copy and with move capture.
  5. data_str
    Illustrates use of arrays, structs, strings, vectors, and maps. Also demonstrates use of generic display functions for iterable collections and folded displays.
  6. dispatch
    Demonstrates both static and dynamic dispatch for trait functions and trait objects.
  7. exercises
    Solutions for three more difficult smart pointer exercises.
  8. functions_and_methods
    • functions
      Pass arguments by value and reference, pass and return functions, use static and dynamic dispatch.
    • methods
      Demonstrate non-generic and generic methods.
    • return_values
      Illustrate Option<T> and Result<T, E> return values.
  9. generics_and_traits
    Demonstrate generic function that accepts generic struct as argument, both constrained by Debug trait.
  10. life_cycle
    Creates type with instances that announce their creation, cloning, and drops. Use that to illustrate life cycle for objects in several common scenarios.
  11. modeling
    Illustrate building objects that implement traits. Use them to build trait objects and use to demonstrate Object Oriented design patterns that use dynamic dispatching.
  12. ownership
    Illustrate some operations that satisfy the Rust ownership model and some that don't with commentary in comments.
  13. point
    Defines a SpaceTime trait for points, implements a Point class that implements, and demonstrate how that might be useful.
  14. smartptrs
    • run_time_checking
      Demonstrates use of RefCell to provide inner mutability. That enables code to build that is satisfies ownership policy but compiler can't verify. That uses run-time checks implemented with RefCell.
    • show_arc
      Simple demo of the Arc<T> thread-safe, reference counting, smart pointer.
    • show_rc
      Simple demo of the Rc<T> smart pointer, non-thread-safe, reference counting, smart pointer.
    • show_refcell
      Simple demo of RefCell<T>
    • test_type
      LifeCycle demo type - uses annonciation.
  15. UndefBehavior
    A demonstration of undefined behavior with C++ code.
  16. hacks_and_helps
    Collection of useful code fragments and problem solvers:
    • atomics
      Demos of sharing AtomicBool and AtomicUsize between threads.
    • date_time_timer
      Create time and data stamps, time program events, and set callback timers.
    • Illustrates a common problem encountered when writing asynchronous methods, and how to fix it, keeping asynchronous processing intact.

Build:

Rust code was built with Cargo from Visual Studio Code terminal and tested on Windows 10.

Status:

Most bites are complete.
  Next Prev Pages Sections About Keys