Datatypes, etc

Rust Playground

https://play.rust-lang.org/

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

  • 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

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 chars
  • These types can be copied implicitly

  • Numeric literals can have type suffix e.g. 0u64

Tuples

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

  • 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

Last modified: Monday, 13 January 2020, 2:18 PM