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 program
  • person.rs - Person struct and methods
  • storage.rs - Functions for managing a collection of people
  • stats.rs - Functions for computing statistics

Steps:

  1. Match signatures to bodies - Each uppercase letter [A] has a corresponding body with a lowercase letter. Find the matches!

  2. Sort the complete snippets into files (main is already done!) In lieu of tape/glue you can write the letter pairs (eg [A][m]) on the correct paper to record your solution.

  3. Mark what's public - write pub on things that need to be accessed from other files.

  4. Write use and mod statements

    • At the top of main.rs, write the use and mod statements needed to connect main to the other modules
    • Some modules may need to import from other modules too!

SIGNATURES (cut these out):

┌─────────────────────────────────────────┐ │ [A] │ │ fn list_names(people: &Vec) │ │ -> Vec { │ └─────────────────────────────────────────┘

┌─────────────────────────────────────────┐ │ [B] │ │ impl Person { │ │ fn get_score(&self) -> f64 { │ └─────────────────────────────────────────┘

┌─────────────────────────────────────────┐ │ [C] │ │ fn validate_age(age: i32) -> bool { │ └─────────────────────────────────────────┘

┌─────────────────────────────────────────┐ │ [D] │ │ fn highest_score(people: &Vec) │ │ -> f64 { │ └─────────────────────────────────────────┘

┌─────────────────────────────────────────┐ │ [E] │ │ impl Person { │ │ fn new(name: String, age: i32, │ │ score: f64) -> Person { │ └─────────────────────────────────────────┘

┌─────────────────────────────────────────┐ │ [F] │ │ fn count_people(people: &Vec) │ │ -> usize { │ └─────────────────────────────────────────┘

┌─────────────────────────────────────────┐ │ [G] │ │ const MIN_PASSING_SCORE: f64 = 60.0; │ └─────────────────────────────────────────┘

┌─────────────────────────────────────────┐ │ [H] │ │ struct Person { │ │ name: String, │ │ age: i32, │ │ score: f64, │ └─────────────────────────────────────────┘

┌─────────────────────────────────────────┐ │ [I] │ │ fn add_person(people: &mut Vec, │ │ person: Person) { │ └─────────────────────────────────────────┘

┌─────────────────────────────────────────┐ │ [J] │ │ fn compute_average(sum: f64, │ │ count: usize) │ │ -> f64 { │ └─────────────────────────────────────────┘

┌─────────────────────────────────────────┐ │ [K] │ │ impl Person { │ │ fn get_age(&self) -> i32 { │ └─────────────────────────────────────────┘

┌─────────────────────────────────────────┐ │ [L] │ │ fn average_score(people: &Vec) │ │ -> f64 { │ └─────────────────────────────────────────┘

┌─────────────────────────────────────────┐ │ [M] │ │ fn format_person(p: &Person) -> String {│ └─────────────────────────────────────────┘

┌─────────────────────────────────────────┐ │ [N] │ │ impl Person { │ │ fn get_name(&self) -> &str { │ └─────────────────────────────────────────┘

┌─────────────────────────────────────────┐ │ [O] │ │ fn count_passing(people: &Vec) │ │ -> usize { │ └─────────────────────────────────────────┘

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));
}



person.rs

storage.rs

stats.rs