Rust

Borrowing

about me

Organizer

@raiderrobert 

twitter | github | medium

where I've worked

But this is a Python Conference......

"Python is too slow"

(sometimes)

"Just use C"

"Just use Rust"

https://github.com/rochacbruno/rust-python-example

Killer feature:

Borrowing

via Hello World

Hello World!

fn main() {
    println!("Hello World!");
} 

Hello World!

fn main() {
    let s = String::from("World!");
    println!("Hello {}", s);
} 

Hello World?

fn main() {
    {
        let s = String::from("World!");
    }
    println!("Hello {}", s);
} 

???

rroskam$ rustc hello.rs
error[E0425]: cannot find value `s` in this scope
 --> hello.rs:5:26
  |
5 |     println!("Hello {}", s);
  |                          ^ not found in this scope

Hello World!

fn main() {
    let s = String::from("World");
    say_hello(s);
} 

fn say_hello(some_string: String) {
    println!("Hello {}!", some_string);
}

Hello World Again!

fn main() {
    let s = String::from("World");
    say_hello(s);
    say_hello(s); // what happens here?
} 

fn say_hello(some_string: String) {
    println!("Hello {}!", some_string);
}

Hello World Again?

rroskam$ rustc hello.rs
error[E0382]: use of moved value: `s`
 --> hello.rs:4:15
  |
3 |     say_hello(s);
  |               - value moved here
4 |     say_hello(s); // compiler error
  |               ^ value used here after move
 

WAT?

Let's Look Again!

fn main() {
    let s = String::from("World");
    say_hello(s); // main moves s into say_hello
    say_hello(s); // s is out of scope
} 

fn say_hello(some_string: String) {
    println!("Hello {}!", some_string);
}

Option #1

fn main() {
    let s = String::from("World");
    let x = say_hello(s);
    say_hello(x);
} 

fn say_hello(some_string: String) -> String {
    println!("Hello {}!", some_string);
    return some_string
}

Option #2

fn main() {
    let s = String::from("World");
    say_hello(s.clone());
    say_hello(s.clone());
} 

fn say_hello(some_string: String) {
    println!("Hello {}!", some_string);
}

There must be a better way.

Option 3: Borrowing

fn main() {
    let s = String::from("World");
    say_hello(&s);
    say_hello(&s); // 🎉 it works!
} 

fn say_hello(some_string: &String) {
    println!("Hello {}!", some_string);
}

Skipping Ahead

Hello {{Your Name}}!

use std::env;

fn main() {
    let mut args: Vec<String> = env::args().collect();
    let name = args.remove(1);
    say_hello(&name);
} 

fn say_hello(some_string: &String) {
    println!("Hello {}!", some_string);
}

Extra friendly!

use std::env;

fn main() {
    let mut args: Vec<String> = env::args().collect();
    let name = args.remove(1);
    say_hello(&name);
    say_hello(&name);
} 

fn say_hello(some_string: &String) {
    println!("Hello {}!", some_string);
}

MOAR INPUTS!

use std::env;

fn main() {
    let args: Vec<String> = env::args().collect();
    let mut name = args[1].clone();
    let count:u32 = args[2].clone().parse().unwrap();
    for _x in 0..count {
        say_hello(&mut name);
    }
} 

fn say_hello(some_string: &mut String) {
    some_string.push_str("🎉");
    println!("Hello {} !", some_string);
}

Rust: Borrowing

By Robert Roskam

Rust: Borrowing

  • 355