SlideShare a Scribd company logo
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

PPTX
What the &~#@&lt;!? (Memory Management in Rust)
PPTX
Multi-Tasking Map (MapReduce, Tasks in Rust)
PPTX
First Ride on Rust
PPTX
Smarter Scheduling
PPTX
RR & Docker @ MuensteR Meetup (Sep 2017)
PPTX
Segmentation Faults, Page Faults, Processes, Threads, and Tasks
PDF
Tracing and awk in ns2
PPTX
2013 0928 programming by cuda
What the &~#@&lt;!? (Memory Management in Rust)
Multi-Tasking Map (MapReduce, Tasks in Rust)
First Ride on Rust
Smarter Scheduling
RR & Docker @ MuensteR Meetup (Sep 2017)
Segmentation Faults, Page Faults, Processes, Threads, and Tasks
Tracing and awk in ns2
2013 0928 programming by cuda

What's hot (8)

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

Viewers also liked (20)

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

Similar to Using Git, Pointers in Rust (20)

PDF
Research @ RELEASeD (presented at SATTOSE2013)
PPTX
Virtual Memory (Making a Process)
PPTX
Assignment of pseudo code
PPTX
Access Control
PDF
R programming for data science
PDF
Reducing Redundancies in Multi-Revision Code Analysis
PDF
Introduction to the R Statistical Computing Environment
PDF
cb streams - gavin pickin
PPTX
GRAPHICAL STRUCTURES in our lives
PDF
Web Traffic Time Series Forecasting
PDF
Functional Reactive Programming on Android
PPTX
Making a Process
PDF
Neo4j after 1 year in production
PDF
Nzitf Velociraptor Workshop
PPT
Introduction to Git for developers
PDF
Custom Pregel Algorithms in ArangoDB
PDF
A Lightweight Infrastructure for Graph Analytics
PDF
Graph Analytics with ArangoDB
PDF
Introduction to Twitter Storm
PDF
Distributed Graph Algorithms
Research @ RELEASeD (presented at SATTOSE2013)
Virtual Memory (Making a Process)
Assignment of pseudo code
Access Control
R programming for data science
Reducing Redundancies in Multi-Revision Code Analysis
Introduction to the R Statistical Computing Environment
cb streams - gavin pickin
GRAPHICAL STRUCTURES in our lives
Web Traffic Time Series Forecasting
Functional Reactive Programming on Android
Making a Process
Neo4j after 1 year in production
Nzitf Velociraptor Workshop
Introduction to Git for developers
Custom Pregel Algorithms in ArangoDB
A Lightweight Infrastructure for Graph Analytics
Graph Analytics with ArangoDB
Introduction to Twitter Storm
Distributed Graph Algorithms

More from David Evans (20)

PPTX
Cryptocurrency Jeopardy!
PPTX
Trick or Treat?: Bitcoin for Non-Believers, Cryptocurrencies for Cypherpunks
PPTX
Hidden Services, Zero Knowledge
PPTX
Anonymity in Bitcoin
PPTX
Midterm Confirmations
PPTX
Scripting Transactions
PPTX
How to Live in Paradise
PPTX
Bitcoin Script
PPTX
Mining Economics
PPTX
Mining
PPTX
The Blockchain
PPTX
Becoming More Paranoid
PPTX
Asymmetric Key Signatures
PPTX
Introduction to Cryptography
PPTX
Class 1: What is Money?
PPTX
Multi-Party Computation for the Masses
PPTX
Proof of Reserve
PPTX
Silk Road
PPTX
Blooming Sidechains!
PPTX
Useful Proofs of Work, Permacoin
Cryptocurrency Jeopardy!
Trick or Treat?: Bitcoin for Non-Believers, Cryptocurrencies for Cypherpunks
Hidden Services, Zero Knowledge
Anonymity in Bitcoin
Midterm Confirmations
Scripting Transactions
How to Live in Paradise
Bitcoin Script
Mining Economics
Mining
The Blockchain
Becoming More Paranoid
Asymmetric Key Signatures
Introduction to Cryptography
Class 1: What is Money?
Multi-Party Computation for the Masses
Proof of Reserve
Silk Road
Blooming Sidechains!
Useful Proofs of Work, Permacoin

Recently uploaded (20)

PDF
Machine learning based COVID-19 study performance prediction
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
1. Introduction to Computer Programming.pptx
PDF
cuic standard and advanced reporting.pdf
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
A Presentation on Artificial Intelligence
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PPT
Teaching material agriculture food technology
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
MIND Revenue Release Quarter 2 2025 Press Release
Machine learning based COVID-19 study performance prediction
“AI and Expert System Decision Support & Business Intelligence Systems”
Mobile App Security Testing_ A Comprehensive Guide.pdf
1. Introduction to Computer Programming.pptx
cuic standard and advanced reporting.pdf
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Programs and apps: productivity, graphics, security and other tools
A Presentation on Artificial Intelligence
20250228 LYD VKU AI Blended-Learning.pptx
Assigned Numbers - 2025 - Bluetooth® Document
Per capita expenditure prediction using model stacking based on satellite ima...
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
Teaching material agriculture food technology
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Advanced methodologies resolving dimensionality complications for autism neur...
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Group 1 Presentation -Planning and Decision Making .pptx
Reach Out and Touch Someone: Haptics and Empathic Computing
MIND Revenue Release Quarter 2 2025 Press Release

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

  • #16: HEAD is your repository (master branch); second is the main repository you tried to pull from