SlideShare a Scribd company logo
1 of 42
cs4414 Fall 2013
University of Virginia
David Evans
Plan for Today
Why and how to use git
Practice programming pointers (making a List)
24 September 2013 University of Virginia cs4414 1
(Paper) notes for today have some of the
code. Posted notes will have all code.
I won’t be able to hold my usual office hours this
afternoon, but can meet (briefly) after class today and/or
arrange another time.
How (Not) to Manage Files
24 September 2013 University of Virginia cs4414 2
“Smart Lawyer”
Version Control
Version Control
• How can Alice find the last working version of
her code before she broke everything trying to
fix a little bug?
• How can Alice and Bob work on a program
together?
• How can 10,000 developers work together on
the Linux kernel?
24 September 2013 University of Virginia cs4414 3
Annual Linux Development Report
24 September 2013 University of Virginia cs4414 4
Number of
Changes per
Hour
“Since the beginning of
the git era (the 2.6.11
release in 2005), a total
of 9,784 developers have
contributed to the Linux
kernel; those developers
worked for a minimum of
1,064 companies.”
Top Companies
24 September 2013 University of Virginia cs4414 5
24 September 2013 University of Virginia cs4414 6
2011-07-21
2013-06-30
24 September 2013 University of Virginia cs4414 7
http://www.vidarholen.net/contents/wordcount/
Centralized Version Control
24 September 2013 University of Virginia cs4414 8
Repository
(cvs, subversion)
Alice:
gash> svn checkout
gash> svn update
[make changes]
gash> svn commit
Bob:
gash> svn checkout
gash> svn update
[make changes]
gash> svn commit
Distributed
Version
Control
24 September 2013 University of Virginia cs4414 9
Main Repository
(git, Mercurial)
[make changes]
Alice’s
Local
Repository
commit
[make changes]
Bob’s
Local
Repository
commitupdate update
24 September 2013 University of Virginia cs4414 10
Main
Repository
(Hg)
[make changes]
Alice’s
Local
Repository
update,
commit
[make changes]
Bob’s
Local
Repository
update,
commit
Repository
(svn)
[make changes] [make changes]
Centralized: One Repo Distributed
What has to happen
before Bob sees
Alice’s changes?
24 September 2013 University of Virginia cs4414 11
Main Repository
(Hg)
changed gash.rs
Alice’s
Local
Repository
commit update
Bob’s
Local
Repository
Alice Bob
see gash.rs
git pull = “pull” and “update”
24 September 2013 University of Virginia cs4414 12
Main Repository
(git)
changed gash.rs
Alice’s
Local
Repository
commit
Bob’s
Local
Repository
Alice Bob
see gash.rs
(I find this asymmetrical
and confusing…but not
many scenarios where
pulling to local without
updating is useful.)
pull is not the analog of push –
it is analog of commit + push
What if Bob had modified his copy?
24 September 2013 University of Virginia cs4414 13
Main Repository
(git)
changed zhttpto.rs
Alice’s
Local
Repository
commit
Bob’s
Local
Repository
Alice Bob
changed zhttpto.rs
gash> git pull
…
error: Your local changes to the following
files would be overwritten by merge:
ps1/zhttpto.rs
Please, commit your changes or stash
them before you can merge.
Aborting
Okay, let’s commit:
24 September 2013 University of Virginia cs4414 14
Main Repository
(git)
changed gash.rs
Alice’s
Local
Repository
commit
Bob’s
Local
Repository
Alice Bob
changed gash.rs
gash> git commit -a –m "Added a comment about lack of security."
[master 1347c1f] Fixed the notorious zombie process bug.
1 files changed, 3 insertions(+), 0 deletions(-)
gash> git pull
Auto-merging ps1/zhttpto.rs
CONFLICT (content): Merge conflict in ps1/zhttpto.rs
Automatic merge failed; fix conflicts and then commit the result.
Observing Conflicts
24 September 2013 University of Virginia cs4414 15
//
// zhttpto.rs
//
// Reference solution for PS1
//
// Special thanks to Kiet Tran for providing code we incorporated into this.
//
<<<<<<< HEAD
// Note: it would be very unwise to run this server on a machine that is
// on the Internet and contains any sensitive files!
=======
// Warning: this is not a secure server!
>>>>>>> faf7829d3ab38459172b622351d68ac1f47bddd0
//
// University of Virginia - cs4414 Fall 2013
Resolving Conflicts (for Luddites)
24 September 2013 University of Virginia cs4414 16
gash> emacs zhttpto.rs
edit conflicted file manually (removing the <<<< and ====)
gash> git commit -a -m "Updated security message."
[master 1e6e684] Updated security message.
gash> git push
…
To https://github.com/cs4414/Reference-Solutions.git
faf7829..1e6e684 master -> master
Resolving Conflicts (for Moderns)
24 September 2013 University of Virginia cs4414 17
git mergetool
Avoiding Conflicts
24 September 2013 University of Virginia cs4414 18
It’s easier to ask
forgiveness than it is to get
permission.
Admiral Grace HopperWith conflicts, it is better to avoid
them than to resolve them!
- pull before you start modifying, and
often while working
- commit early and often, use good
messages
- push whenever you have something
worth sharing (but don’t push junk)
- divide your project into small,
coherent files
- communicate well with your
teammates!
Avoiding Major Conflicts with Teammates
24 September 2013 University of Virginia cs4414 19
Don’t resolve conflicts
by just undoing others’
work!
At least make sure you
understand it before
replacing their changes
with your own.
24 September 2013 University of Virginia cs4414 20
What’s more important for getting an
interesting computing job?
Impressive Transcript from
Prestigious Institution
Impressive Code and Record
in Hacker Communities
24 September 2013 University of Virginia cs4414 21
Linked Lists in Rust
24 September 2013 University of Virginia cs4414 22
What’s a List?
24 September 2013 University of Virginia cs4414 23
Linked Lists
A List is an object that is either:
Null (a special value representing empty list)
or a pair whose second part is a List
24 September 2013 University of Virginia cs4414 24
struct Node {
head : int,
tail : Option<@Node>
}
type List = Option<@Node> ;
Keeping things simple for now!
@ = automatically managed
24 September 2013 University of Virginia cs4414 25
let p: List =
Some(@Node{head: 1,
tail: Some(@Node{head : 2,
tail: Some(@Node{head: 3,
tail: None})})});
struct Node {
head : int,
tail : Option<@Node>
}
type List = Option<@Node> ;
to_str
24 September 2013 University of Virginia cs4414 26
struct Node {
head : int,
tail : Option<@Node>
}
type List = Option<@Node> ;
24 September 2013 University of Virginia cs4414 27
fn to_str(lst: Option<@Node>) -> ~str {
fn elements_to_str(n: @Node) -> ~str {
match (n.tail) {
None => fmt!("%?", n.head),
Some(tail) => fmt!("%?, %s", n.head, elements_to_str(tail))
}
}
match(lst) {
None => ~"Null",
Some(n) => fmt!("[%s]", elements_to_str(n))
}
}
Using Traits
24 September 2013 University of Virginia cs4414 28
trait ToStr {
fn to_str (&self) -> ~str;
}
(ToStr is part of the core)
Similar to interface in Java (except Rust
traits can include default implementations).
24 September 2013 University of Virginia cs4414 29
impl ToStr for List {
fn to_str(&self) -> ~str {
fn elements_to_str(n: @Node) -> ~str {
match (n.tail) {
None => fmt!("%?", n.head),
Some(tail) => fmt!("%?, %s", n.head, elements_to_str(tail))
}
}
match(*self) {
None => ~"Null",
Some(n) => fmt!("[%s]", elements_to_str(n))
}
}
}
fn main() {
let lst : List = Some(@Node{head: 1, tail: Some(@Node{head : 2,
tail: Some(@Node{head: 3, tail: None})})});
println(fmt!("%s", lst.to_str()));
}
Using our List (?)
24 September 2013 University of Virginia cs4414 30
fn main() {
let lst : List =
Some(@Node{head: 1,
tail: Some(@Node{head : 2,
tail: Some(@Node{head: 3, tail: None})})});
println(lst.to_str());
lst.head = 0;
println(lst.to_str());
}
Making it mutable
24 September 2013 University of Virginia cs4414 31
struct Node {
head : int,
tail : Option<@Node>
}
type List = Option<@Node> ;
Since Rust 0.6 – can’t
make struct fields mut
struct Node {
head : int,
tail : Option<@mut Node>
}
type List = Option<@mut Node> ;
24 September 2013 University of Virginia cs4414 32
fn main() {
let lst : List = Some(@mut Node{head: 1,
tail: Some(@mut Node{head : 2,
tail: Some(@mut Node{head: 3, tail: None})})});
println(lst.to_str());
match lst {
None => fail!("Unexpected None!"),
Some(n) => n.head = 0
}
println(lst.to_str());
}
Increment All
24 September 2013 University of Virginia cs4414 33
Write a List method that increments the value of every
element of the list.
Increment All
24 September 2013 University of Virginia cs4414 34
trait Increment {
fn incr(&self);
}
impl Increment for List {
fn incr(&self) {
let mut current = *self;
loop {
match(current) {
None => break,
Some(node) => { node.head += 1; current = node.tail },
}
}
}
}
Mapping
24 September 2013 University of Virginia cs4414 35
self.mapr(|x: int| { x + 1 })
Define a higher-order
mapr method that
applies a function to all
elements in a List.
24 September 2013 University of Virginia cs4414 36
impl Map for List {
fn mapr(&self, f: &fn(int) -> int) {
let mut current = *self;
loop {
match(current) {
None => break,
Some(node) => { node.head = f(node.head);
current = node.tail },
}
}
}
}
Don’t we want to avoid @?
24 September 2013 University of Virginia cs4414 37
24 September 2013 University of Virginia cs4414 38
struct Node {
head : int,
tail : Option<~Node>
}
type List = Option<~Node> ;
What else needs to change to make a List with owned Nodes?
24 September 2013 University of Virginia cs4414 39
struct Node {
head : int,
tail : Option<~Node>
}
type List = Option<~Node> ;
trait Map {
fn mapr(&self, &fn(int) -> int)
}
-> List;
24 September 2013 University of Virginia cs4414 40
struct Node {
head : int,
tail : Option<~Node>
}
type List = Option<~Node> ;
trait Map {
fn mapr(&self, &fn(int) -> int) -> List;
}
impl Map for List {
fn mapr(&self, f: &fn(int) -> int) -> List {
match(*self) {
None => None,
Some(ref node) => { Some(~Node{ head: f(node.head),
tail: node.tail.mapr(f) }) },
}
}
} Is this better or worse than the @mut version?
Next class: making map
multi-threaded!
Read the MapReduce paper
(or at least the slides) before
Thursday’s class
24 September 2013 University of Virginia cs4414 41
Posted notes (later today) will have all code.
I won’t be able to hold my usual
office hours this afternoon, but
can meet after class today
and/or arrange another time.

