34
Intro to Rust Tsuyoshi Maeda Workshop repository: goo.gl/MkesJf

Intro to rust

Embed Size (px)

Citation preview

Intro to RustTsuyoshi Maeda

Workshop repository: goo.gl/MkesJf

About me● Tsuyoshi Maeda● Freelance engineer● Recent my technical interests

○ Serverless○ Blockchain

● Hobbies○ Playing sports (Futsal, Badminton etc)○ Playing the guitar (Rock, J-POP)

Workshop repository: goo.gl/MkesJf

Outline● Targets of this slides● What is Rust?● How do you get started?● What is special about it?● Why should you learn it?

Workshop repository: goo.gl/MkesJf

Targets of this slides● People who

○ Don’t know what Rust is○ Have never used Rust

Workshop repository: goo.gl/MkesJf

What is Rust?

From website of Rust

Workshop repository: goo.gl/MkesJf

What is Rust?

and more ...

What is Rust?2016 2017

- https://insights.stackoverflow.com/survey/2016- https://insights.stackoverflow.com/survey/2017

Developer Survey Results by Stack Overflow

Workshop repository: goo.gl/MkesJf

● Very active (3000 commits in last one month)

What is Rust?

About 1 month ago. Now (01/06/2018)

Workshop repository: goo.gl/MkesJf

What’s Rust?

- He created first version JavaScript in 10 days.

Workshop repository: goo.gl/MkesJf

How do you get started?● Install cargo (Package manager of rust)

○ Go to workshop repository of right bottom and follow “Install cargo”

● Learn Cargo● Learn syntax of rust

Workshop repository: goo.gl/MkesJf

Cargo● Package manager

○ npm (JavaScript)○ gem (Ruby)○ pip (Python)

● Cargo.toml

Cargo● Crates.io

○ Rust package registry■ npmjs■ Rubygems

○ To install■ cargo install `crate`

○ To uninstall■ cargo uninstall `crate`

Cargo(main commands)● To set up project

○ cargo new○ cargo init○ With `--bin` option, src/main.rs is generated instead of src/lib.rs

● To run program○ cargo run

Syntax (Comparing to JavaScript)● Functions● Variables● Scalar types● Strings● Vectors● Structs

Workshop repository: goo.gl/MkesJf

Syntax: Functions● Use “fn” keyword to create function.● Set argument type.● Set return type.● Do not put semicolon on return value.

Workshop repository: goo.gl/MkesJf

function add(a, b) { return a + b;}

function log_something() { console.log(‘something’);}

function log_something_2() { console.log(‘something_2’);}

Syntax: Functions

Rust JavaScript

fn add(a: i32, b: i32) -> i32 { a + b}

fn log_something() { println!(“something”);}

fn log_something_2() -> () { println!(“something”);}

Syntax: Variables● Use “let” keyword to create variable.● All variables are immutable in default.● Put “mut” keyword after “let” to mutate variable.

Workshop repository: goo.gl/MkesJf

Const immut_var = “can not change value”;// immut_var = “I said you cannot change!!”;

let mut_var = “can change value”;mut_var = “updated value!”

Syntax: Variables

Rust JavaScript

let immut_var = “can not change value”;// immut_var = “I said you cannot change!!”;

let mut mut_var = “can change value”;mut_var = “updated value!”

Syntax: Scalar types● Integer:

○ 1, -10, 24, 0

● Floating-point:○ 1.3, -2,6, 10.5

● Boolean:○ true, false

● Character:○ ‘A’, ‘ ’, ‘あ’, ‘龘’ ○ With single quotations.

Workshop repository: goo.gl/MkesJf

Syntax: Strings● More than 2 letters.● With double quotations.● String / &str

○ String: String::from(“abc”), “abc”.to_string();

○ &str: “abc”, &something_string_variable

Workshop repository: goo.gl/MkesJf

Syntax: Strings

Rust JavaScript

