Rust: system programming with guarantees

Similar documents
Introduction to Rust. CC-BY Mozilla. Jonathan Pallant

Index. object lifetimes, and ownership, use after change by an alias errors, use after drop errors, BTreeMap, 309

CMSC 330: Organization of Programming Languages. Ownership, References, and Lifetimes in Rust

CMSC 330: Organization of Programming Languages

Rust. A new systems programming language. 1.0 was released on May 15th. been in development 5+ years. releases every 6 weeks

Advances in Programming Languages

Rust for C++ Programmers

Guaranteeing memory safety in Rust

Introduction to Rust 2 / 101

The Concept of Ownership in Rust and Swift

CMSC 330: Organization of Programming Languages

CS558 Programming Languages

$ cat ~/.profile GIT_AUTHOR_NAME=Florian Gilcher TWITTER_HANDLE=argorak GITHUB_HANDLE=skade BLOG=skade.

Aaron Turon! Mozilla Research

MY GOOD FRIEND RUST. Matthias Endler trivago

Rust intro. (for Java Developers) JFokus #jfokus 1. 1

CS558 Programming Languages

Rust for high level applications. Lise Henry

A program execution is memory safe so long as memory access errors never occur:

Why you should take a look at

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Next Gen Networking Infrastructure With Rust

Sandcrust: Automatic Sandboxing of Unsafe Components in Rust

Memory Management and Efficient Graph Processing in Rust

