113
Introduction to Rust (Programming Language) Robert “Bob” Reyes 08 Sep 2017 # MozillaPH # RustPH

Rust 101 (2017 edition)

Embed Size (px)

Citation preview

Page 1: Rust 101 (2017 edition)

Introduction to

Rust (Programming Language)

Robert “Bob” Reyes08 Sep 2017

#MozillaPH

#RustPH

Page 2: Rust 101 (2017 edition)

#MozillaPH

#RustPH

If you’re on social media, please use

our official hashtags for this talk.

Page 3: Rust 101 (2017 edition)

About Me

• Mozilla Rep for the PHL since 2011

• Technopreneur at TurfSitePH.net

• Technology Columnist at MB.com.ph

• Team Manager at SIPFC.org

• Dad of Xeon & Haswell

Page 4: Rust 101 (2017 edition)

Being an Open Source Dev

Page 5: Rust 101 (2017 edition)

Target Audience

• People with some background in programming (any language).

• People with zero or near-zero knowledge about Rust (Programming Language).

• People who wants to learn a new programming language.

Page 6: Rust 101 (2017 edition)

Agenda

• Mozilla in the Philippines

• Installing Rust

• Hello World, the Rust way

• Intro to Cargo

• IDE Support

• Variables & Constants

• Simple Arithmetic Functions

Page 8: Rust 101 (2017 edition)

#MozillaPH

Page 9: Rust 101 (2017 edition)

History of Mozilla

On 23 Feb 1998, Netscape Communications Corp.

created a project called Mozilla (Mosaic + Godzilla).

Mozilla was launched 31 Mar 1998.

Page 10: Rust 101 (2017 edition)

We have a NEW brand identity…

Page 11: Rust 101 (2017 edition)

We have a NEW brand identity…

Page 13: Rust 101 (2017 edition)

Mozilla’s Mission

To ensure the Internetis a global public resource, open & accessible to all.

Page 14: Rust 101 (2017 edition)

Get involved …

Page 15: Rust 101 (2017 edition)

Some stuff that we are working on …

Page 16: Rust 101 (2017 edition)
Page 17: Rust 101 (2017 edition)

#MozillaPH

Page 18: Rust 101 (2017 edition)
Page 19: Rust 101 (2017 edition)

#MozillaPH

Page 20: Rust 101 (2017 edition)

How to be part of

MozillaPH?

Page 21: Rust 101 (2017 edition)

Areas of Contribution

Helping Users (Support)

Testing & QA

Coding

Marketing

Translation & Localization

Web Development

Firefox Marketplace

Add-ons

Visual Design

Documentation & Writing

Education

Rust Development

http://join.mozillaph.org

Page 22: Rust 101 (2017 edition)

Join MozillaPH now!

http://join.mozillaph.org

Page 23: Rust 101 (2017 edition)

Internship

at Mozilla

https://careers.mozilla.org/university/

Page 24: Rust 101 (2017 edition)

Mozilla HQ

Page 25: Rust 101 (2017 edition)

Mozilla HQ

Page 26: Rust 101 (2017 edition)

Mozilla HQ

Page 27: Rust 101 (2017 edition)

Mozilla HQ

Page 28: Rust 101 (2017 edition)

Mozilla HQ

Page 29: Rust 101 (2017 edition)

Mozilla HQ

Page 30: Rust 101 (2017 edition)

Mozilla HQ

Page 31: Rust 101 (2017 edition)

Mozilla HQ

Page 32: Rust 101 (2017 edition)

Mozilla HQ

Page 33: Rust 101 (2017 edition)

Mozilla HQ

Page 34: Rust 101 (2017 edition)

Mozilla HQ

Page 35: Rust 101 (2017 edition)

Mozilla HQ

Page 36: Rust 101 (2017 edition)

Mozilla HQ

Page 37: Rust 101 (2017 edition)

Mozilla HQ

Page 38: Rust 101 (2017 edition)

Mozilla HQ

Page 39: Rust 101 (2017 edition)

Mozilla HQ

Page 40: Rust 101 (2017 edition)

Mozilla HQ

Page 41: Rust 101 (2017 edition)

Mozilla HQ

Page 42: Rust 101 (2017 edition)

Mozilla HQ

Page 43: Rust 101 (2017 edition)
Page 44: Rust 101 (2017 edition)

#MozillaPH

Page 45: Rust 101 (2017 edition)
Page 46: Rust 101 (2017 edition)

What is

Rust?

Page 47: Rust 101 (2017 edition)