fn output_str(input: &str) { println!(“{}”, input);}

fn output_string(input: String) { println!(“{}”, input);}

output_str(“abc”);output_string(“abc”.to_string());

function output_str(input) { console.log(input);}

function output_string(input) { console.log(input);}

output_str(“abc”);output_string(“ABC”);

Syntax: Vectors● Same as array of JavaScript● “vec![...]” is a macro to create Vector instance.● Need to put “mut” if you add element in the vector like push.● Be able to put values that are all only same type.

Workshop repository: goo.gl/MkesJf

Syntax: Vectors

Rust JavaScript

let mut numbers: Vec<i32> = vec![1, 2, 3];numbers.push(4);for number in numbers { println!(“number: {}”, number);}

const numbers = [1, 2, 3];numbers.push(4);numbers.forEach((number) => { console.log(`number: ${number}`);});

Syntax: Structs● Same as object of JavaScript● Be able to create class with “impl” keyword.

○ Instance methods has “&self” as first argument to refer instance itself.

● Need to do following to output information○ Put “#[derive(Debug)]” before “struct” keyword.○ Use “{:?}” or “{:#?}” instead of “{}” in “println!”.

Workshop repository: goo.gl/MkesJf

Syntax: Structs#[derive(Debug)]struct Person { name: String}

impl Person { pub fn new(name: String) -> Person { Person {name: name} } pub fn greet(&self) -> () { println!("My name is {}", &self.name); }}

Rust

fn main() { let person1 = Person::new("Alice".to_string()); let person2 = Person::new("Bob".to_string()); println!("{:?}, {:?}", person1, person2); person1.greet(); person2.greet();}

Workshop repository: goo.gl/MkesJf

class Person { constructor(name) { this.name = name; }

greet() { console.log(“My name is %s”, this.name); }}

Syntax: Structs

JavaScript

function main() { let person1 = new Person("Alice".to_string()); let person2 = new Person("Bob".to_string()); console.log(person1, person2); person1.greet(); person2.greet();}

Workshop repository: goo.gl/MkesJf

Quiz time1. What is the entry point/function of Rust?

○ https://repl.it/repls/BlackSlightFirecrest2. Which function is correct to return value?

○ https://repl.it/repls/QuickwittedWorthwhileEelelephant3. How do you assign value into a variable and change value of the variable?

○ https://repl.it/repls/GreenyellowIllegalOtter

Workshop repository: goo.gl/MkesJf

What is special about it?● Ownership● Be able to Compile to WebAssembly

Workshop repository: goo.gl/MkesJf

Ownership● After assigning a not scalar

variable to a new variable, old variable cannot be no longer used. (move ownership to new variable)

○ Integer is scalar type.○ String is not scalar type.

Workshop repository: goo.gl/MkesJf

Ownership (Borrow)● If you use pass the reference of

variable to a new variable, ownership does not move to the new variable.

○ “str3“ borrows values of “str1” and “str2”

○ “str1” and “str2” still have ownership of the each value.

Workshop repository: goo.gl/MkesJf

WebAssembly

Workshop repository: goo.gl/MkesJf

From MDN

Why should you learn it?● Improve web performance with WebAssembly

○ In my opinion, especially good for game programming.

● Update version actively● blazingly fast, prevents segfaults, and guarantees thread safety (By Rust

home page)

Workshop repository: goo.gl/MkesJf

Workshop● Clone a repository for this workshop (below link.)

○ https://goo.gl/MkesJf ● Try “Basic Requirements”● Try “Advanced Requirements” after “Basic Requirements”

Workshop repository: goo.gl/MkesJf

Online accounts & blogs● Twitter: @duyoji● Github: @duyoji● Qiita: @duyoji● LinkedIn: Tsuyoshi Maeda● Blog

○ English■ http://tsuyoshi-maeda.hatenablog.com/

○ Japanese■ http://duyoji.hatenablog.com/

Workshop repository: goo.gl/MkesJf