More Related Content

What's hot

Workshop on command line tools - day 2
Workshop on command line tools - day 2Workshop on command line tools - day 2
Workshop on command line tools - day 2Leandro Lima
 
Workshop on command line tools - day 1
Workshop on command line tools - day 1Workshop on command line tools - day 1
Workshop on command line tools - day 1Leandro Lima
 
Intro to Rust from Applicative / NY Meetup
Intro to Rust from Applicative / NY MeetupIntro to Rust from Applicative / NY Meetup
Intro to Rust from Applicative / NY Meetupnikomatsakis
 
Andrey Listochkin "Everybody stand back! I know regular expressions"
Andrey Listochkin "Everybody stand back! I know regular expressions"Andrey Listochkin "Everybody stand back! I know regular expressions"
Andrey Listochkin "Everybody stand back! I know regular expressions"Fwdays
 
ICML 2018 Reproducible Machine Learning - A. Gramfort
ICML 2018 Reproducible Machine Learning - A. GramfortICML 2018 Reproducible Machine Learning - A. Gramfort
ICML 2018 Reproducible Machine Learning - A. Gramfortagramfort
 
Keynote 1 - Engineering Software Analytics Studies
Keynote 1 - Engineering Software Analytics StudiesKeynote 1 - Engineering Software Analytics Studies
Keynote 1 - Engineering Software Analytics StudiesESEM 2014
 
