CMSC 330: Organization of Programming Languages

Similar documents
CMSC 330: Organization of Programming Languages. OCaml Data Types

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages. Rust Basics

Rust Advanced Topics

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

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

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

Introduction to Rust. CC-BY Mozilla. Jonathan Pallant

CMSC 330: Organization of Programming Languages. OCaml Expressions and Functions

CMSC 330: Organization of Programming Languages

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

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

Rust for C++ Programmers

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

CMSC 330: Organization of Programming Languages. Lets, Tuples, Records

Exercises on ML. Programming Languages. Chanseok Oh

Fundamentals of Programming (Python) Object-Oriented Programming. Ali Taheri Sharif University of Technology Spring 2018

CMSC 430 Introduction to Compilers. Fall Everything (else) you always wanted to know about OCaml (but were afraid to ask)

Introduction to OCaml

OCaml. ML Flow. Complex types: Lists. Complex types: Lists. The PL for the discerning hacker. All elements must have same type.

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

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

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

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

A quick introduction to SML

Inductive Data Types

Introduction to Rust 2 / 101

Idiomatic Rust. Writing concise and elegant Rust code

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

Begin at the beginning

Common Programming. Variables and Mutability

OCaml Data CMSC 330: Organization of Programming Languages. User Defined Types. Variation: Shapes in Java

Mini-ML. CS 502 Lecture 2 8/28/08

CSE 130 [Winter 2014] Programming Languages

Programovací jazyky F# a OCaml. Chapter 3. Composing primitive types into data

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

Programming Languages

Guaranteeing memory safety in Rust

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

Recap from last time. Programming Languages. CSE 130 : Fall Lecture 3: Data Types. Put it together: a filter function

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

Programming Languages

This quiz is open book and open notes, but do not use a computer.

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Recap: Functions as first-class values

Recap: ML s Holy Trinity. Story So Far... CSE 130 Programming Languages. Datatypes. A function is a value! Next: functions, but remember.

Why OCaml? Introduction to Objective Caml. Features of OCaml. Applications written in OCaml. Stefan Wehr

CSE 130 Programming Languages. Datatypes. Ranjit Jhala UC San Diego

Introduction to Objective Caml

