Writing Programs

  • Programs look a lot like any imperative language

  • Can't have mutable global variables (kind of)

  • Assignment is done with let statement, needn't provide types (static type inference)

  • Types must agree completely, conversions are explicit with as or conversion functions

  • Blocks must be bracketed. (Brackets and parens are kind of interchangeable)

  • Semicolon is statement separator; optional null statements and expressions (comma)

  • Last expression in block is value of that block

  • if statement is ternary expression

  • Fairly normal-looking while and for

  • match is switch equivalent

  • Functions have reasonable declaration syntax using fn

Rust Datatypes

  • Emphasis on safe, efficient, simple

  • Type system borrows heavily from all the other parametric/template type systems out there

  • "Borrow checker" tangled in with the type system statically checks lifetimes

Fundamental Datatypes

  • Unit type: ()

  • Boolean type: bool (true, false)

  • Integer types:

    • i8, u8, i16, u16, i32, u32, i64, u64, i128, u128
    • isize, usize
  • Floating types: f32, f64

  • Character type: char

    • 32-bit Unicode code point
    • Conversion-to implies checking: from_u32 vs from_u32_unchecked
    • Strings are UTF-8, so not array of char
  • These types can be copied implicitly

  • Numeric literals can have type suffix e.g. 0u64, underbars e.g. 1_000_000_u64

Data Structuring

  • Tuple types e.g. (u8, char, f64)

  • Structure types e.g. struct { x: u8, y: char }

  • Enumerated types e.g. enum { X, Y(char) }

  • Arrays with size e.g. [u16;12] type,

  • References to things e.g. &mut [int;12]

  • Box and Vec types

  • Functions and closures are complicated

  • Strings are complicated (!)

Tuples

  • Fixed number of values of diverse type

      (u64, f64, char)
    

    with obvious value representation

      let t = (12, 1.2, 'x');
    
  • Great for e.g. multiple return values, points

  • Copyable if elements are

  • Pattern-matching syntax

  • Accessible by dot-index

Structs

  • Fairly standard structure syntax with colons

      struct Point {
          x: u64,
          y: u64,
      }
    

    initialized as they look

      let p = Point { x: 12, y: 20 };
    

    with all kinds of super-secret shortcuts (see book)

  • Structs can have an "implementation"

      impl Point {
          fn x_coord(&self) -> u64 {
              self.x
          }
      }
    

    for OO-like syntax and vaguely OO semantics

      let px = p.x_coord();
    

Enums

  • Fairly simple

      enum Color {
          Red,
          Green,
          Blue,
          Custom(f64, f64, f64),
      }
    
  • "Disjoint union" / "sum type": value of this type is one of the above. Note that variants can contain values

      let c = Color::Custom(0.7,0.5,0.3);
    

Arrays

  • Rust arrays have their size encoded in the type

      [u64;3]
    
  • Can create with list of values

      let a = [1,2,3];
    

    or with constant value

      let a = [0u64;3];
    

    but cannot be created uninitialized

  • Array lookup and assignment work as expected

      a[2] = 7;
      a[2] + 1
    
  • Array indices are always bounds-checked

References

  • When you stick an & in front of a thing, you get a "sort of pointer" to that thing

  • For arrays, for example, you get a "fat pointer" that keeps track of the position in the array and the length of the resulting "slice"

      let a = [1,2,3,4];
      let ar = &a[2..3];
      println!("{}", (*ar)[0]);
      let b = a.clone();
    
  • That last bit of syntax is obnoxious: fortunately, things are derefed automatically mostly

      println!("{}", ar[0]);
    
  • Much more to say next week about references

Box

  • Causes storage to be allocated (think malloc)

      let b = Box::new(5);
    
  • Boxes can be treated like references

  • Boxes can be passed around and stuff

  • Much more next week

Vec

  • Like Box, but for arrays

  • Can create with Vec::new() or the vec! macro

      let v = Vec::new();
      let w = vec![1,2,3];
    

Strings

  • Representation is UTF-8 stored as sequence of u8

      "h\u{1F980}ello"
    
  • However, protected so that invalid UTF-8 cannot be stored

  • Two types: str is UTF-8-safe array, String is like Vec but UTF-8 safe

  • There's lots of automatic interconversion between stuff to try to make this mess right

  • You probably want to skip ahead to ch 17 (why?) and skim that

Book Example: Iron-GCD

  • Turn the command-line app into a webapp

  • There are a lot of neat web frameworks in Rust

  • Performance is pretty good

Book Example: Mandelbrot

  • Shows use of external crate via module interface

  • Shows performance of Rust

  • Concurrency!

Last modified: Wednesday, 26 June 2019, 2:02 AM