Rust: Reach Further (from QCon Sao Paolo 2018)
Rust: Reach Further (from QCon Sao Paolo 2018)Rust: Reach Further (from QCon Sao Paolo 2018)
Rust: Reach Further (from QCon Sao Paolo 2018)nikomatsakis
 

What's hot (8)

Anton Dignös - Towards a Temporal PostgresSQL
Anton Dignös - Towards a Temporal PostgresSQLAnton Dignös - Towards a Temporal PostgresSQL
Anton Dignös - Towards a Temporal PostgresSQL
 
Workshop on command line tools - day 2
Workshop on command line tools - day 2Workshop on command line tools - day 2
Workshop on command line tools - day 2
 
Workshop on command line tools - day 1
Workshop on command line tools - day 1Workshop on command line tools - day 1
Workshop on command line tools - day 1
 
Intro to Rust from Applicative / NY Meetup
Intro to Rust from Applicative / NY MeetupIntro to Rust from Applicative / NY Meetup
Intro to Rust from Applicative / NY Meetup
 
Andrey Listochkin "Everybody stand back! I know regular expressions"
Andrey Listochkin "Everybody stand back! I know regular expressions"Andrey Listochkin "Everybody stand back! I know regular expressions"
Andrey Listochkin "Everybody stand back! I know regular expressions"
 
ICML 2018 Reproducible Machine Learning - A. Gramfort
ICML 2018 Reproducible Machine Learning - A. GramfortICML 2018 Reproducible Machine Learning - A. Gramfort
ICML 2018 Reproducible Machine Learning - A. Gramfort
 
Keynote 1 - Engineering Software Analytics Studies
Keynote 1 - Engineering Software Analytics StudiesKeynote 1 - Engineering Software Analytics Studies
Keynote 1 - Engineering Software Analytics Studies
 
Rust: Reach Further (from QCon Sao Paolo 2018)
Rust: Reach Further (from QCon Sao Paolo 2018)Rust: Reach Further (from QCon Sao Paolo 2018)
Rust: Reach Further (from QCon Sao Paolo 2018)
 

Viewers also liked

Creating Custom Drupal Modules
Creating Custom Drupal ModulesCreating Custom Drupal Modules
Creating Custom Drupal Modulestanoshimi
 
Manipulating file in Python
Manipulating file in PythonManipulating file in Python
Manipulating file in Pythonshoukatali500
 