Dialects of ML. CMSC 330: Organization of Programming Languages. Dialects of ML (cont.) Features of ML. Functional Languages. Features of ML (cont.

Advances in Programming Languages

CS61A Lecture 16. Amir Kamil UC Berkeley February 27, 2013

Parametric types. map: ( a (a b) a list b list. filter: ( a bool) a list a list. Why? a and b are type variables!

CSCI 2041: Functions, Mutation, and Arrays

A Third Look At ML. Chapter Nine Modern Programming Languages, 2nd ed. 1

MUTABLE LISTS AND DICTIONARIES 4

Objective-C. Stanford CS193p Fall 2013

News. Programming Languages. Complex types: Lists. Recap: ML s Holy Trinity. CSE 130: Spring 2012

Recap: ML s Holy Trinity

Recap. Recap. If-then-else expressions. If-then-else expressions. If-then-else expressions. If-then-else expressions

Getting started with Java

CSC324 Principles of Programming Languages

Sharing, mutability, and immutability. Ruth Anderson UW CSE 160 Spring 2018

Typed Racket: Racket with Static Types

Advanced Systems Programming

CMSC 330: Organization of Programming Languages. Basic OCaml Modules

Next Gen Networking Infrastructure With Rust

An introduction introduction to functional functional programming programming using usin Haskell

CS61A Lecture 16. Amir Kamil UC Berkeley February 27, 2013

Programming Languages and Techniques (CIS120)

CSCI 2041: Basic OCaml Syntax and Features

CSCI-GA Scripting Languages

Side note: Tail Recursion. Begin at the beginning. Side note: Tail Recursion. Base Types. Base Type: int. Base Type: int

Pointers, Dynamic Data, and Reference Types

Nicholas Matsakis! Mozilla Research

Tail Recursion: Factorial. Begin at the beginning. How does it execute? Tail recursion. Tail recursive factorial. Tail recursive factorial

Haskell Types, Classes, and Functions, Currying, and Polymorphism

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

Background. CMSC 330: Organization of Programming Languages. Useful Information on OCaml language. Dialects of ML. ML (Meta Language) Standard ML

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

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1

Variables and Bindings

Plan (next 4 weeks) 1. Fast forward. 2. Rewind. 3. Slow motion. Rapid introduction to what s in OCaml. Go over the pieces individually

CSE 130 Programming Languages. Lecture 3: Datatypes. Ranjit Jhala UC San Diego

Variables One More (but not the last) Time with feeling

CS Lecture 5: Pattern Matching. Prof. Clarkson Fall Today s music: Puff, the Magic Dragon by Peter, Paul & Mary

DaMPL. Language Reference Manual. Henrique Grando

CSE341: Programming Languages Lecture 11 Type Inference. Dan Grossman Spring 2016

D7020E. The dynamic evolution of a system Robust and Energy-Efficient Real-Time Systems. blocked Lecture 2: Components & the Timber language.

D7020E. Robust and Energy-Efficient Real-Time Systems D7020E. Lecture 2: Components & the Timber language

CIS 120 Midterm II November 8, Name (printed): Pennkey (login id):

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

Question 2. [5 points] Given the following symbolic constant definition

Next Gen Networking Infrastructure With Rust

CMSC330 Fall 2016 Midterm #1 2:00pm/3:30pm

INF121: Functional Algorithmic and Programming

Programming Languages

CMSC 330: Organization of Programming Languages

Transcription:

CMSC 330: Organization of Programming Languages Structs and Enums in Rust CMSC330 Spring 2018 Copyright 2018 Niki Vazou, the University of Maryland. Some material based on https://doc.rustlang.org/book/second-edition/index.html 1

Rust Data So far, we ve seen the following kinds of data Scalar types (int, float, char, string, bool) Tuples, Arrays, and Collections How can we build other data structures? Structs (like Objects; support for methods) Enums (like Ocaml Data Types) 2

Structs: Definitions & Construction struct Rectangle { width: u32, height: u32, Field with int type fn main() { // construction let rect1 = Rectangle { width: 30, height: 50 ; // accessing fields println!("rect1 s width is {", rect1.width); > rect1 s width is 30 Construction Field accessing 3

Structs: Printing struct Rectangle{ width:u32, height:u32, fn main() { letrect1 = Rectangle { width:30, height:50; println!("rect1 is {", rect1); error[e0277]: the trait bound `Rectangle: std::fmt::display` is not satisfied 4

Structs: Printing with Derived Traits #[derive(debug)] struct Rectangle{ width:u32, height:u32, Derive printing format Use printing format fn main() { letrect1 = Rectangle { width:30, height:50; println!("rect1 is {:?", rect1); > rect1 is Rectangle { width: 30, height: 50 5

Structs Syntax struct T [<T>] {n1:t1,, ni:ti, the ni are called fields, begin with a lowercase letter [<T>] optionally for generics (see later) Evaluation Construction: T {n1:v1, ni:vi is a value if vi are values. Destruction: t.ni returns the ni field of t Type Checking T {n1:v1, ni:vi : T [if vi has type ti] 6

Methods: Definitions on Structs impl Rectangle { fn area(&self) -> u32 { self.width * self.height Self argument of type Rectangle impl Rectangle defines an implementation block - self arg has type Rectangle (reference) - same ownership rules: - &self for borrowing, - self to take ownership, - &mut self to allow mutation 7

Methods: Calls fn main() { let rect1 = Rectangle { width:30, height:50; println!("the area is { pixels.", rect1.area()); dot syntax to call methods If method had arguments, use function call e.g., rect1.area(3) 8

Methods: Many Arguments impl Rectangle { fn can_hold(&self, other:&rectangle) -> bool { self.width > other.width && self.height > other.height fn square(size:u32) -> Rectangle { Rectangle { width: size, height: size square is called an Associated Method - no self argument - operates on Rectangles - called with let sq = Rectangle::square(3); 9

Generic Lifetimes struct ImportantExcerpt< a> { part: & a str, fn main() { let novel =String::from("Generic Lifetime"); let i = ImportantExcerpt { part: &novel; When structs hold references, we need to add a lifetime annotation on every reference in the struct s definition. 10

Lifetimes in Methods struct ImportantExcerpt< a> { part: & a str, impl< a> ImportantExcerpt< a> { fn level(&self) -> i32 { 3 Implementation needs lifetime annotation. Lifetime is inferred in function (using elision [*] ). [*] https://doc.rust-lang.org/book/second-edition/ch10-03-lifetime-syntax.html#lifetimeelision 11

Quiz 1: point is immutable at HERE struct Point { x: i32, y: i32, let mut point = Point { x: 0, y: 0 ; point.x = 5; let point = point; // HERE A. True B. False 12

Quiz 1: point is immutable at HERE struct Point { x: i32, y: i32, let mut point = Point { x: 0, y: 0 ; point.x = 5; let point = point; // HERE A. True. B. False Mutability is a property of the binding; the old point s contents are copied to the new one 13

Enums enum IpAddr{ V4(String), V6(String), definition construction let home = IpAddr::V4(String::from("127.0.0.1")); let loopback = IpAddr::V6(String::from("::1")); Like Variants in Ocaml type IpAddr = V4 of string V6 of string ;; let home = V4 "127.0.0.1 ;; let loopback = V6 "1";; 14

Enums with Blocks enum IpAddr{ V4(String), V6(String), impl IpAddr { fn call(&self) { // method body would be defined here let m = IpAddr ::V6(String::from("::1")); m.call(); 15

Enums with Structs Like in OCaml, enums might contain any type, e.g., structs, references, struct Ipv4Addr{ // details elided struct Ipv6Addr{ // details elided enumipaddr{ V4(Ipv4Addr), V6(Ipv6Addr), 16

The Option Enum: Generic Types enum Option<T> { Some(T), None, let some_number = Some(5); let some_string = Some("a string"); let absent_number:option<&rectangle> = None; Defined in standard lib Instantiation with any type! type a Option = Some of a None ;; Compare with OCaml let some_number = Some 5 ;; let some_string = Some "a string ;; val absent_number :: int option ;; let absent_number = None;; 17

Generics in Structs & Methods Generic T in struct struct Point<T> { x: T, y: T, Instantiate T as i32 fn main() { let p = Point { x:5, y:10; println!("p.x = {", p.x()); Generic T in methods impl<t> Point<T> { fn x(&self) -> &T { &self.x 18

Matching fn plus_one(x:option<i32>) -> Option<i32> { match x { Some(i) => Some(i +1), None => None, 19

Matching should be exhaustive! fn plus_one(x:option<i32>) -> Option<i32> { match x { Some(i) => Some(i +1), Error at compile time! error[e0004]: non-exhaustive patterns: `None` not covered 20

Enums Syntax enum T [<T>] {C1 [(t1)],, Cn [(tn)], the Ci are called constructors Must begin with a capital letter; may include associated data notated with brackets [] to indicate it s optional Evaluation A constructor Ci is a value if it has no assoc. data Ci(vi) is a value if it does Destructing a value of type t is by pattern matching patterns are constructors Ci with data components, if any Type Checking Ci [(vi)] : T [if vi has type ti] 21

Quiz 2: Output of following code enum Number { Zero, One, Two, use Number::Zero; let t = Number::One; match t { Zero=> println!("0"), Number::One => println!("1"), A. 0 B. 1 C. Compile Error 22

Quiz 2: Output of following code enum Number { Zero, One, Two, use Number::Zero; let t = Number::One; match t { Zero=> println!("0"), Number::One => println!("1"), A. 0 B. 1 C. Compile Error. Pattern `Two` not covered 23

If-let, for non exhaustive matches fn check(x: Option<i32>) { if let Some(42) = x { println!("success!") // only executed if the match succeds else { println!("failure!") fn main (){ check(some(3));; // prints "Failure!" check(some(42));; // prints "Success!" check(none);; // prints "Failure!" 24

Recap: Structs and Enums 1. Structs define data structures with fields And implementation blocks collect methods on to specify the behavior of structs (like objects) 2. Enums define a set of possible data types Like OCaml variant types Use match or if-let to deconstruct 25