Programming In Rust. Jim Blandy, / Portland, (Slides are as presented; followup discussion,

CS558 Programming Languages

1: /////////////// 2: // 1. Basics // 3: /////////////// 4: 5: // Functions. i32 is the type for 32-bit signed integers 6: fn add2(x: i32, y: i32) ->

C2Rust Migrating Legacy Code to Rust

PyCon Otto 6-9 April 2017 Florence. Rusty Python. Rust, a new language for our programmer tool belt. Matteo

RustPython. FOSDEM 2019 Brought to you by: Shing and Windel :P

The Rust Way of OS Development. Philipp Oppermann

When a colleague of mine first enthused to me about Rust, I

CPSC 3740 Programming Languages University of Lethbridge. Data Types

The Case for N-Body Simulations in Rust

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

CMSC 330: Organization of Programming Languages. Strings, Slices, Vectors, HashMaps in Rust

The Case for Building a Kernel

GE PROBLEM SOVING AND PYTHON PROGRAMMING. Question Bank UNIT 1 - ALGORITHMIC PROBLEM SOLVING

The Rust Programming Language

Next Gen Networking Infrastructure With Rust

6. Pointers, Structs, and Arrays. 1. Juli 2011

Nicholas Matsakis! Mozilla Research

COMP-520 GoLite Tutorial

Rust Advanced Topics

Region-based Memory Management. Advanced Operating Systems Lecture 10

CS558 Programming Languages. Winter 2013 Lecture 3

Let s Rust in Samba. Trying to use Samba with Rust libraries. Kai Blin Samba Team. SambaXP

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

JAVA An overview for C++ programmers

Introduction to the D programming language. Marc Fuentes - SED

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

Concepts of programming languages

RustBelt: Securing the Foundations of the Rust Programming Language

CMSC 330: Organization of Programming Languages. Functional Programming with OCaml

6. Pointers, Structs, and Arrays. March 14 & 15, 2011

Chapter 5. Names, Bindings, and Scopes

Seminar on Languages for Scientific Computing Aachen, 6 Feb Navid Abbaszadeh.

Programming Languages and Techniques (CIS120)

Rust. A safe, concurrent, practical language. Graydon Hoare October 2012

The Future of the Realtime Web BETTER APIS WITH GRAPHQL. Josh

JVM ByteCode Interpreter

Short Notes of CS201

Programming Languages Third Edition. Chapter 7 Basic Semantics

Scala : an LLVM-targeted Scala compiler

CMSC 330: Organization of Programming Languages. Rust Basics

CS201 - Introduction to Programming Glossary By

From Data to Effects Dependence Graphs: Source-to-Source Transformations for C

So what does studying PL buy me?

Unifer Documentation. Release V1.0. Matthew S

.consulting.solutions.partnership. Clojure by Example. A practical introduction to Clojure on the JVM

CS558 Programming Languages

Bash command shell language interpreter

Beginning. Rust From Novice to Professional. Carlo Milanesi

EMBEDDED RUST ON THE BEAGLEBOARD X15

GNU Radio with a Rusty FPGA. Brennan Ashton

Adding Lifetime Checking to Chapel Michael Ferguson, Cray Inc. CHIUW 2018 May 25, 2018

CMSC 330: Organization of Programming Languages. Memory Management and Garbage Collection

Chapter 9 :: Subroutines and Control Abstraction

Chapter 1 Getting Started

D Programming Language

CS558 Programming Languages Winter 2018 Lecture 4a. Andrew Tolmach Portland State University

Declarative concurrency. March 3, 2014

The Dynamic Typing Interlude

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

How Rust views tradeoffs. Steve Klabnik

CMSC 330: Organization of Programming Languages

Software Verification for Java 5

QUIZ. What is wrong with this code that uses default arguments?

Tokio: How we hit 88mph. Alex Crichton

Amit Levy SITP Retreat, June 22, Tock: A Secure Operating System for Microcontrollers

Go Forth and Code. Jonathan Gertig. CSC 415: Programing Languages. Dr. Lyle

The PCAT Programming Language Reference Manual

CS 241 Honors Memory

CS 11 Haskell track: lecture 1

Certified Core Java Developer VS-1036

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

CS 415 Midterm Exam Spring 2002

Lecture #16: Introduction to Runtime Organization. Last modified: Fri Mar 19 00:17: CS164: Lecture #16 1

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University

Transcription:

Rust: system programming with guarantees CRI Monthly Seminar Arnaud Spiwack Pierre Guillou MINES ParisTech, PSL Research University Fontainebleau, July 6th, 2015 1 / 29

High level programming languages A programming language is low level when its programs require attention to the irrelevant. Alan Perlis A language that doesn t affect the way you think about programming, is not worth knowing. also Alan Perlis 2 / 29

High level on the rise Mozilla C++ = Rust Apple Objective-C = Swift Microsoft C = F From Academia Scala (Twitter,... ) Ocaml (Facebook,... ) Haskell (Facebook,... ) 3 / 29

Rust in a nutshell Rust is Strongly typed Memory safe Garbage-collector free Higher order Precise wrt. memory Rust isn t Always easy For everything Finished 4 / 29

Ownership, linearity let x = &mut 42; let y = x; println!("value: {.", x); error: use of moved value: x 5 / 29

Ownership, linearity let x = &mut 42; let y = x; println!("value: {.", y); // was x Value: 42. 5 / 29

Ownership, linearity let x = &mut 42; let y = x.clone(); // was x println!("value: {.", x); // was y Value: 42. 5 / 29

Datatypes enum List<A> { Nil, Cons(A,List<A>) error: illegal recursive enum type; wrap the inner value in a box to make it representable 6 / 29

Datatypes enum List<A> { Nil, Cons(A,List<A>) // unboxed! error: illegal recursive enum type; wrap the inner value in a box to make it representable 6 / 29

Datatypes enum List<A> { Nil, Cons(A,Box<List<A>>) Works fine but cloning is linear time. 6 / 29

Reference counting enum List<A> { Nil, Cons(A,Rc<List<A>>) fn append<a:clone>(l1:rc<list<a>>,l2:rc<list<a>>) -> Rc<List<A>> { match *l1 { List::Cons(ref a,ref t) => Rc::new( List::Cons( a.clone(), append(t.clone(), l2) )), List::Nil => l2 7 / 29

Reference counting enum List<A> { Nil, Cons(A,Rc<List<A>>) fn append<a:clone>(l1:rc<list<a>>,l2:rc<list<a>>) -> Rc<List<A>> { // definition omitted let l1 = Rc::new((1..5).collect()); // build list using an iterator let l2 = Rc::new((5..10).collect()); println!("append: {", append(l1,l2)) append: [1,2,3,4,5,6,7,8,9] 8 / 29

Speaking about iterators let l = (1..10).collect(); println!("iterator: {", l) Iterator: [1,2,3,4,5,6,7,8,9] Some type annotations have been omitted 9 / 29

Speaking about iterators let nat = 0..; // the natural numbers let ten = nat.take(10); // take the 10 first let l = ten.collect(); println!("iterator: {", l) Iterator: [0,1,2,3,4,5,6,7,8,9] Some type annotations have been omitted 9 / 29

Speaking about iterators let nat = 0..; // the natural numbers let odd = nat.filter( n n%2==1); // only the odd ones let ten = odd.take(10); // take the 10 first let l = ten.collect(); println!("iterator: {", l) Iterator: [1,3,5,7,9,11,13,15,17,19] Some type annotations have been omitted 9 / 29

Speaking about iterators let nat = 0..; // the natural numbers let odd = nat.filter( n n%2==1); // only the odd ones let pairs = odd.flat_map( n // cartesian product vec!(true,false).into_iter().map(move p (n,p))); let ten = pairs.take(5); // take the 5 first let l = ten.collect(); println!("iterator: {", l) Iterator: [(1,true),(1,false),(3,true),(3,false),(5,true)] Some type annotations have been omitted 9 / 29

Back to lists enum List<A> { Nil, Cons(A,Box<List<A>>) Power of ownership Modify value = Make new value fn append<a>(l1:&mut List<A>,l2:List<A>) { match *l1 { List::Cons(_,ref mut t) => append(&mut *t,l2), List::Nil => *l1 = l2 10 / 29

Remark: non-aliasing let mut x = List::Nil; let y = &mut x; let z = & x; error: cannot borrow x as immutable because it is also borrowed as mutable 11 / 29

Non-aliasing continued 12 / 29

Non-aliasing continued 12 / 29

Non-aliasing continued DANGLING!! 12 / 29

Lifetimes let mut x = List::Nil; let y = &mut x; let z = & x; error: cannot borrow x as immutable because it is also borrowed as mutable Some type annotations have been omitted 13 / 29

Lifetimes let mut x = List::Nil; { let y = &mut x; let z = & x; ok Some type annotations have been omitted 13 / 29

Lifetimes let mut x = List::Nil; let z = &mut &x; { let y = List::Nil; let w = & y; *z = w; error: y does not live long enough Some type annotations have been omitted 13 / 29

Ownership passing enum List<A> { Nil, Cons(A,Box<List<A>>) fn append<a>(l1:&mut List<A>,l2:List<A>) { // definition omitted // This type works on the functional version as well fn append2<a>(l1:list<a>,l2:list<a>) -> List<A> { append(&mut l1,l2); l1 14 / 29

Abstraction: type scope impl<a> List<A> { /// invoked as List::foo(... ) fn foo (... ) ->... { // definition /// invoked as l.foo(... ) fn bar(&self,... ) ->... { // definition 15 / 29

Abstraction: traits /// Allows lists to be used as a target of the collect method impl<a> FromIterator<A> for List<A> { // required methods go here /// Allows lists to be used with format macros (such as println! ) impl<a:display> Display for List<A> { // required methods go here 16 / 29

Traits: remember enum List<A> { Nil, Cons(A,Rc<List<A>>) // Clone is a trait fn append<a:clone>(l1:rc<list<a>>,l2:rc<list<a>>) -> Rc<List<A>> { match *l1 { List::Cons(ref a,ref t) => Rc::new( List::Cons( a.clone(), append(t.clone(), l2) )), List::Nil => l2 17 / 29

Recap Rust has ownership, borrowing, lifetimes unboxed datatypes, pattern-matching traits iterators And so much more array slices, unboxed functional values statically sized data in stack vs dynamically sized data in heap lifetime-parametric data & functions trait objects typed exceptions (sort of) macros, iterator-based for loop safer concurrency And, also, a vibrant ecosystem compilation, standard library documentation, test some projects written in Rust 18 / 29

Modules and Crates Crate compilation unit library or binary link: extern crate other_crate; Module named scope a crate = several modules visibility: pub keyword a tree structure from the top-level source directory use keyword to import into the local scope and defining public interfaces no need to maintain separate.h/.mli files: // declare external interface // independantly of internal code organization pub mod my_interface { // import all my_internal_module functions // into current module pub use my_internal_module; 19 / 29

Compiling Rust programs The Rust compiler rustc hardware targets: (x86, ARM) (Linux, OSX, Windows, *BSD) built on top of LLVM written in Rust (previously in Ocaml) compiling rather slow, optimization on their way Conditional compilation code annotations #[cfg(feature)], macros cfg!(feature) compiler flags: --cfg feature 20 / 29

Linking to other libraries Linking to a Rust crate extern crate declaration to import public items $ rustc -L path -l lib to add library Linking to a C library #[link(name="m")] extern { // signatures fn pow(a: f64, b: f64) -> f64; pub fn rust_pow(a: f64, b: f64) -> f64 { unsafe { pow(a, b) Calling Rust from C (or Python, C++,... ) #[no_mangle] and extern fn for functions #[repr(c)] for structs 21 / 29

Rust standard libraries The std crate 55 kloc (glibc: 1.8 MLoC) automatically linked at compile time except with #[no_std] bare metal basic data structures: strings, vectors, hashmaps, error handling I/O (files, fs, path), concurrency (threads, channels, processes, simd) The core crate 25 kloc minimal and portable replacement for the std crate no heap allocation, no concurrency, no I/O The libc crate wrapper around LibC functions (glibc on Linux) 22 / 29

Documentation, test Documentation Test can be written in source code using /// delimiters Markdown syntax $ rustdoc to generate HTML code examples in documentation can be compiled and executed as tests #[test] annotation conditionnaly compiled with $ rustc --test assert! macros for testing conditions anywhere in source code, even in documentation also support performance tests with #[bench] #[test] fn test_rust_pow() { assert_eq!(rust_pow(2.0, 3.0), 8.0); 23 / 29

Cargo, a project manager for Rust $ cargo new project_name [--bin] create a folder hierarchy: project_name/{src, target initialize git/hg version control repository $ cargo run: compile and run in one command $ cargo [test bench]: compile and run tests/benchmarks $ cargo doc: generate documentation Cargo.toml: semantic versionning, metadata, dependencies... http://crates.io: package repository 24 / 29

Projects written in Rust the Rust compiler and the standard library currently 85 kloc in Rust 6-weeks iteration process (like Firefox, Chrome) on GitHub cargo, the Rust project manager 15 kloc in Rust 2500 available crates on http://crates.io Servo, a parallel web browser layout engine research project aims at replacing the Gecko engine already passes the Acid2 rendering test currently 150 kloc in Rust 25 / 29

Servoception servo -o wikiservo.png --resolution 720x540 https://en.wikipedia.org/wiki/servo_\(layout\_engine\) 26 / 29

On-line resources main page: http://rust-lang.org guides: Rust Book, Rust by Example Reddit, Stack Overflow, GitHub, IRC, Twitter,... linkable online interpreter: Rust playground 27 / 29

Conclusion Rust innovative programming language high-level features fine-grained memory model hardware performance efficient tools for developers interesting projects thriving community 28 / 29

Rust: system programming with guarantees CRI Monthly Seminar Arnaud Spiwack Pierre Guillou MINES ParisTech, PSL Research University Fontainebleau, July 6th, 2015 29 / 29