EuroPython 2013 - FAST, DOCUMENTED AND RELIABLE JSON BASED WEBSERVICES WITH P...
EuroPython 2013 - FAST, DOCUMENTED AND RELIABLE JSON BASED WEBSERVICES WITH P...EuroPython 2013 - FAST, DOCUMENTED AND RELIABLE JSON BASED WEBSERVICES WITH P...
EuroPython 2013 - FAST, DOCUMENTED AND RELIABLE JSON BASED WEBSERVICES WITH P...Alessandro Molina
 
Using Git on the Command Line
Using Git on the Command LineUsing Git on the Command Line
Using Git on the Command LineBrian Richards
 
Code Refactoring - Live Coding Demo (JavaDay 2014)
Code Refactoring - Live Coding Demo (JavaDay 2014)Code Refactoring - Live Coding Demo (JavaDay 2014)
Code Refactoring - Live Coding Demo (JavaDay 2014)Peter Kofler
 
FLTK Summer Course - Part III - Third Impact
FLTK Summer Course - Part III - Third ImpactFLTK Summer Course - Part III - Third Impact
FLTK Summer Course - Part III - Third ImpactMichel Alves
 
Blisstering drupal module development ppt v1.2
Blisstering drupal module development ppt v1.2Blisstering drupal module development ppt v1.2
Blisstering drupal module development ppt v1.2Anil Sagar
 
FLTK Summer Course - Part II - Second Impact - Exercises
FLTK Summer Course - Part II - Second Impact - Exercises FLTK Summer Course - Part II - Second Impact - Exercises
FLTK Summer Course - Part II - Second Impact - Exercises Michel Alves
 
FLTK Summer Course - Part VIII - Eighth Impact
FLTK Summer Course - Part VIII - Eighth ImpactFLTK Summer Course - Part VIII - Eighth Impact
FLTK Summer Course - Part VIII - Eighth ImpactMichel Alves
 
Introduction to Git Commands and Concepts
Introduction to Git Commands and ConceptsIntroduction to Git Commands and Concepts
Introduction to Git Commands and ConceptsCarl Brown
 
FLTK Summer Course - Part I - First Impact - Exercises
FLTK Summer Course - Part I - First Impact - ExercisesFLTK Summer Course - Part I - First Impact - Exercises
FLTK Summer Course - Part I - First Impact - ExercisesMichel Alves
 
"Git Hooked!" Using Git hooks to improve your software development process
"Git Hooked!" Using Git hooks to improve your software development process"Git Hooked!" Using Git hooks to improve your software development process
"Git Hooked!" Using Git hooks to improve your software development processPolished Geek LLC
 
Servicios web con Python
Servicios web con PythonServicios web con Python
Servicios web con PythonManuel Pérez
 
Git hooks For PHP Developers
Git hooks For PHP DevelopersGit hooks For PHP Developers
Git hooks For PHP DevelopersUmut IŞIK
 
FLTK Summer Course - Part VII - Seventh Impact
FLTK Summer Course - Part VII  - Seventh ImpactFLTK Summer Course - Part VII  - Seventh Impact
FLTK Summer Course - Part VII - Seventh ImpactMichel Alves
 
FLTK Summer Course - Part VI - Sixth Impact - Exercises
FLTK Summer Course - Part VI - Sixth Impact - ExercisesFLTK Summer Course - Part VI - Sixth Impact - Exercises
FLTK Summer Course - Part VI - Sixth Impact - ExercisesMichel Alves
 
TMS - Schedule of Presentations and Reports
TMS - Schedule of Presentations and ReportsTMS - Schedule of Presentations and Reports
TMS - Schedule of Presentations and ReportsMichel Alves
 
FLTK Summer Course - Part II - Second Impact
FLTK Summer Course - Part II - Second ImpactFLTK Summer Course - Part II - Second Impact
FLTK Summer Course - Part II - Second ImpactMichel Alves
 

Viewers also liked (20)

Scheduling
SchedulingScheduling
Scheduling
 
Creating Custom Drupal Modules
Creating Custom Drupal ModulesCreating Custom Drupal Modules
Creating Custom Drupal Modules
 
Manipulating file in Python
Manipulating file in PythonManipulating file in Python
Manipulating file in Python
 
EuroPython 2013 - FAST, DOCUMENTED AND RELIABLE JSON BASED WEBSERVICES WITH P...
EuroPython 2013 - FAST, DOCUMENTED AND RELIABLE JSON BASED WEBSERVICES WITH P...EuroPython 2013 - FAST, DOCUMENTED AND RELIABLE JSON BASED WEBSERVICES WITH P...
EuroPython 2013 - FAST, DOCUMENTED AND RELIABLE JSON BASED WEBSERVICES WITH P...
 
Using Git on the Command Line
Using Git on the Command LineUsing Git on the Command Line
Using Git on the Command Line
 
Code Refactoring - Live Coding Demo (JavaDay 2014)
Code Refactoring - Live Coding Demo (JavaDay 2014)Code Refactoring - Live Coding Demo (JavaDay 2014)
Code Refactoring - Live Coding Demo (JavaDay 2014)
 