What is Rust?

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

• Compiles to Native Code like C++ & D.

• Strength includes memory safety & correctness (just like in C).

“Rust is a modern native-code language with a focus on safety.”

Page 48: Rust 101 (2017 edition)

What is Rust?

• At present…

• Rust has more than 6.8K libraries, with more than 85M downloads at crates.io

• More than 40 companies publically using in production (Friends of Rust).

Page 49: Rust 101 (2017 edition)

Mozilla &

Rust?

Page 50: Rust 101 (2017 edition)

Mozilla ❤️ Rust

• Started as Mozillian Graydon Hoare’s side project.

• He began working on Rust full time in 2009.

• Rust 1.0 released in 2015.

• New releases every six (06) weeks.

• Rust is Mozilla-supported, rather than Mozilla-owned.

Page 51: Rust 101 (2017 edition)

Rust & Servo

Page 52: Rust 101 (2017 edition)

Rust & Servo

• One part of Mozilla is working on improving Rust.

• Another part is building a brand new web rendering engine using Rust: Servo.

• High-level goals of Servo:

• Implement parallel layout.

• Experiment with alternative rendering design.

• Investigate feasibility of complete JS GC integration.

• Incubator for experiments that can be reintegrated into Firefox.

Page 53: Rust 101 (2017 edition)

Rust & Firefox

Page 54: Rust 101 (2017 edition)

Rust & Firefox

• We are already shipping Rust code in Firefox (inside the media stack).

• The process of re-writing existing C++ code to Rust is called Oxidation.

• Some of the most challenging projects related to Oxidation is part of the Quantum project.

• The amount of Rust code in Firefox will increase over time.

Page 55: Rust 101 (2017 edition)

Rust & Firefox

Page 56: Rust 101 (2017 edition)

Why

Rust?

Page 57: Rust 101 (2017 edition)

Why Rust?

• Rust solves two core problems from C++ & similar low-level programming languages:

• Data races in multithreaded code.

• Use-after-free (UAF) errors.

• UAF’s are huge source of security exploits in Firefox & other browsers.

• Both problems are addressed by making “ownership” explicit.

Page 58: Rust 101 (2017 edition)

Top 10 IoT Programming

Languages

1. C Language

2. C++

3. Python

4. Java

5. JavaScript

6. Rust

7. Go

8. Parasail

9. B#

10.Assembly

• No particular order.

• Based on popularity & following.

Page 59: Rust 101 (2017 edition)

Low-Level

vs

High-Level

Page 60: Rust 101 (2017 edition)

Programming Languages

Hardware

Machine Language

Assembly Language

High-Level LanguageFortran | C | Pascal

OO & Visual Languages

C++ | D | Rust

Page 61: Rust 101 (2017 edition)

Ownership in Rust

Page 62: Rust 101 (2017 edition)

Ownership in Rust

• Encapsulates two (02) concepts:

• The lifetime of a value (i.e. when it is valid to interact with it).

• Unique vs Shared Values.

• Leads to a few rules:

• Uniquely-owned values are destroyed when they go out of scope.

• Only uniquely-owned values can be mutated.

• Borrowing a value means it is no longer uniquely owned.

Page 63: Rust 101 (2017 edition)

Ownership in Rust (Analogy)

• I have a coloring book, a marker & some friends.

• If I leave, the coloring book & marker leave with me.

• If a friend borrows the book, I can no longer color it.

• My friends can also lend it to other friends.

• If someone leaves while my friends are borrowing the book, they give it back before leaving.

Page 64: Rust 101 (2017 edition)

Ownership in Rust (Analogy)

• I can’t leave if the book is still borrowed.

• Either I wait, or I should have given it away.

• I can also lend someone the marker along with the book, if they want to color in it.

Page 65: Rust 101 (2017 edition)

How Does Ownership Help?

• Use-After-Free

• Can’t return/store pointer to an object that may be deallocated before pointer.

• Can’t use immutable & mutable pointers to the same object.

• Data Races

• Forced transfer ownership of data or copy when using multiple threads.

• Only types that abide by compiler rules can be shared – ensures type locking/atomic access is used.

Page 66: Rust 101 (2017 edition)

Race

Condition

Page 67: Rust 101 (2017 edition)
Page 68: Rust 101 (2017 edition)

What is Race Condition?• A race condition is…

• An undesirable situation.

• Occurs when a device or system attempts to perform two or more operations at the same time.

• But because of the nature of the device or system, the operations must be done in the proper sequence to be done correctly.

