Activity 27 - Module Organization Puzzle
Group members:
Your Challenge:
You have a pile of code snippets that need to be organized into 4 files:
main.rs- The main programperson.rs- Person struct and methodsstorage.rs- Functions for managing a collection of peoplestats.rs- Functions for computing statistics
Steps:
-
Match signatures to bodies - Each uppercase letter [A] has a corresponding body with a lowercase letter. Find the matches!
-
Sort the complete snippets into files (
mainis already done!) In lieu of tape/glue you can write the letter pairs (eg [A][m]) on the correct paper to record your solution. -
Mark what's public - write
pubon things that need to be accessed from other files. -
Write
useandmodstatements- At the top of
main.rs, write theuseandmodstatements needed to connectmainto the other modules - Some modules may need to import from other modules too!
- At the top of
SIGNATURES (cut these out):
┌─────────────────────────────────────────┐
│ [A] │
│ fn list_names(people: &Vec
┌─────────────────────────────────────────┐ │ [B] │ │ impl Person { │ │ fn get_score(&self) -> f64 { │ └─────────────────────────────────────────┘
┌─────────────────────────────────────────┐ │ [C] │ │ fn validate_age(age: i32) -> bool { │ └─────────────────────────────────────────┘
┌─────────────────────────────────────────┐
│ [D] │
│ fn highest_score(people: &Vec
┌─────────────────────────────────────────┐ │ [E] │ │ impl Person { │ │ fn new(name: String, age: i32, │ │ score: f64) -> Person { │ └─────────────────────────────────────────┘
┌─────────────────────────────────────────┐
│ [F] │
│ fn count_people(people: &Vec
┌─────────────────────────────────────────┐ │ [G] │ │ const MIN_PASSING_SCORE: f64 = 60.0; │ └─────────────────────────────────────────┘
┌─────────────────────────────────────────┐ │ [H] │ │ struct Person { │ │ name: String, │ │ age: i32, │ │ score: f64, │ └─────────────────────────────────────────┘
┌─────────────────────────────────────────┐
│ [I] │
│ fn add_person(people: &mut Vec
┌─────────────────────────────────────────┐ │ [J] │ │ fn compute_average(sum: f64, │ │ count: usize) │ │ -> f64 { │ └─────────────────────────────────────────┘
┌─────────────────────────────────────────┐ │ [K] │ │ impl Person { │ │ fn get_age(&self) -> i32 { │ └─────────────────────────────────────────┘
┌─────────────────────────────────────────┐
│ [L] │
│ fn average_score(people: &Vec
┌─────────────────────────────────────────┐ │ [M] │ │ fn format_person(p: &Person) -> String {│ └─────────────────────────────────────────┘
┌─────────────────────────────────────────┐ │ [N] │ │ impl Person { │ │ fn get_name(&self) -> &str { │ └─────────────────────────────────────────┘
┌─────────────────────────────────────────┐
│ [O] │
│ fn count_passing(people: &Vec
BODIES (cut these out, they're scrambled!):
┌─────────────────────────────────────────┐ │ [a] │ │ } │ └─────────────────────────────────────────┘
┌─────────────────────────────────────────┐ │ [b] │ │ self.age │ │ } │ │ } │ └─────────────────────────────────────────┘
┌─────────────────────────────────────────┐ │ [c] │ │ if validate_age(age) { │ │ Person { name, age, score } │ │ } else { │ │ panic!("Invalid age"); │ │ } │ │ } │ │ } │ └─────────────────────────────────────────┘
┌─────────────────────────────────────────┐ │ [d] │ │ people.iter() │ │ .filter(|p| p.get_score() │ │ >= MIN_PASSING_SCORE)│ │ .count() │ │ } │ └─────────────────────────────────────────┘
┌─────────────────────────────────────────┐ │ [e] │ │ &self.name │ │ } │ │ } │ └─────────────────────────────────────────┘
┌─────────────────────────────────────────┐ │ [f] │ │ sum / count as f64 │ │ } │ └─────────────────────────────────────────┘
┌─────────────────────────────────────────┐ │ [g] │ │ people.iter() │ │ .map(|p| p.get_name() │ │ .to_string()) │ │ .collect() │ │ } │ └─────────────────────────────────────────┘
┌─────────────────────────────────────────┐ │ [h] │ │ people.len() │ │ } │ └─────────────────────────────────────────┘
┌─────────────────────────────────────────┐ │ [i] │ │ let sum: f64 = people.iter() │ │ .map(|p| p.get_score()) │ │ .sum(); │ │ compute_average(sum, │ │ people.len()) │ │ } │ └─────────────────────────────────────────┘
┌─────────────────────────────────────────┐ │ [j] │ │ people.iter() │ │ .map(|p| p.get_score()) │ │ .max_by(|a, b| │ │ a.partial_cmp(b) │ │ .unwrap()) │ │ .unwrap() │ │ } │ └─────────────────────────────────────────┘
┌─────────────────────────────────────────┐ │ [k] │ │ age > 0 && age < 150 │ │ } │ └─────────────────────────────────────────┘
┌─────────────────────────────────────────┐ │ [l] │ │ self.score │ │ } │ │ } │ └─────────────────────────────────────────┘
┌─────────────────────────────────────────┐ │ [m] │ │ people.push(person); │ │ } │ └─────────────────────────────────────────┘
┌─────────────────────────────────────────┐ │ [n] │ └─────────────────────────────────────────┘
┌─────────────────────────────────────────┐ │ [o] │ │ format!("{} (age {}, score: {:.1})",│ │ p.get_name(), │ │ p.get_age(), │ │ p.get_score()) │ │ } │ └─────────────────────────────────────────┘
main.rs
use and mod statements here:
fn main(){
let mut people = Vec::new();
let alice = Person::new("Alice".to_string(), 25, 92.5);
let bob = Person::new("Bob".to_string(), 30, 87.0);
add_person(&mut people, alice);
add_person(&mut people, bob);
println!("Count: {}", count_people(&people));
println!("Names: {:?}", list_names(&people));
println!("Average: {:.1}", average_score(&people));
println!("Highest: {:.1}", highest_score(&people));
println!("Passing: {}", count_passing(&people));
}