FLTK Summer Course - Part III - Third Impact
FLTK Summer Course - Part III - Third ImpactFLTK Summer Course - Part III - Third Impact
FLTK Summer Course - Part III - Third Impact
 
Blisstering drupal module development ppt v1.2
Blisstering drupal module development ppt v1.2Blisstering drupal module development ppt v1.2
Blisstering drupal module development ppt v1.2
 
Advanced Git
Advanced GitAdvanced Git
Advanced Git
 
FLTK Summer Course - Part II - Second Impact - Exercises
FLTK Summer Course - Part II - Second Impact - Exercises FLTK Summer Course - Part II - Second Impact - Exercises
FLTK Summer Course - Part II - Second Impact - Exercises
 
FLTK Summer Course - Part VIII - Eighth Impact
FLTK Summer Course - Part VIII - Eighth ImpactFLTK Summer Course - Part VIII - Eighth Impact
FLTK Summer Course - Part VIII - Eighth Impact
 
Introduction to Git Commands and Concepts
Introduction to Git Commands and ConceptsIntroduction to Git Commands and Concepts
Introduction to Git Commands and Concepts
 
FLTK Summer Course - Part I - First Impact - Exercises
FLTK Summer Course - Part I - First Impact - ExercisesFLTK Summer Course - Part I - First Impact - Exercises
FLTK Summer Course - Part I - First Impact - Exercises
 
"Git Hooked!" Using Git hooks to improve your software development process
"Git Hooked!" Using Git hooks to improve your software development process"Git Hooked!" Using Git hooks to improve your software development process
"Git Hooked!" Using Git hooks to improve your software development process
 
Servicios web con Python
Servicios web con PythonServicios web con Python
Servicios web con Python
 
Git hooks For PHP Developers
Git hooks For PHP DevelopersGit hooks For PHP Developers
Git hooks For PHP Developers
 
FLTK Summer Course - Part VII - Seventh Impact
FLTK Summer Course - Part VII  - Seventh ImpactFLTK Summer Course - Part VII  - Seventh Impact
FLTK Summer Course - Part VII - Seventh Impact
 
FLTK Summer Course - Part VI - Sixth Impact - Exercises
FLTK Summer Course - Part VI - Sixth Impact - ExercisesFLTK Summer Course - Part VI - Sixth Impact - Exercises
FLTK Summer Course - Part VI - Sixth Impact - Exercises
 
TMS - Schedule of Presentations and Reports
TMS - Schedule of Presentations and ReportsTMS - Schedule of Presentations and Reports
TMS - Schedule of Presentations and Reports
 
FLTK Summer Course - Part II - Second Impact
FLTK Summer Course - Part II - Second ImpactFLTK Summer Course - Part II - Second Impact
FLTK Summer Course - Part II - Second Impact
 

Similar to Using Git, Pointers in Rust

Research @ RELEASeD (presented at SATTOSE2013)
Research @ RELEASeD (presented at SATTOSE2013)Research @ RELEASeD (presented at SATTOSE2013)
Research @ RELEASeD (presented at SATTOSE2013)kim.mens
 
Virtual Memory (Making a Process)
Virtual Memory (Making a Process)Virtual Memory (Making a Process)
Virtual Memory (Making a Process)David Evans
 
Assignment of pseudo code
Assignment of pseudo codeAssignment of pseudo code
Assignment of pseudo codeBurhan Chaudhry
 
R programming for data science
R programming for data scienceR programming for data science
R programming for data scienceSovello Hildebrand
 
Reducing Redundancies in Multi-Revision Code Analysis
Reducing Redundancies in Multi-Revision Code AnalysisReducing Redundancies in Multi-Revision Code Analysis
Reducing Redundancies in Multi-Revision Code AnalysisSebastiano Panichella
 
Introduction to the R Statistical Computing Environment
Introduction to the R Statistical Computing EnvironmentIntroduction to the R Statistical Computing Environment
Introduction to the R Statistical Computing Environmentizahn
 
GRAPHICAL STRUCTURES in our lives
GRAPHICAL STRUCTURES in our livesGRAPHICAL STRUCTURES in our lives
GRAPHICAL STRUCTURES in our livesxryuseix
 
Web Traffic Time Series Forecasting
Web Traffic  Time Series ForecastingWeb Traffic  Time Series Forecasting
Web Traffic Time Series ForecastingBillTubbs
 
Functional Reactive Programming on Android
Functional Reactive Programming on AndroidFunctional Reactive Programming on Android
Functional Reactive Programming on AndroidSam Lee
 
Making a Process
Making a ProcessMaking a Process
Making a ProcessDavid Evans
 
Neo4j after 1 year in production
Neo4j after 1 year in productionNeo4j after 1 year in production
Neo4j after 1 year in productionAndrew Nikishaev
 
Introduction to Git for developers
Introduction to Git for developersIntroduction to Git for developers
Introduction to Git for developersDmitry Guyvoronsky
 
