SlideShare a Scribd company logo
1 of 57
Class 2:
First Ride on Rust
cs4414 Fall 2013
University of Virginia
David Evans
Menu
• Recap last week, syllabus questions
• Overview of Rust (and why we are using it)
• Brief history of Computing Systems
• Some Rust constructs (for PS1)
112 September 2013 University of Virginia cs4414
Why learn a new
programming language?
12 September 2013 University of Virginia cs4414 2
J S Bach, “Coffee
Cantata”, BWV 211 (1732)
www.npj.com/homepage/teritowe/jsbhand.
html
“Jamais Jamais Jamais” from
Harmonice Musices Odhecaton
A. (1501)
Modern Music Notation
Roman Haubenstock-
Ramati, Concerto a Tre
John Cage, Fontana Mix
http://www.medienkunstnetz.de/works/fontana-mix/audio/1/
Thought and Action
Languages change the way we think
BASIC: think about GOTO
Algol, Python: think about assignments, control blocks
Scheme, Haskell: think about procedures
Java, C++: think about types, objects
Languages provide abstractions of machine resources
– Hide dangerous/confusing details: memory
locations, instruction opcodes, number
representations, calling conventions, etc.
– Hiding more increases simplicity, but limits expressiveness
What do we want for systems programming?
Why so many programming
languages?
Fundamental Differences
• All equivalently powerful!
– Universal languages: all capable of simulating each other
• Fundamental differences
– Expressiveness: how easy it is to describe a computation
– “Truthiness”: likelihood that a program means what a
programmer things it means
– Safeness: impact of programmer mistakes
• There is a fundamental conflict between
expressiveness and truthiness/safeness
Programming Language Design Space
Expressiveness
“Truthiness”
Scheme
Python
Java
C++
C
low
high
Spec#
Ada
strict typing,
static
BASIC
more mistake prone less mistake prone
print ("Hello!")
(display “Hello!”)
“a safe, concurrent, practical language”
12 September 2013 University of Virginia cs4414 10
Its design is oriented toward
concerns of “programming in
the large”, that is, of creating
and maintaining boundaries –
both abstract and operational
– that preserve large-system
integrity, availability and
concurrency.
from http://www.rust-lang.org/ Rust
12 September 2013 University of Virginia cs4414 11
http://www.rust-lang.org/
12 September 2013 University of Virginia cs4414 12
fn max(a: int, b: int) -> int {
if a > b {
a
} else {
b
}
}
fn main() {
println(fmt!("Max: %?", max(3, 4)));
}
Expressions and Statements
Java/C++
IfStatement ::=
if (Expression)
StatementTrue
[ else
StatementFalse
]
Rust
IfExpression ::=
if Expression Block
[ else Block ]
Block ::= { [Statement* Expr] }
Expression ::= Block
Statement ::= Expression ;
12 September 2013 University of Virginia cs4414 13
Simplified: see http://static.rust-lang.org/doc/0.7/rust.html#if-expressions for full
grammar.
Warning: “(looking for consistency in the manual's grammar is bad: it's entirely wrong in
many places.)”
Quiz
12 September 2013 University of Virginia cs4414 14
IfExpression ::=
if Expression Block
[ else Block ]
Block ::= { [Statement* Expr] }
Expression ::= Block
fn max(a: int, b: int) -> int {
if { } { a } else { b }
}
a) Syntax Error
b) Type Error
c) Run-time Error
Quiz
12 September 2013 University of Virginia cs4414 15
IfExpression ::=
if Expression Block
[ else Block ]
Block ::= { [Statement* Expr] }
Expression ::= Block
fn max(a: int, b: int) -> int {
if { } { a } else { b }
}
$ rustc block.rs
block.rs:2:7: 2:10 error: mismatched types: expected `bool` but found `()`
(expected bool but found ())
block.rs:2 if { } {
^~~
(Trick) Quiz
12 September 2013 University of Virginia cs4414 16
IfExpression ::=
if Expression Block
[ else Block ]
Block ::= { [Statement* Expr] }
Expression ::= Block
fn max(a: int, b: int) -> int {
if { let x = 4414; x = x + a; x > b + 4414 } { a }
else { b }
} a) Syntax Error
b) Type Error
c) Run-time Error
(Trick) Quiz
12 September 2013 University of Virginia cs4414 17
IfExpression ::=
if Expression Block
[ else Block ]
Block ::= { [Statement* Expr] }
Expression ::= Block
fn max(a: int, b: int) -> int {
if { let x = 4414; x = x + a; x > b + 4414 } { a }
else { b }
}
$ rustc block.rs
block.rs:2:23: 2:24 error: re-assignment of immutable variable `x`
block.rs:2 if { let x = 4414; x = x + a; x > b + 4414 } {
^
“Variables” are invariable…unless declared with mut.
Quiz
12 September 2013 University of Virginia cs4414 18
IfExpression ::=
if Expression Block
[ else Block ]
Block ::= { [Statement* Expr] }
Expression ::= Block
fn max(a: int, b: int) -> int {
if { let mut x = 4414; x = x + a; x > b + 4414 } { a }
else { b }
}
$ rust run block.rs
Max: 5
Quiz
12 September 2013 University of Virginia cs4414 19
IfExpression ::=
if Expression Block
[ else Block ]
Block ::= { [Statement* Expr] }
Expression ::= Block
Statement ::= Expression ;
fn max(a: int, b: int) -> int {
if a > b { a } else { b; }
}
fn main() {
println(fmt!("Max: %?", max(5, 4)));
}
Quiz
12 September 2013 University of Virginia cs4414 20
fn max(a: int, b: int) -> int {
if a > b { a } else { b; }
}
fn main() {
println(fmt!("Max: %?", max(5, 4)));
}
block.rs:2:24: 2:30 error: mismatched types: expected `int` but found `()`
(expected int but found ())
block.rs:2 if a > b { a } else { b; }
^~~~~~
The semi-colon makes it a statement – no value
Higher-Order Functions
12 September 2013 University of Virginia cs4414 21
Java Rust
Java 8 (March 2014)
Scheme
(define (make-adder a)
(lambda (n)
(+ a n)))
| <parameters> | Block
12 September 2013 University of Virginia cs4414 22
Define a function, make_adder(int), that takes
an integer as input and returns a function that
takes and int and returns the sum of the original
and input int.
make_adder(3)(1) => 4
let increment = make_adder(1);
12 September 2013 University of Virginia cs4414 23
fn make_adder(a : int) -> ~fn(int) -> int {
|b| { a + b}
}
12 September 2013 University of Virginia cs4414 24
Define a function, ntimes, that takes as inputs a
function f (int -> int) and an integer n, and
returns a function that applies f n-times.
fn double(a: int) -> int { a * 2 }
fn main() {
let quadruple = ntimes(double, 2);
println(fmt!("quad: %?", quadruple(2)));
}
12 September 2013 University of Virginia cs4414 25
fn ntimes(f: extern fn(int) -> int, times: int) -> ~fn(int) -> int {
|x| { if times == 0 { x } else { ntimes(f, times - 1)(f(x)) } }
}
Reading a File
12 September 2013 University of Virginia cs4414 26
http://static.rust-lang.org/doc/0.7/std/io.html#function-file_reader
This isn’t an excerpt! This is all the documentation
there is in the Rust manual.
Arrgh! What to do?
12 September 2013 University of Virginia cs4414 27
Image: credit unknown
12 September 2013 University of Virginia cs4414 28
“Baby”
programmer
(cs1xxx)
12 September 2013 University of Virginia cs4414 29
Give up in
disgust!
cs4414 Student
“Professional
Amateur
Programmer”
12 September 2013 University of Virginia cs4414 30
Solving Programming Mysteries
12 September 2013 University of Virginia cs4414 31
http://www.smellsfunny.com/view.php?fid=17557
Solving Programming Mysteries
1. DuckDuckGo (or Google) is your friend!
12 September 2013 University of Virginia cs4414 32
12 September 2013 University of Virginia cs4414 33
Caveats:
HinderRust will probably not help at all
Rust 0.7 is quite different from Rust 0.3
Easy to get lost in lots of details
Top-ranked result is not always the best solution
Dangerous to “cut-and-paste” code you don’t understand!
Solving Programming Mysteries
1. DuckDuckGo (or Google) is your friend!
2. stackoverflow [rust]
12 September 2013 University of Virginia cs4414 34
12 September 2013 University of Virginia cs4414 35
Questions tagged with rust: 116
Questions tagged with java: 468,140
Solving Programming Mysteries
1. DuckDuckGo (or Google) is your friend!
2. stackoverflow [rust]
3. Experiment!
12 September 2013 University of Virginia cs4414 36
12 September 2013 University of Virginia cs4414 37
fn main() {
let filereader = std::io::file_reader(~std::path::Path("test.txt"));
}
$ rustc experiment.rs
experiment.rs:6:8: 6:18 warning: unused variable: `filereader` [-W unused-variable
(default)]
experiment.rs:6 let filereader = std::io::file_reader(~std::path::Path("test.txt"));
^~~~~~~~~~
fn main() {
let filereader = std::io::file_reader(~std::path::Path("test.txt"));
println(fmt!("Reader: %?", filereader));
}
$ rust run experiment.rs
Reader: Err(~"error opening test.txt")
$ echo ‘hello’ > test.txt
$ rust run experiment.rs
warning: no debug symbols in executable (-arch x86_64)
Reader: Ok()
Solving Programming Mysteries
1. DuckDuckGo (or Google) is your friend!
2. stackoverflow [rust]
3. Experiment!
4. Ask for help:
– Course Piazza forum
– IRC
12 September 2013 University of Virginia cs4414 38
If you figure something useful out that is not well
documented, document it! course forum post, “blog” post
Back to Reading Files
12 September 2013 University of Virginia cs4414 39
Result<T, U> : enumerated type:
Ok(T)
Err(U)
Result<@Reader, ~str>
match expression
12 September 2013 University of Virginia cs4414 40
fn load_file(pathname : ~str) -> ~[~str] {
let filereader : Result<@Reader, ~str> =
io::file_reader(~path::Path(pathname));
match filereader {
Ok(reader) => reader.read_lines(),
Err(msg) => fail!("Cannot open file: " + msg),
}
}
How does this compare to the “Java” way?
match
12 September 2013 University of Virginia cs4414 41
match filereader {
Ok(reader) => reader.read_lines(),
}
compile-time completeness
cat.rs:9:4: 11:5 error: non-exhaustive patterns: Err not covered
current =
match current {
current if current % 2 == 0 => current / 2,
_ => 3 * current + 1
};
match predicates can be
arbitrary expressions
_ (like default in C
switch)
Why NOT to use Rust
12 September 2013 University of Virginia cs4414 42
12 September 2013 University of Virginia cs4414 43
https://mail.mozilla.org/pipermail/rust-dev/2013-August/005409.html
12 September 2013 University of Virginia cs4414 44
https://github.com/graydon/rust/wiki
This is a very preliminary work in
progress. No supported releases yet
nor defined release schedule /
plans. Caveat emptor. It will crash. It
will change syntax and semantics. It
will eat your laundry. Use at your
own risk. Etc.
Originally posted: 15 Dec 2011 (but still there today)
Rust or Bust?
Rust
 Immature, Unstable
Poorly documented
Few open source
projects (except Servo)
Not in high employer
demand
No other courses
Bust (C)
Mature, Stable
Hundreds of books, etc.
Lots of open source
projects (incl. Linux)
Popular for interview
questions
Nearly all OS courses
12 September 2013 University of Virginia cs4414 45
Rust or Bust?
Rust
Benefits from past 30
years of language and
compiler research
Bust (C)
Suffers from
maintaining backwards
compatibility with
language stripped down
to fit in ~9 KB
12 September 2013 University of Virginia cs4414 46
12 September 2013 University of Virginia cs4414 47
PDP-11
Ken Thompson
Dennis Ritchie PDP-11
~4KB of memory
12 September 2013 University of Virginia cs4414 48
“I call it my billion-
dollar mistake.”(Sir
Tony Hoare)
int main(int argc, char **argv) {
char *s = (char *) malloc (MY_SIZE);
*s = 'a';
return 0;
}
Photo: Bertrand Meyer
$ gcc -Wall null.c
$ ./a.out
Segmentation fault (core dumped)
Note: Sir Tony invented null references in 1964 (before C), and said this in 2009
(back when an American billion dollars was still real money). See a great talk here:
http://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare
He designed compiler to check every reference is safe (but null and bounds checking), but C
removed them as too expensive.
Rust or Bust?
Rust
 Benefits from past 30
years of language and
compiler research
 Chance to influence a
new, exciting, fast-
evolving language
 Some really cool features
for memory
management and
concurrency
 FUN!
Bust (C)
 Suffers from maintaining
backwards compatibility
 C++0x standard process
began in 1998, released in
2011, over 40 meetings
 Lots of complexity, but
not designed for safety
 Boring, Annoying
12 September 2013 University of Virginia cs4414 49
Diving In: zhttpo.rs
12 September 2013 University of Virginia cs4414 50
// zhttpto.rs
// ...
extern mod extra;
use extra::uv;
use extra::{net_ip, net_tcp};
use std::str;
static BACKLOG: uint = 5;
static PORT: uint = 4414;
static IPV4_LOOPBACK: &'static str = "127.0.0.1";
fn new_connection_callback(new_conn :net_tcp::TcpNewConnection, _killch:
std::comm::SharedChan<Option<extra::net_tcp::TcpErrData>>)
{
...
Comments similar to C++
Dependency on an external “crate” (compilation unit)
Import into namespace:
uv = “extra::uv”
global “variables” (no mut, so
they are constants)
main
12 September 2013 University of Virginia cs4414 51
fn main() {
net_tcp::listen(net_ip::v4::parse_addr(IPV4_LOOPBACK), PORT, BACKLOG,
&uv::global_loop::get(),
|_chan| { println(fmt!("Listening on tcp port %u ...", PORT)); },
new_connection_callback);
}
12 September 2013 University of Virginia cs4414 52
http://static.rust-lang.org/doc/std/net_tcp.html#function-listen
12 September 2013 University of Virginia cs4414 53
fn main() {
net_tcp::listen(net_ip::v4::parse_addr(IPV4_LOOPBACK), PORT, BACKLOG,
&uv::global_loop::get(),
|_chan| { println(fmt!("Listening on tcp port %u ...", PORT)); },
new_connection_callback);
}
on_establish_cb: ~fn(SharedChan<Option<TcpErrData>>)
a callback that is evaluated if/when the listener is successfully
established. it takes no parameters
new_connect_cb - a callback to be evaluated, on the libuv thread, whenever a client
attempts to connect on the provided ip/port. the callback’s arguments are: …
12 September 2013 University of Virginia cs4414 54
fn new_connection_callback(new_conn : net_tcp::TcpNewConnection,
_killch : std::comm::SharedChan<…>)
{
do spawn {
let accept_result = extra::net_tcp::accept(new_conn);
match accept_result {
Err(err) => { println(fmt!("Connection error: %?", err)); },
Ok(sock) => {
let peer_addr: ~str = net_ip::format_addr(&sock.get_peer_addr());
println(fmt!("Received connection from: %s", peer_addr));
let read_result = net_tcp::read(&sock, 0u);
match read_result {
Err(err) => { println(fmt!("Receive error: %?", err)); },
Ok(bytes) => {
let request_str = str::from_bytes(bytes.slice(0, bytes.len() - 1));
println(fmt!("Request received:n%s", request_str));
let response: ~str = ~
"HTTP/1.1 …”;
net_tcp::write(&sock, response.as_bytes_with_null_consume());
},
};
} } }; }
Charge
• You should be making progress on PS1 now:
you know everything you need to finish it
– Take advantage of Piazza forum and IRC for any
strange Rustiness you encounter
• Next class: processes!
12 September 2013 University of Virginia cs4414 55
Ways to Learn New Languages
• Read the compiler source code
• Read the reference manual
• Go through a (well designed!) tutorial
• Read a good book with lots of exercises
• Dive right into a complex program and try to
modify it
• Find a mentor who is a language expert
12 September 2013 University of Virginia cs4414 56

More Related Content

Similar to First Ride on Rust

talk at Virginia Bioinformatics Institute, December 5, 2013
talk at Virginia Bioinformatics Institute, December 5, 2013talk at Virginia Bioinformatics Institute, December 5, 2013
talk at Virginia Bioinformatics Institute, December 5, 2013ericupnorth
 
Using the Power to Prove
Using the Power to ProveUsing the Power to Prove
Using the Power to ProveKazuho Oku
 
Making a Process
Making a ProcessMaking a Process
Making a ProcessDavid Evans
 
Language Technology Enhanced Learning
Language Technology Enhanced LearningLanguage Technology Enhanced Learning
Language Technology Enhanced Learningtelss09
 
Programming with Millions of Examples (HRL)
Programming with Millions of Examples (HRL)Programming with Millions of Examples (HRL)
Programming with Millions of Examples (HRL)Eran Yahav
 
Introduction to Scala for JCConf Taiwan
Introduction to Scala for JCConf TaiwanIntroduction to Scala for JCConf Taiwan
Introduction to Scala for JCConf TaiwanJimin Hsieh
 
Functional Programming & Event Sourcing - a pair made in heaven
Functional Programming & Event Sourcing - a pair made in heavenFunctional Programming & Event Sourcing - a pair made in heaven
Functional Programming & Event Sourcing - a pair made in heavenPawel Szulc
 
Event Sourcing and Functional Programming
Event Sourcing and Functional ProgrammingEvent Sourcing and Functional Programming
Event Sourcing and Functional ProgrammingGlobalLogic Ukraine
 
Genomic Analysis in Scala
Genomic Analysis in ScalaGenomic Analysis in Scala
Genomic Analysis in ScalaRyan Williams
 
Introduction to R
Introduction to RIntroduction to R
Introduction to Ragnonchik
 
Beauty and Power of Go
Beauty and Power of GoBeauty and Power of Go
Beauty and Power of GoFrank Müller
 
Kotlin Advanced - Apalon Kotlin Sprint Part 3
Kotlin Advanced - Apalon Kotlin Sprint Part 3Kotlin Advanced - Apalon Kotlin Sprint Part 3
Kotlin Advanced - Apalon Kotlin Sprint Part 3Kirill Rozov
 
Putting a Fork in Fork (Linux Process and Memory Management)
Putting a Fork in Fork (Linux Process and Memory Management)Putting a Fork in Fork (Linux Process and Memory Management)
Putting a Fork in Fork (Linux Process and Memory Management)David Evans
 

Similar to First Ride on Rust (20)

talk at Virginia Bioinformatics Institute, December 5, 2013
talk at Virginia Bioinformatics Institute, December 5, 2013talk at Virginia Bioinformatics Institute, December 5, 2013
talk at Virginia Bioinformatics Institute, December 5, 2013
 
Using the Power to Prove
Using the Power to ProveUsing the Power to Prove
Using the Power to Prove
 
Making a Process
Making a ProcessMaking a Process
Making a Process
 
Language Technology Enhanced Learning
Language Technology Enhanced LearningLanguage Technology Enhanced Learning
Language Technology Enhanced Learning
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
Programming with Millions of Examples (HRL)
Programming with Millions of Examples (HRL)Programming with Millions of Examples (HRL)
Programming with Millions of Examples (HRL)
 
Introduction to Scala for JCConf Taiwan
Introduction to Scala for JCConf TaiwanIntroduction to Scala for JCConf Taiwan
Introduction to Scala for JCConf Taiwan
 
Clojure night
Clojure nightClojure night
Clojure night
 
Functional Programming & Event Sourcing - a pair made in heaven
Functional Programming & Event Sourcing - a pair made in heavenFunctional Programming & Event Sourcing - a pair made in heaven
Functional Programming & Event Sourcing - a pair made in heaven
 
Event Sourcing and Functional Programming
Event Sourcing and Functional ProgrammingEvent Sourcing and Functional Programming
Event Sourcing and Functional Programming
 
6-TDD
6-TDD6-TDD
6-TDD
 
Invitation to Scala
Invitation to ScalaInvitation to Scala
Invitation to Scala
 
Programação Funcional
Programação FuncionalProgramação Funcional
Programação Funcional
 
Genomic Analysis in Scala
Genomic Analysis in ScalaGenomic Analysis in Scala
Genomic Analysis in Scala
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
 
Beauty and Power of Go
Beauty and Power of GoBeauty and Power of Go
Beauty and Power of Go
 
Scala - brief intro
Scala - brief introScala - brief intro
Scala - brief intro
 
Kotlin Advanced - Apalon Kotlin Sprint Part 3
Kotlin Advanced - Apalon Kotlin Sprint Part 3Kotlin Advanced - Apalon Kotlin Sprint Part 3
Kotlin Advanced - Apalon Kotlin Sprint Part 3
 
Managing Memory
Managing MemoryManaging Memory
Managing Memory
 
Putting a Fork in Fork (Linux Process and Memory Management)
Putting a Fork in Fork (Linux Process and Memory Management)Putting a Fork in Fork (Linux Process and Memory Management)
Putting a Fork in Fork (Linux Process and Memory Management)
 

More from David Evans

Cryptocurrency Jeopardy!
Cryptocurrency Jeopardy!Cryptocurrency Jeopardy!
Cryptocurrency Jeopardy!David Evans
 
Trick or Treat?: Bitcoin for Non-Believers, Cryptocurrencies for Cypherpunks
Trick or Treat?: Bitcoin for Non-Believers, Cryptocurrencies for CypherpunksTrick or Treat?: Bitcoin for Non-Believers, Cryptocurrencies for Cypherpunks
Trick or Treat?: Bitcoin for Non-Believers, Cryptocurrencies for CypherpunksDavid Evans
 
Hidden Services, Zero Knowledge
Hidden Services, Zero KnowledgeHidden Services, Zero Knowledge
Hidden Services, Zero KnowledgeDavid Evans
 
Anonymity in Bitcoin
Anonymity in BitcoinAnonymity in Bitcoin
Anonymity in BitcoinDavid Evans
 
Midterm Confirmations
Midterm ConfirmationsMidterm Confirmations
Midterm ConfirmationsDavid Evans
 
Scripting Transactions
Scripting TransactionsScripting Transactions
Scripting TransactionsDavid Evans
 
How to Live in Paradise
How to Live in ParadiseHow to Live in Paradise
How to Live in ParadiseDavid Evans
 
Mining Economics
Mining EconomicsMining Economics
Mining EconomicsDavid Evans
 
Becoming More Paranoid
Becoming More ParanoidBecoming More Paranoid
Becoming More ParanoidDavid Evans
 
Asymmetric Key Signatures
Asymmetric Key SignaturesAsymmetric Key Signatures
Asymmetric Key SignaturesDavid Evans
 
Introduction to Cryptography
Introduction to CryptographyIntroduction to Cryptography
Introduction to CryptographyDavid Evans
 
Class 1: What is Money?
Class 1: What is Money?Class 1: What is Money?
Class 1: What is Money?David Evans
 
Multi-Party Computation for the Masses
Multi-Party Computation for the MassesMulti-Party Computation for the Masses
Multi-Party Computation for the MassesDavid Evans
 
Proof of Reserve
Proof of ReserveProof of Reserve
Proof of ReserveDavid Evans
 
Blooming Sidechains!
Blooming Sidechains!Blooming Sidechains!
Blooming Sidechains!David Evans
 
Useful Proofs of Work, Permacoin
Useful Proofs of Work, PermacoinUseful Proofs of Work, Permacoin
Useful Proofs of Work, PermacoinDavid Evans
 

More from David Evans (20)

Cryptocurrency Jeopardy!
Cryptocurrency Jeopardy!Cryptocurrency Jeopardy!
Cryptocurrency Jeopardy!
 
Trick or Treat?: Bitcoin for Non-Believers, Cryptocurrencies for Cypherpunks
Trick or Treat?: Bitcoin for Non-Believers, Cryptocurrencies for CypherpunksTrick or Treat?: Bitcoin for Non-Believers, Cryptocurrencies for Cypherpunks
Trick or Treat?: Bitcoin for Non-Believers, Cryptocurrencies for Cypherpunks
 
Hidden Services, Zero Knowledge
Hidden Services, Zero KnowledgeHidden Services, Zero Knowledge
Hidden Services, Zero Knowledge
 
Anonymity in Bitcoin
Anonymity in BitcoinAnonymity in Bitcoin
Anonymity in Bitcoin
 
Midterm Confirmations
Midterm ConfirmationsMidterm Confirmations
Midterm Confirmations
 
Scripting Transactions
Scripting TransactionsScripting Transactions
Scripting Transactions
 
How to Live in Paradise
How to Live in ParadiseHow to Live in Paradise
How to Live in Paradise
 
Bitcoin Script
Bitcoin ScriptBitcoin Script
Bitcoin Script
 
Mining Economics
Mining EconomicsMining Economics
Mining Economics
 
Mining
MiningMining
Mining
 
The Blockchain
The BlockchainThe Blockchain
The Blockchain
 
Becoming More Paranoid
Becoming More ParanoidBecoming More Paranoid
Becoming More Paranoid
 
Asymmetric Key Signatures
Asymmetric Key SignaturesAsymmetric Key Signatures
Asymmetric Key Signatures
 
Introduction to Cryptography
Introduction to CryptographyIntroduction to Cryptography
Introduction to Cryptography
 
Class 1: What is Money?
Class 1: What is Money?Class 1: What is Money?
Class 1: What is Money?
 
Multi-Party Computation for the Masses
Multi-Party Computation for the MassesMulti-Party Computation for the Masses
Multi-Party Computation for the Masses
 
Proof of Reserve
Proof of ReserveProof of Reserve
Proof of Reserve
 
Silk Road
Silk RoadSilk Road
Silk Road
 
Blooming Sidechains!
Blooming Sidechains!Blooming Sidechains!
Blooming Sidechains!
 
Useful Proofs of Work, Permacoin
Useful Proofs of Work, PermacoinUseful Proofs of Work, Permacoin
Useful Proofs of Work, Permacoin
 

Recently uploaded

ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 

Recently uploaded (20)

ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 

First Ride on Rust

  • 1. Class 2: First Ride on Rust cs4414 Fall 2013 University of Virginia David Evans
  • 2. Menu • Recap last week, syllabus questions • Overview of Rust (and why we are using it) • Brief history of Computing Systems • Some Rust constructs (for PS1) 112 September 2013 University of Virginia cs4414
  • 3. Why learn a new programming language? 12 September 2013 University of Virginia cs4414 2
  • 4. J S Bach, “Coffee Cantata”, BWV 211 (1732) www.npj.com/homepage/teritowe/jsbhand. html “Jamais Jamais Jamais” from Harmonice Musices Odhecaton A. (1501)
  • 5. Modern Music Notation Roman Haubenstock- Ramati, Concerto a Tre John Cage, Fontana Mix http://www.medienkunstnetz.de/works/fontana-mix/audio/1/
  • 6.
  • 7. Thought and Action Languages change the way we think BASIC: think about GOTO Algol, Python: think about assignments, control blocks Scheme, Haskell: think about procedures Java, C++: think about types, objects Languages provide abstractions of machine resources – Hide dangerous/confusing details: memory locations, instruction opcodes, number representations, calling conventions, etc. – Hiding more increases simplicity, but limits expressiveness What do we want for systems programming?
  • 8. Why so many programming languages?
  • 9. Fundamental Differences • All equivalently powerful! – Universal languages: all capable of simulating each other • Fundamental differences – Expressiveness: how easy it is to describe a computation – “Truthiness”: likelihood that a program means what a programmer things it means – Safeness: impact of programmer mistakes • There is a fundamental conflict between expressiveness and truthiness/safeness
  • 10. Programming Language Design Space Expressiveness “Truthiness” Scheme Python Java C++ C low high Spec# Ada strict typing, static BASIC more mistake prone less mistake prone print ("Hello!") (display “Hello!”)
  • 11. “a safe, concurrent, practical language” 12 September 2013 University of Virginia cs4414 10 Its design is oriented toward concerns of “programming in the large”, that is, of creating and maintaining boundaries – both abstract and operational – that preserve large-system integrity, availability and concurrency. from http://www.rust-lang.org/ Rust
  • 12. 12 September 2013 University of Virginia cs4414 11 http://www.rust-lang.org/
  • 13. 12 September 2013 University of Virginia cs4414 12 fn max(a: int, b: int) -> int { if a > b { a } else { b } } fn main() { println(fmt!("Max: %?", max(3, 4))); }
  • 14. Expressions and Statements Java/C++ IfStatement ::= if (Expression) StatementTrue [ else StatementFalse ] Rust IfExpression ::= if Expression Block [ else Block ] Block ::= { [Statement* Expr] } Expression ::= Block Statement ::= Expression ; 12 September 2013 University of Virginia cs4414 13 Simplified: see http://static.rust-lang.org/doc/0.7/rust.html#if-expressions for full grammar. Warning: “(looking for consistency in the manual's grammar is bad: it's entirely wrong in many places.)”
  • 15. Quiz 12 September 2013 University of Virginia cs4414 14 IfExpression ::= if Expression Block [ else Block ] Block ::= { [Statement* Expr] } Expression ::= Block fn max(a: int, b: int) -> int { if { } { a } else { b } } a) Syntax Error b) Type Error c) Run-time Error
  • 16. Quiz 12 September 2013 University of Virginia cs4414 15 IfExpression ::= if Expression Block [ else Block ] Block ::= { [Statement* Expr] } Expression ::= Block fn max(a: int, b: int) -> int { if { } { a } else { b } } $ rustc block.rs block.rs:2:7: 2:10 error: mismatched types: expected `bool` but found `()` (expected bool but found ()) block.rs:2 if { } { ^~~
  • 17. (Trick) Quiz 12 September 2013 University of Virginia cs4414 16 IfExpression ::= if Expression Block [ else Block ] Block ::= { [Statement* Expr] } Expression ::= Block fn max(a: int, b: int) -> int { if { let x = 4414; x = x + a; x > b + 4414 } { a } else { b } } a) Syntax Error b) Type Error c) Run-time Error
  • 18. (Trick) Quiz 12 September 2013 University of Virginia cs4414 17 IfExpression ::= if Expression Block [ else Block ] Block ::= { [Statement* Expr] } Expression ::= Block fn max(a: int, b: int) -> int { if { let x = 4414; x = x + a; x > b + 4414 } { a } else { b } } $ rustc block.rs block.rs:2:23: 2:24 error: re-assignment of immutable variable `x` block.rs:2 if { let x = 4414; x = x + a; x > b + 4414 } { ^ “Variables” are invariable…unless declared with mut.
  • 19. Quiz 12 September 2013 University of Virginia cs4414 18 IfExpression ::= if Expression Block [ else Block ] Block ::= { [Statement* Expr] } Expression ::= Block fn max(a: int, b: int) -> int { if { let mut x = 4414; x = x + a; x > b + 4414 } { a } else { b } } $ rust run block.rs Max: 5
  • 20. Quiz 12 September 2013 University of Virginia cs4414 19 IfExpression ::= if Expression Block [ else Block ] Block ::= { [Statement* Expr] } Expression ::= Block Statement ::= Expression ; fn max(a: int, b: int) -> int { if a > b { a } else { b; } } fn main() { println(fmt!("Max: %?", max(5, 4))); }
  • 21. Quiz 12 September 2013 University of Virginia cs4414 20 fn max(a: int, b: int) -> int { if a > b { a } else { b; } } fn main() { println(fmt!("Max: %?", max(5, 4))); } block.rs:2:24: 2:30 error: mismatched types: expected `int` but found `()` (expected int but found ()) block.rs:2 if a > b { a } else { b; } ^~~~~~ The semi-colon makes it a statement – no value
  • 22. Higher-Order Functions 12 September 2013 University of Virginia cs4414 21 Java Rust Java 8 (March 2014) Scheme (define (make-adder a) (lambda (n) (+ a n))) | <parameters> | Block
  • 23. 12 September 2013 University of Virginia cs4414 22 Define a function, make_adder(int), that takes an integer as input and returns a function that takes and int and returns the sum of the original and input int. make_adder(3)(1) => 4 let increment = make_adder(1);
  • 24. 12 September 2013 University of Virginia cs4414 23 fn make_adder(a : int) -> ~fn(int) -> int { |b| { a + b} }
  • 25. 12 September 2013 University of Virginia cs4414 24 Define a function, ntimes, that takes as inputs a function f (int -> int) and an integer n, and returns a function that applies f n-times. fn double(a: int) -> int { a * 2 } fn main() { let quadruple = ntimes(double, 2); println(fmt!("quad: %?", quadruple(2))); }
  • 26. 12 September 2013 University of Virginia cs4414 25 fn ntimes(f: extern fn(int) -> int, times: int) -> ~fn(int) -> int { |x| { if times == 0 { x } else { ntimes(f, times - 1)(f(x)) } } }
  • 27. Reading a File 12 September 2013 University of Virginia cs4414 26 http://static.rust-lang.org/doc/0.7/std/io.html#function-file_reader This isn’t an excerpt! This is all the documentation there is in the Rust manual.
  • 28. Arrgh! What to do? 12 September 2013 University of Virginia cs4414 27 Image: credit unknown
  • 29. 12 September 2013 University of Virginia cs4414 28 “Baby” programmer (cs1xxx)
  • 30. 12 September 2013 University of Virginia cs4414 29 Give up in disgust!
  • 32. Solving Programming Mysteries 12 September 2013 University of Virginia cs4414 31 http://www.smellsfunny.com/view.php?fid=17557
  • 33. Solving Programming Mysteries 1. DuckDuckGo (or Google) is your friend! 12 September 2013 University of Virginia cs4414 32
  • 34. 12 September 2013 University of Virginia cs4414 33 Caveats: HinderRust will probably not help at all Rust 0.7 is quite different from Rust 0.3 Easy to get lost in lots of details Top-ranked result is not always the best solution Dangerous to “cut-and-paste” code you don’t understand!
  • 35. Solving Programming Mysteries 1. DuckDuckGo (or Google) is your friend! 2. stackoverflow [rust] 12 September 2013 University of Virginia cs4414 34
  • 36. 12 September 2013 University of Virginia cs4414 35 Questions tagged with rust: 116 Questions tagged with java: 468,140
  • 37. Solving Programming Mysteries 1. DuckDuckGo (or Google) is your friend! 2. stackoverflow [rust] 3. Experiment! 12 September 2013 University of Virginia cs4414 36
  • 38. 12 September 2013 University of Virginia cs4414 37 fn main() { let filereader = std::io::file_reader(~std::path::Path("test.txt")); } $ rustc experiment.rs experiment.rs:6:8: 6:18 warning: unused variable: `filereader` [-W unused-variable (default)] experiment.rs:6 let filereader = std::io::file_reader(~std::path::Path("test.txt")); ^~~~~~~~~~ fn main() { let filereader = std::io::file_reader(~std::path::Path("test.txt")); println(fmt!("Reader: %?", filereader)); } $ rust run experiment.rs Reader: Err(~"error opening test.txt") $ echo ‘hello’ > test.txt $ rust run experiment.rs warning: no debug symbols in executable (-arch x86_64) Reader: Ok()
  • 39. Solving Programming Mysteries 1. DuckDuckGo (or Google) is your friend! 2. stackoverflow [rust] 3. Experiment! 4. Ask for help: – Course Piazza forum – IRC 12 September 2013 University of Virginia cs4414 38 If you figure something useful out that is not well documented, document it! course forum post, “blog” post
  • 40. Back to Reading Files 12 September 2013 University of Virginia cs4414 39 Result<T, U> : enumerated type: Ok(T) Err(U) Result<@Reader, ~str>
  • 41. match expression 12 September 2013 University of Virginia cs4414 40 fn load_file(pathname : ~str) -> ~[~str] { let filereader : Result<@Reader, ~str> = io::file_reader(~path::Path(pathname)); match filereader { Ok(reader) => reader.read_lines(), Err(msg) => fail!("Cannot open file: " + msg), } } How does this compare to the “Java” way?
  • 42. match 12 September 2013 University of Virginia cs4414 41 match filereader { Ok(reader) => reader.read_lines(), } compile-time completeness cat.rs:9:4: 11:5 error: non-exhaustive patterns: Err not covered current = match current { current if current % 2 == 0 => current / 2, _ => 3 * current + 1 }; match predicates can be arbitrary expressions _ (like default in C switch)
  • 43. Why NOT to use Rust 12 September 2013 University of Virginia cs4414 42
  • 44. 12 September 2013 University of Virginia cs4414 43 https://mail.mozilla.org/pipermail/rust-dev/2013-August/005409.html
  • 45. 12 September 2013 University of Virginia cs4414 44 https://github.com/graydon/rust/wiki This is a very preliminary work in progress. No supported releases yet nor defined release schedule / plans. Caveat emptor. It will crash. It will change syntax and semantics. It will eat your laundry. Use at your own risk. Etc. Originally posted: 15 Dec 2011 (but still there today)
  • 46. Rust or Bust? Rust  Immature, Unstable Poorly documented Few open source projects (except Servo) Not in high employer demand No other courses Bust (C) Mature, Stable Hundreds of books, etc. Lots of open source projects (incl. Linux) Popular for interview questions Nearly all OS courses 12 September 2013 University of Virginia cs4414 45
  • 47. Rust or Bust? Rust Benefits from past 30 years of language and compiler research Bust (C) Suffers from maintaining backwards compatibility with language stripped down to fit in ~9 KB 12 September 2013 University of Virginia cs4414 46
  • 48. 12 September 2013 University of Virginia cs4414 47 PDP-11 Ken Thompson Dennis Ritchie PDP-11 ~4KB of memory
  • 49. 12 September 2013 University of Virginia cs4414 48 “I call it my billion- dollar mistake.”(Sir Tony Hoare) int main(int argc, char **argv) { char *s = (char *) malloc (MY_SIZE); *s = 'a'; return 0; } Photo: Bertrand Meyer $ gcc -Wall null.c $ ./a.out Segmentation fault (core dumped) Note: Sir Tony invented null references in 1964 (before C), and said this in 2009 (back when an American billion dollars was still real money). See a great talk here: http://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare He designed compiler to check every reference is safe (but null and bounds checking), but C removed them as too expensive.
  • 50. Rust or Bust? Rust  Benefits from past 30 years of language and compiler research  Chance to influence a new, exciting, fast- evolving language  Some really cool features for memory management and concurrency  FUN! Bust (C)  Suffers from maintaining backwards compatibility  C++0x standard process began in 1998, released in 2011, over 40 meetings  Lots of complexity, but not designed for safety  Boring, Annoying 12 September 2013 University of Virginia cs4414 49
  • 51. Diving In: zhttpo.rs 12 September 2013 University of Virginia cs4414 50 // zhttpto.rs // ... extern mod extra; use extra::uv; use extra::{net_ip, net_tcp}; use std::str; static BACKLOG: uint = 5; static PORT: uint = 4414; static IPV4_LOOPBACK: &'static str = "127.0.0.1"; fn new_connection_callback(new_conn :net_tcp::TcpNewConnection, _killch: std::comm::SharedChan<Option<extra::net_tcp::TcpErrData>>) { ... Comments similar to C++ Dependency on an external “crate” (compilation unit) Import into namespace: uv = “extra::uv” global “variables” (no mut, so they are constants)
  • 52. main 12 September 2013 University of Virginia cs4414 51 fn main() { net_tcp::listen(net_ip::v4::parse_addr(IPV4_LOOPBACK), PORT, BACKLOG, &uv::global_loop::get(), |_chan| { println(fmt!("Listening on tcp port %u ...", PORT)); }, new_connection_callback); }
  • 53. 12 September 2013 University of Virginia cs4414 52 http://static.rust-lang.org/doc/std/net_tcp.html#function-listen
  • 54. 12 September 2013 University of Virginia cs4414 53 fn main() { net_tcp::listen(net_ip::v4::parse_addr(IPV4_LOOPBACK), PORT, BACKLOG, &uv::global_loop::get(), |_chan| { println(fmt!("Listening on tcp port %u ...", PORT)); }, new_connection_callback); } on_establish_cb: ~fn(SharedChan<Option<TcpErrData>>) a callback that is evaluated if/when the listener is successfully established. it takes no parameters new_connect_cb - a callback to be evaluated, on the libuv thread, whenever a client attempts to connect on the provided ip/port. the callback’s arguments are: …
  • 55. 12 September 2013 University of Virginia cs4414 54 fn new_connection_callback(new_conn : net_tcp::TcpNewConnection, _killch : std::comm::SharedChan<…>) { do spawn { let accept_result = extra::net_tcp::accept(new_conn); match accept_result { Err(err) => { println(fmt!("Connection error: %?", err)); }, Ok(sock) => { let peer_addr: ~str = net_ip::format_addr(&sock.get_peer_addr()); println(fmt!("Received connection from: %s", peer_addr)); let read_result = net_tcp::read(&sock, 0u); match read_result { Err(err) => { println(fmt!("Receive error: %?", err)); }, Ok(bytes) => { let request_str = str::from_bytes(bytes.slice(0, bytes.len() - 1)); println(fmt!("Request received:n%s", request_str)); let response: ~str = ~ "HTTP/1.1 …”; net_tcp::write(&sock, response.as_bytes_with_null_consume()); }, }; } } }; }
  • 56. Charge • You should be making progress on PS1 now: you know everything you need to finish it – Take advantage of Piazza forum and IRC for any strange Rustiness you encounter • Next class: processes! 12 September 2013 University of Virginia cs4414 55
  • 57. Ways to Learn New Languages • Read the compiler source code • Read the reference manual • Go through a (well designed!) tutorial • Read a good book with lots of exercises • Dive right into a complex program and try to modify it • Find a mentor who is a language expert 12 September 2013 University of Virginia cs4414 56