Page 69: Rust 101 (2017 edition)

Features of

Rust?

Page 70: Rust 101 (2017 edition)

Features of Rust

• Zero-cost abstractions

• Move semantics

• Guaranteed memory safety

• Threads without data races

• Trait-based generics

• Pattern matching

• Type inference

• Minimal runtime

• Efficient C bindings

Page 71: Rust 101 (2017 edition)

Projects using Rust

Page 72: Rust 101 (2017 edition)

Projects Using Rust

Magic Pocket

Dropbox's file storage system that powers their Diskotech petabyte storage machines.

Servo

Mozilla's new parallel rendering engine developed in collaboration with Samsung.

OpenDNS

Uses Rust in two of its components.

Redox OS

A microkernel operating system being developed in Rust.

Page 73: Rust 101 (2017 edition)

Projects Using Rust

Xi Editor

The xi editor project is an attempt to build a high quality text editor, using modern software engineering techniques.

GNU uutils coreutils

uutils is an attempt at writing universal (as in cross-platform) CLI utils in Rust. This repo is to aggregate the GNU coreutils rewrites.

Neon

A safe Rust abstraction layer for native Node.jsmodules.

Page 74: Rust 101 (2017 edition)

Installing

Rust

Page 75: Rust 101 (2017 edition)

Installing Rust

• Installer for Windows, Mac OS X & Linux available via https://www.rust-lang.org

• If you wish to run Rust on your local machine when you’re offline.