Custom Pregel Algorithms in ArangoDB
Custom Pregel Algorithms in ArangoDBCustom Pregel Algorithms in ArangoDB
Custom Pregel Algorithms in ArangoDBArangoDB Database
 
A Lightweight Infrastructure for Graph Analytics
A Lightweight Infrastructure for Graph AnalyticsA Lightweight Infrastructure for Graph Analytics
A Lightweight Infrastructure for Graph AnalyticsDonald Nguyen
 
Graph Analytics with ArangoDB
Graph Analytics with ArangoDBGraph Analytics with ArangoDB
Graph Analytics with ArangoDBArangoDB Database
 
Introduction to Twitter Storm
Introduction to Twitter StormIntroduction to Twitter Storm
Introduction to Twitter StormUwe Printz
 
Distributed Graph Algorithms
Distributed Graph AlgorithmsDistributed Graph Algorithms
Distributed Graph AlgorithmsSaurav Kumar
 

Similar to Using Git, Pointers in Rust (20)

Research @ RELEASeD (presented at SATTOSE2013)
Research @ RELEASeD (presented at SATTOSE2013)Research @ RELEASeD (presented at SATTOSE2013)
Research @ RELEASeD (presented at SATTOSE2013)
 
Virtual Memory (Making a Process)
Virtual Memory (Making a Process)Virtual Memory (Making a Process)
Virtual Memory (Making a Process)
 
Assignment of pseudo code
Assignment of pseudo codeAssignment of pseudo code
Assignment of pseudo code
 
Access Control
Access ControlAccess Control
Access Control
 
R programming for data science
R programming for data scienceR programming for data science
R programming for data science
 
Reducing Redundancies in Multi-Revision Code Analysis
Reducing Redundancies in Multi-Revision Code AnalysisReducing Redundancies in Multi-Revision Code Analysis
Reducing Redundancies in Multi-Revision Code Analysis
 
Introduction to the R Statistical Computing Environment
Introduction to the R Statistical Computing EnvironmentIntroduction to the R Statistical Computing Environment
Introduction to the R Statistical Computing Environment
 
cb streams - gavin pickin
cb streams - gavin pickincb streams - gavin pickin
cb streams - gavin pickin
 
GRAPHICAL STRUCTURES in our lives
GRAPHICAL STRUCTURES in our livesGRAPHICAL STRUCTURES in our lives
GRAPHICAL STRUCTURES in our lives
 
Web Traffic Time Series Forecasting
Web Traffic  Time Series ForecastingWeb Traffic  Time Series Forecasting
Web Traffic Time Series Forecasting
 
Functional Reactive Programming on Android
Functional Reactive Programming on AndroidFunctional Reactive Programming on Android
Functional Reactive Programming on Android
 
Making a Process
Making a ProcessMaking a Process
Making a Process
 
Neo4j after 1 year in production
Neo4j after 1 year in productionNeo4j after 1 year in production
Neo4j after 1 year in production
 
Nzitf Velociraptor Workshop
Nzitf Velociraptor WorkshopNzitf Velociraptor Workshop
Nzitf Velociraptor Workshop
 
Introduction to Git for developers
Introduction to Git for developersIntroduction to Git for developers
Introduction to Git for developers
 
Custom Pregel Algorithms in ArangoDB
Custom Pregel Algorithms in ArangoDBCustom Pregel Algorithms in ArangoDB
Custom Pregel Algorithms in ArangoDB
 
A Lightweight Infrastructure for Graph Analytics
A Lightweight Infrastructure for Graph AnalyticsA Lightweight Infrastructure for Graph Analytics
A Lightweight Infrastructure for Graph Analytics
 
Graph Analytics with ArangoDB
Graph Analytics with ArangoDBGraph Analytics with ArangoDB
Graph Analytics with ArangoDB
 
Introduction to Twitter Storm
Introduction to Twitter StormIntroduction to Twitter Storm
Introduction to Twitter Storm
 
Distributed Graph Algorithms
Distributed Graph AlgorithmsDistributed Graph Algorithms
Distributed Graph Algorithms
 

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

Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 

Recently uploaded (20)

Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 

