Rust Hack & Learn December 2018

Be Nice and have a Great Time!

We follow the Rust Code of Conduct (www.rust-lang.org/en-US/conduct.html)

In case of problem, feel free to contact either me or Karsten in person or via mail/twitter

Mesosphere

Big Thanks To

Marco Neumann

@crepererum

The Path to Rust 2018

Birth

  • Graydon Hoare
  • only a Makefile

Real GIT Birth

  • OCaml-based bootstrap compiler
  • no Rust-based compiler
  • very different syntax
type buf_writer = obj {
  fn write(vec[u8] buf) -> uint;
};


fn read(vec[u8] v) -> uint {
  auto len = _vec.len[u8](v);
  auto buf = _vec.buf[u8](v);
  auto count = os.libc.read(fd, buf, len);
  if (count < 0) {
    log "error filling buffer";
    log sys.rustrt.last_os_error();
    fail;
  } else {
    ret uint(count);
  }
}
drop {
  os.libc.close(fd);
}
alt, any, as, auth, auto, be, bind, bool, case, chan,
char, check, claim, do, drop, each, else, export, f32,
f64, fail, false, fn, for, i16, i32, i64, i8, if,
import, in, int, io, iter, join, let, log, meta, mod,
mutable, native, obj, port, prove, put, rec, ret,
spawn, state, str, syntax, tag, task, thread, true, tup,
type, u16, u32, u64, u8, uint, unsafe, use, vec, while,
with, yield

0.6

  • green threads
  • different pointer system
# struct Point {x: float, y: float}
let on_the_stack :  Point =  Point {x: 3.0, y: 4.0};
let shared_box   : @Point = @Point {x: 5.0, y: 1.0};
let unique_box   : ~Point = ~Point {x: 7.0, y: 9.0};

1.0

  • stable release
  • cargo + crates.io
  • train-based releases (stable, beta, nightly)
  • as of today: "Begin of 2015 edition"
  • MIR becomes the default
    • prepares non-zeroing drops (1.13)
    • Non-Lexical Lifetimes (NLL) (1.31)
  • better error styles
  • ? operator
fn read_username_from_file() -> Result<String, io::Error> {
    let mut f = File::open("username.txt")?;
    let mut s = String::new();

    f.read_to_string(&mut s)?;

    Ok(s)
}
#[macro_use]
extern crate serde_derive;

extern crate serde_json;

#[derive(Serialize, Deserialize, Debug)]
struct Pet {
    name: String,
}

fn main() {
    let pet = Pet { name: String::from("Ferris") };

    let serialized = serde_json::to_string(&pet).unwrap();
    println!("serialized = {}", serialized);

    let deserialized: Pet = serde_json::from_str(&serialized).unwrap();
    println!("deserialized = {:?}", deserialized);
}
#[derive(Queryable)]
struct Pet {
    name: String,
}

fn main() {
    use diesel_demo::schema::pets::dsl::*;

    let connection = establish_connection();
    let results = pets
        .limit(5)
        .load::<Pet>(&connection)
        .expect("Error loading pets");

    println!("Displaying {} pets", results.len());
    for pet in results {
        println!("{}", pet.name);
    }
}
  • untagged unions for C-interopt
  • break w/ values
union MyUnion {
    f1: u32,
    f2: f32,
}

let u = MyUnion { f1: 1 };

unsafe { u.f1 = 5 };
let value = unsafe { u.f1 };
// old code
let x;

loop {
    x = 7;
    break;
}

// new code
let x = loop { break 7; };
  • associated constants
struct Struct;

impl Struct {
    const ID: u32 = 0;
}

fn main() {
    println!("the ID of Struct is: {}", Struct::ID);
}
  • rustfmt preview (until 1.31)
  • incremental compilation
  • “The Rust Programming Language” Second Edition
  • impl Trait
// before
fn foo() -> Box<Fn(i32) -> i32> {
    Box::new(|x| x + 1)
}

// after
fn foo() -> impl Fn(i32) -> i32 {
    |x| x + 1
}
  • procedural macros
#[route(GET, "/")]
fn index() {
    // ...
}
let sql = sql!(SELECT * FROM posts WHERE id=1);
  • Non-lexical Lifetimes (NLL)
  • const fn
  • Rust 2018
  • rustfmt + clippy
const fn foo(x: i32) -> i32 {
    x + 1
}
fn main() {
    let mut x = 5;

    let y = &x;

    let z = &mut x;
}

Getting Started

Rust

IDE

Any editor + terminal

Docs

More at areweideyet.com

Crates

W/o Installation

Rust Playground (play.rust-lang.org)

Evcxr + Binder (goo.gl/AHsKxe)

Beginner Tasks

Rustlings (github.com/rustlings/rustlings)

Locally or via Rust Playground

Exercism (exercism.io/tracks/rust)

Nice large collections, requires special CLI client

Katas (byron.github.io/rust-hack-and-learn/)

Locally

Advanced Tasks

Call for Participation (goo.gl/66kAvr)

Different Crates

Contribute to Rust (www.rust-lang.org/en-US/contribute-community.html)

Rust Itself

Your Call / Idea

Next Meetup

at Mytaxi

January, 24th

You Talk?

Rust Hack & Learn December 2018

By Marco Neumann

Rust Hack & Learn December 2018

  • 756