• Rust Playground [https://play.rust-lang.org]

• If you are online, you may opt to use this one instead.

• You may use ANY text editor to code in Rust.

• As a practice, please save your Rust code using .rs file extension.

Page 76: Rust 101 (2017 edition)

Rust Playground

Page 77: Rust 101 (2017 edition)

Rust Playground (Run)

Page 78: Rust 101 (2017 edition)

Function

Main()

Page 79: Rust 101 (2017 edition)

Function main()

• Every Rust program must have at least one (01) function.

• Simplest possible function declaration is named as “main”

fn main() {

}

• Functions can also take arguments

fn print_number(x:i32) {

println!(“x is: {}”, x);

}

Page 80: Rust 101 (2017 edition)

Hello Worldin Rust

Page 81: Rust 101 (2017 edition)

helloworld.rs

fn main()

{

println!(“Hello world in Rust!”);

}

Page 82: Rust 101 (2017 edition)

helloworld.rs (Run)

Page 83: Rust 101 (2017 edition)

Cargo

Page 84: Rust 101 (2017 edition)

Cargo

• A tool that allows Rust projects to declare their various dependencies & ensure that you’ll always get a repeatable build.

• Cargo does:

1. Introduces two (02) metadata files with various bits of project information.

2. Fetches & builds your project’s dependencies.

3. Invokes rustc or another build tool with the correct parameters to build your project.

4. Introduces conventions to make working with Rust projects easier.

Page 85: Rust 101 (2017 edition)

Cargo

• To start a new project with Cargo, we invoke in the command line:

cargo new hello_world --bin

• Cargo will generate the following files & folders:

Cargo.toml [file]

src [folder]

main.rs [file]

• Acts as a manifest file

• Contains all of the metadata that Cargo needs to compile your project.

Page 86: Rust 101 (2017 edition)

TOML, what?!

Page 87: Rust 101 (2017 edition)

TOML

• Tom’s Obvious, Minimal Language (or some say, Tom’s Own Markup Language)

• Created by Tom Preston-Werner

• Aims to be a minimal configuration file format that's easy to read due to obvious semantics.

• Designed to map unambiguously to a hash table.

• Should be easy to parse into data structures in a wide variety of languages.

Page 88: Rust 101 (2017 edition)

Cargo.toml

Page 89: Rust 101 (2017 edition)

Cargo.toml

[package]

name = "hello_world”

version = "0.1.0”

authors = ["Your Name <[email protected]>"]

Page 90: Rust 101 (2017 edition)

IDE Support

Page 91: Rust 101 (2017 edition)

IDE Support

• Modern IDEs give developers a massive increase in productivity.

• Several community projects have provided an excellent start towards IDE support.

• Good IDE support requires a number of components:

• The compiler must be modified to operate in a different mode.

• Must provide name & type information from the compiler to the IDE.

• Must write plugins for the IDEs themselves so they know what to do with Rust projects.

Page 92: Rust 101 (2017 edition)

IDE Support

• Available IDE Plugins:

• Eclipse [https://github.com/RustDT/RustDT]

• Intellij IDEA [https://github.com/intellij-rust/intellij-rust]

• Visual Studio [https://github.com/PistonDevelopers/VisualRust]

• Editor Plugins:

• Atom [https://atom.io/packages/language-rust]

• Emacs [https://github.com/rust-lang/rust-mode]

• Sublime Text [https://packagecontrol.io/packages/Rust]

• Vim [https://github.com/rust-lang/rust.vim]

• Visual Studio Code [https://github.com/saviorisdead/RustyCode]

Page 93: Rust 101 (2017 edition)

More on IDE Support

https://www.rust-lang.org/ides.html

Page 94: Rust 101 (2017 edition)

Types & Variables

Page 95: Rust 101 (2017 edition)

Variables

• Variable Declaration

fn main() {

let a:u8 = 123;

}

• Whereas

• a variable

• u8 data type (unsigned; 0 or positive; 8-bit)

• 123 value of variable “a”

Page 96: Rust 101 (2017 edition)

Numeric Data Types

• i8 8-bit signed integer

• i16 16-bit signed integer

• i32 32-bit signed integer

• i64 64-bit signed integer

• u8 8-bit unsigned integer

• u16 16-bit unsigned integer

• u32 32-bit unsigned integer

• u64 64-bit unsigned integer

• isize pointer-size signed integer

• usize pointer-size unsigned integer

• f32/f64 32/64-bit floating point

Page 97: Rust 101 (2017 edition)

Mutability in Rust

Page 98: Rust 101 (2017 edition)

Mutability

• The ability to change something.

• Mutable variable binding.

• You’re allowed to change what the binding points to.

let x = 5;

x = 6; will result to an error!

• We can use the mut keyword:

let mut x = 5;

x = 6; no problem; no error!

Page 99: Rust 101 (2017 edition)

Constants

• Aside from using variables, we can also declare constants in Rust:

const PI:u8 = 3;

no fixed memory address

memory safety is NOT compromised

static X:i32 = 123;

Page 100: Rust 101 (2017 edition)

Operatorsin Rust

Page 101: Rust 101 (2017 edition)

Arithmetic Operators

+ addition & array/string concatenation

- subtraction

* multiplication

/ quotient

% remainder

Page 102: Rust 101 (2017 edition)

Arithmetic Operators

• Rust DOES NOT support ++ and – used in other programming languages.

• a = a+1; or

• a += 1;

• b = b-1; or

• b -= 1;

• You may also use the following in Rust

• *=

• /=

• %=

Page 103: Rust 101 (2017 edition)

Arithmetic Operators

let mut a = 2+3*4;

println!(“Answer = {}.”, a);

let mut a = (2+3)*4;

println!(“Answer = {}.”, a);

Page 104: Rust 101 (2017 edition)

Arithmetic Operators

let mut a = 10/3;

println!(“Answer = {}.”, a);

let mut a = 10%3;

println!(“Answer = {}.”, a);

let a=10;

println!(“Remainder of {} / {}

= {}”, a, 3, (a%3));

Page 105: Rust 101 (2017 edition)

More Sample

(Try them out)

Page 106: Rust 101 (2017 edition)

Arithmetic Operators (eg1)

fn main() {

let num1 = 20;

let num2 = 10;

println!(“The SUM of the numbers =

{}”, (num1 + num2));

println!(”The DIFFERENCE of the

numbers = {}”, (num1 – num2));

… and so on

Page 107: Rust 101 (2017 edition)

Arithmetic Operators (eg2)

fn main() {

let num1 = 20;

let num2 = 10;

let sum = num1 + num2;

let dif = num1 – num2;

let pro = num1 * num2;

let quo = num1 / num2;

println!(“The SUM of the numbers =

{}”, sum);

… and so on

Page 108: Rust 101 (2017 edition)

Q&A

Page 109: Rust 101 (2017 edition)

References

Page 110: Rust 101 (2017 edition)

Reference Materials

• The Rust Programming Language Book

• https://doc.rust-lang.org/book/

• Rust by Example

• http://rustbyexample.com

• Rust User Forums

• https://users.rust-lang.org

• https://internals.rust-lang.org

Page 111: Rust 101 (2017 edition)

facebook.com/groups/rustph

Page 112: Rust 101 (2017 edition)

https://rustph.slack.com

To request an invite:

https://rustphslack.herokuapp.com

Page 113: Rust 101 (2017 edition)

Thank you!

Maraming salamat po!

http://www.mozillaphilippines.org

[email protected]