Using Git, Pointers in Rust

  • 1. cs4414 Fall 2013 University of Virginia David Evans
  • 2. Plan for Today Why and how to use git Practice programming pointers (making a List) 24 September 2013 University of Virginia cs4414 1 (Paper) notes for today have some of the code. Posted notes will have all code. I won’t be able to hold my usual office hours this afternoon, but can meet (briefly) after class today and/or arrange another time.
  • 3. How (Not) to Manage Files 24 September 2013 University of Virginia cs4414 2 “Smart Lawyer” Version Control
  • 4. Version Control • How can Alice find the last working version of her code before she broke everything trying to fix a little bug? • How can Alice and Bob work on a program together? • How can 10,000 developers work together on the Linux kernel? 24 September 2013 University of Virginia cs4414 3 Annual Linux Development Report
  • 5. 24 September 2013 University of Virginia cs4414 4 Number of Changes per Hour “Since the beginning of the git era (the 2.6.11 release in 2005), a total of 9,784 developers have contributed to the Linux kernel; those developers worked for a minimum of 1,064 companies.”
  • 6. Top Companies 24 September 2013 University of Virginia cs4414 5
  • 7. 24 September 2013 University of Virginia cs4414 6 2011-07-21 2013-06-30
  • 8. 24 September 2013 University of Virginia cs4414 7 http://www.vidarholen.net/contents/wordcount/
  • 9. Centralized Version Control 24 September 2013 University of Virginia cs4414 8 Repository (cvs, subversion) Alice: gash> svn checkout gash> svn update [make changes] gash> svn commit Bob: gash> svn checkout gash> svn update [make changes] gash> svn commit
  • 10. Distributed Version Control 24 September 2013 University of Virginia cs4414 9 Main Repository (git, Mercurial) [make changes] Alice’s Local Repository commit [make changes] Bob’s Local Repository commitupdate update
  • 11. 24 September 2013 University of Virginia cs4414 10 Main Repository (Hg) [make changes] Alice’s Local Repository update, commit [make changes] Bob’s Local Repository update, commit Repository (svn) [make changes] [make changes] Centralized: One Repo Distributed
  • 12. What has to happen before Bob sees Alice’s changes? 24 September 2013 University of Virginia cs4414 11 Main Repository (Hg) changed gash.rs Alice’s Local Repository commit update Bob’s Local Repository Alice Bob see gash.rs
  • 13. git pull = “pull” and “update” 24 September 2013 University of Virginia cs4414 12 Main Repository (git) changed gash.rs Alice’s Local Repository commit Bob’s Local Repository Alice Bob see gash.rs (I find this asymmetrical and confusing…but not many scenarios where pulling to local without updating is useful.) pull is not the analog of push – it is analog of commit + push
  • 14. What if Bob had modified his copy? 24 September 2013 University of Virginia cs4414 13 Main Repository (git) changed zhttpto.rs Alice’s Local Repository commit Bob’s Local Repository Alice Bob changed zhttpto.rs gash> git pull … error: Your local changes to the following files would be overwritten by merge: ps1/zhttpto.rs Please, commit your changes or stash them before you can merge. Aborting
  • 15. Okay, let’s commit: 24 September 2013 University of Virginia cs4414 14 Main Repository (git) changed gash.rs Alice’s Local Repository commit Bob’s Local Repository Alice Bob changed gash.rs gash> git commit -a –m "Added a comment about lack of security." [master 1347c1f] Fixed the notorious zombie process bug. 1 files changed, 3 insertions(+), 0 deletions(-) gash> git pull Auto-merging ps1/zhttpto.rs CONFLICT (content): Merge conflict in ps1/zhttpto.rs Automatic merge failed; fix conflicts and then commit the result.
  • 16. Observing Conflicts 24 September 2013 University of Virginia cs4414 15 // // zhttpto.rs // // Reference solution for PS1 // // Special thanks to Kiet Tran for providing code we incorporated into this. // <<<<<<< HEAD // Note: it would be very unwise to run this server on a machine that is // on the Internet and contains any sensitive files! ======= // Warning: this is not a secure server! >>>>>>> faf7829d3ab38459172b622351d68ac1f47bddd0 // // University of Virginia - cs4414 Fall 2013
  • 17. Resolving Conflicts (for Luddites) 24 September 2013 University of Virginia cs4414 16 gash> emacs zhttpto.rs edit conflicted file manually (removing the <<<< and ====) gash> git commit -a -m "Updated security message." [master 1e6e684] Updated security message. gash> git push … To https://github.com/cs4414/Reference-Solutions.git faf7829..1e6e684 master -> master
  • 18. Resolving Conflicts (for Moderns) 24 September 2013 University of Virginia cs4414 17 git mergetool
  • 19. Avoiding Conflicts 24 September 2013 University of Virginia cs4414 18 It’s easier to ask forgiveness than it is to get permission. Admiral Grace HopperWith conflicts, it is better to avoid them than to resolve them! - pull before you start modifying, and often while working - commit early and often, use good messages - push whenever you have something worth sharing (but don’t push junk) - divide your project into small, coherent files - communicate well with your teammates!
  • 20. Avoiding Major Conflicts with Teammates 24 September 2013 University of Virginia cs4414 19 Don’t resolve conflicts by just undoing others’ work! At least make sure you understand it before replacing their changes with your own.
  • 21. 24 September 2013 University of Virginia cs4414 20 What’s more important for getting an interesting computing job?
  • 22. Impressive Transcript from Prestigious Institution Impressive Code and Record in Hacker Communities 24 September 2013 University of Virginia cs4414 21
  • 23. Linked Lists in Rust 24 September 2013 University of Virginia cs4414 22
  • 24. What’s a List? 24 September 2013 University of Virginia cs4414 23
  • 25. Linked Lists A List is an object that is either: Null (a special value representing empty list) or a pair whose second part is a List 24 September 2013 University of Virginia cs4414 24 struct Node { head : int, tail : Option<@Node> } type List = Option<@Node> ; Keeping things simple for now! @ = automatically managed
  • 26. 24 September 2013 University of Virginia cs4414 25 let p: List = Some(@Node{head: 1, tail: Some(@Node{head : 2, tail: Some(@Node{head: 3, tail: None})})}); struct Node { head : int, tail : Option<@Node> } type List = Option<@Node> ;
  • 27. to_str 24 September 2013 University of Virginia cs4414 26 struct Node { head : int, tail : Option<@Node> } type List = Option<@Node> ;
  • 28. 24 September 2013 University of Virginia cs4414 27 fn to_str(lst: Option<@Node>) -> ~str { fn elements_to_str(n: @Node) -> ~str { match (n.tail) { None => fmt!("%?", n.head), Some(tail) => fmt!("%?, %s", n.head, elements_to_str(tail)) } } match(lst) { None => ~"Null", Some(n) => fmt!("[%s]", elements_to_str(n)) } }
  • 29. Using Traits 24 September 2013 University of Virginia cs4414 28 trait ToStr { fn to_str (&self) -> ~str; } (ToStr is part of the core) Similar to interface in Java (except Rust traits can include default implementations).
  • 30. 24 September 2013 University of Virginia cs4414 29 impl ToStr for List { fn to_str(&self) -> ~str { fn elements_to_str(n: @Node) -> ~str { match (n.tail) { None => fmt!("%?", n.head), Some(tail) => fmt!("%?, %s", n.head, elements_to_str(tail)) } } match(*self) { None => ~"Null", Some(n) => fmt!("[%s]", elements_to_str(n)) } } } fn main() { let lst : List = Some(@Node{head: 1, tail: Some(@Node{head : 2, tail: Some(@Node{head: 3, tail: None})})}); println(fmt!("%s", lst.to_str())); }
  • 31. Using our List (?) 24 September 2013 University of Virginia cs4414 30 fn main() { let lst : List = Some(@Node{head: 1, tail: Some(@Node{head : 2, tail: Some(@Node{head: 3, tail: None})})}); println(lst.to_str()); lst.head = 0; println(lst.to_str()); }
  • 32. Making it mutable 24 September 2013 University of Virginia cs4414 31 struct Node { head : int, tail : Option<@Node> } type List = Option<@Node> ; Since Rust 0.6 – can’t make struct fields mut struct Node { head : int, tail : Option<@mut Node> } type List = Option<@mut Node> ;
  • 33. 24 September 2013 University of Virginia cs4414 32 fn main() { let lst : List = Some(@mut Node{head: 1, tail: Some(@mut Node{head : 2, tail: Some(@mut Node{head: 3, tail: None})})}); println(lst.to_str()); match lst { None => fail!("Unexpected None!"), Some(n) => n.head = 0 } println(lst.to_str()); }
  • 34. Increment All 24 September 2013 University of Virginia cs4414 33 Write a List method that increments the value of every element of the list.
  • 35. Increment All 24 September 2013 University of Virginia cs4414 34 trait Increment { fn incr(&self); } impl Increment for List { fn incr(&self) { let mut current = *self; loop { match(current) { None => break, Some(node) => { node.head += 1; current = node.tail }, } } } }
  • 36. Mapping 24 September 2013 University of Virginia cs4414 35 self.mapr(|x: int| { x + 1 }) Define a higher-order mapr method that applies a function to all elements in a List.
  • 37. 24 September 2013 University of Virginia cs4414 36 impl Map for List { fn mapr(&self, f: &fn(int) -> int) { let mut current = *self; loop { match(current) { None => break, Some(node) => { node.head = f(node.head); current = node.tail }, } } } }
  • 38. Don’t we want to avoid @? 24 September 2013 University of Virginia cs4414 37
  • 39. 24 September 2013 University of Virginia cs4414 38 struct Node { head : int, tail : Option<~Node> } type List = Option<~Node> ; What else needs to change to make a List with owned Nodes?
  • 40. 24 September 2013 University of Virginia cs4414 39 struct Node { head : int, tail : Option<~Node> } type List = Option<~Node> ; trait Map { fn mapr(&self, &fn(int) -> int) } -> List;
  • 41. 24 September 2013 University of Virginia cs4414 40 struct Node { head : int, tail : Option<~Node> } type List = Option<~Node> ; trait Map { fn mapr(&self, &fn(int) -> int) -> List; } impl Map for List { fn mapr(&self, f: &fn(int) -> int) -> List { match(*self) { None => None, Some(ref node) => { Some(~Node{ head: f(node.head), tail: node.tail.mapr(f) }) }, } } } Is this better or worse than the @mut version?
  • 42. Next class: making map multi-threaded! Read the MapReduce paper (or at least the slides) before Thursday’s class 24 September 2013 University of Virginia cs4414 41 Posted notes (later today) will have all code. I won’t be able to hold my usual office hours this afternoon, but can meet after class today and/or arrange another time.

Editor's Notes

  1. HEAD is your repository (master branch); second is the main repository you tried to pull from