Activity 25: Diagrams and Trade-offs
Name(s):
Part 1: Draw what crosses the frame
Draw the state at the marked line.
Problem 1
fn process(x: &mut i32, y: &i32) { *x += *y; // DRAW HERE } fn main() { let mut a = 10; let b = 5; process(&mut a, &b); }
Problem 2
fn append_data(list: &mut Vec<i32>, value: i32) { list.push(value); let last = &list[list.len() - 1]; // DRAW HERE } fn main() { let mut numbers = vec![10, 20]; append_data(&mut numbers, 30); }
Part 2: Three versions, all correct
Three people wrote the same function. All three compile and work correctly.
#![allow(unused)] fn main() { // Version A // `mut names` here may be unfamiliar, but it lets us make names mutable // and acts like we did `let mut names = ...` fn add_tag(mut names: Vec<String>) -> Vec<String> { names.push(String::from("new")); names } // called as: let names = add_tag(names); }
#![allow(unused)] fn main() { // Version B fn add_tag(names: &Vec<String>) -> Vec<String> { let mut out = names.clone(); out.push(String::from("new")); out } // called as: let names = add_tag(&names); }
#![allow(unused)] fn main() { // Version C fn add_tag(names: &mut Vec<String>) { names.push(String::from("new")); } // called as: add_tag(&mut names); }
1. Write one pro and one con for each. Think about what happens to the caller's vector afterward, and about how much work happens per call.
| Pro | Con | |
|---|---|---|
| A | ||
| B | ||
| C |
2. Rank them, best first, for a vector holding ten thousand names.
_______ , _______ , _______
3. Does your ranking change if the vector holds three names? Why or why not?
4. Your caller needs the original vector, unchanged, after the call. Which versions still work?
Only B. A took the original away and gave you back a revised one. C changed it in place. B is the expensive one and the only one that meets this requirement.
5. Imagine you are conducting a code review for someone who wrote the version you ranked last. Write a question or observation you could imagine bringing up to the person who wrote it.
Good review comments ask about design choices more than syntax:
- "This clones the whole vector on every call, was that on purpose?"
- "The caller has to rebind the result here, is that what you wanted?"
- "This changes the caller's vector, is that obvious enough at the call site?"
Each of those can be answered with "yes, because..." - what might look wrong to you at first could have a real reason behind it!
Solutions
Part 1
Problem 1
Stack: Heap:
┌─────────────────────────┐
│ process() │ (nothing)
│ ├ x: &mut i32 ──────────┼──┐
│ └ y: &i32 ──────────────┼──┼──┐
├─────────────────────────┤ │ │
│ main() │ │ │
│ ├ a: i32 = 15 (was 10) │◀─┘ │
│ └ b: i32 = 5 │◀────┘
└─────────────────────────┘
Problem 2
Stack: Heap:
┌───────────────────────────┐
│ append_data() │
│ ├ list: &mut Vec<i32> ────┼──┐
│ ├ value: i32 = 30 │ │
│ └ last: &i32 ─────────────┼──┼────────────────────────────────────┐
├───────────────────────────┤ │ │
│ main() │ │ │
│ └ numbers: Vec<i32> │◀─┘ ┌──────┬──────┬──────┬──────┐ │
│ ├ ptr ─────────────────┼──────▶│ 10 │ 20 │ 30 │ │ │
│ ├ len: 3 (was 2) │ └──────┴──────┴──────┴──────┘ │
│ └ cap: 4 (was 2) │ ▲ │
└───────────────────────────┘ (index 2) └─────────────┘
Part 2
All three compile, and all three are defensible.
- A takes the vector and hands it back. Works, but the caller has to rebind:
let names = add_tag(names);. Odd for a function whose whole job is "push one thing" - B is the only one that leaves the caller's vector alone. It's also the only one that copies every
String, so on ten thousand names it's the expensive one - C is what most Rust programmers write, and it's what
Vec::pushitself looks like. The cost: the caller's vector changes, and the&mutat the call site is the only warning
At ten thousand names: C, then A, then B. A moves a handle, B copies ten thousand strings.
At three names, the clone costs almost nothing, so rank on clarity. Any order you can defend is fine.