101

*

WHAT
WHY
HOW

WHAT

Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety.

Graydon Hoare's personal project started in 2005

-

Sponsored (not owned) by Mozilla since 2009

-

Rust 1.0 since May 2015

-

Current stable version 1.24

-

6 weeks release cycle

COMMUNITY

WHY

Must be this tall to write multi-threaded code

Just follow these rules perfectly,

you’re smart.

C/C++

Inspired by @qedunham

Wait a minute,

I’ll take care of it.

Java and others

Hack without fear!

Rust

Speed
Safety
PARALELLISM

Choose all three!

Zero-cost abstractions

-

Memory safety without garbage collector

-

Threads without data races

class String  
  def blank?
    /\A[[:space:]]*\z/ === self
  end
end 
964K iter/sec

Inspired by @nikomatsakis

static VALUE rb_str_blank(VALUE str) {
  rb_encoding *enc;
  char *s, *e;
  enc = STR_ENC_GET(str);
  s = RSTRING_PTR(str);
  if (!s || RSTRING_LEN(str) == 0) return Qtrue;
  e = RSTRING_END(str);
  while (s < e) {
    int n;
    unsigned int cc = rb_enc_codepoint_len(s, e, &n, enc);
    switch (cc) {
      case 9:
      case 0xa:
      case 0xb:
      case 0xc:
      case 0xd:
      case 0x20:
      case 0x85:
      case 0xa0:
      case 0x1680:
      case 0x2000:
      case 0x2001:
      case 0x2002:
      case 0x2003:
      case 0x2004:
      case 0x2005:

      case 0x2006:
      case 0x2007:
      case 0x2008:
case
0x2009: case 0x200a: case 0x2028: case 0x2029: case 0x202f: case 0x205f: case 0x3000:
          break;
      default:
          return Qfalse;
    }
    s += n;
  }
  return Qtrue;
}
10.5M iter/sec
pub extern "C" fn fast_blank(buf: Buf) -> bool {  
    buf.as_slice().chars().all(|c| c.is_whitespace())
}
11M iter/sec \o/

Get Rust string slice

Get iterator over each character

Are all characters whitespaces?

void add_one(std::unique_ptr<int> num) {
  *num += 1;
}

int main() {
  auto x = std::make_unique<int>(5);
  printf("%i\n", *x);
  add_one(std::move(x));
  printf("%i\n", *x);
}

 

C++

% g++ -Wall -std=c++14 cpp_smart_pointers.cpp  -o cpp_smart_pointers

% ./cpp_smart_pointers
5
[1]    28585 segmentation fault (core dumped)  ./cpp_smart_pointers

C++

no warning

fn add_one(mut num: Box<i32>) {
  *num += 1;
}

fn main() {
  let x = Box::new(5);
  println!("{}", *x);
  add_one(x);
  println!("{}", *x);
}

Rust

error[E0382]: use of moved value: `*x`
 --> src/main.rs:9:18
  |
8 |   add_one(x);
  |           - value moved here
9 |   println!("{}", *x);
  |                  ^^ value used here after move
  |
  = note: move occurs because `x` has type `std::boxed::Box<i32>`, which does not implement the `Copy` trait

error: aborting due to previous error
void example() {
  vector vector;
  vector.push_back(5);
  auto& element = vector[0];
  vector.push_back(6);
  cout << element;
}
...
data
length
capacity
5
6
5

Invalid reference


Undefined

behaviour

element

C++

supose it is 1

fn main() {
  let mut v = vec![];
  v.push(5);
  let x = &v[0];
  println!("{}", x);
  v.push(6);
  println!("{}", x);
}

Rust

error[E0502]: cannot borrow `v` as mutable because it is also borrowed as immutable
 --> src/main.rs:6:5
  |
4 |     let x = &v[0];
  |              - immutable borrow occurs here
5 |     print!("{}, ", x);
6 |     v.push(6);
  |     ^ mutable borrow occurs here
7 |     println!("{}", x);
8 | }
  | - immutable borrow ends here

error: aborting due to previous error

Rust

fn main() {
  let mut v = vec![];
  v.push(5);
  {
    let x = &v[0];
    println!("{}", x);
  }
  v.push(6);
  let x = &v[0];
  println!("{}", x);
}

Rust

OWNERSHIP and Borrowing

Ownership

Mutable borrow

Immutable borrow

fn f(x: Type) {...}
fn f(x: &mut Type) {...}
fn f(x: &Type) {...}
  • Total control
  • read-write
  • one at a time
  • read-write
  • Share as you like
  • read-only

Inspired by Jeena Lee and @lastontheboat 

HOW

ONLINE

RUSTUP

curl https://sh.rustup.rs -sSf | sh

CARGO

TESTING

pub fn add_two(a: i32) -> i32 {
    a + 2
}
#[test]
fn it_works() {
    assert_eq!(add_two(2), 4);
}

IDE

RUSTLANG NURSERY

COMMUNITY CHANNELS

Users discourse forum

Internals discourse forum

r/rust on Reddit

Rust StackOverflow tag

#rust-beginners

LEARN

THANK YOU!

Rust 101

By Fernando Jiménez Moreno