Recap - Basic Score Tracker
Part of the Logic & Flow section of Coddy's Rust journey — lesson 14 of 66.
Challenge
EasyYou will receive two inputs: first, a comma-separated list of test scores, and second, a score to add. Create a vector from the initial scores, add the new score, calculate the average of all scores, and print the results.
Requirements:
- Read the first input containing comma-separated scores (e.g.,
85,92,78) - Split the string by commas and trim each individual token before converting to an integer (e.g., use
.trim()on each element after splitting) - Create a mutable vector from these scores
- Read the second input and convert it to an integer (trim it first)
- Use
.push()to add the new score to the vector - Calculate the average of all scores in the vector
- Print the total number of scores
- Print the average score (as an integer)
Input:
- First line: Comma-separated integers representing initial scores (e.g.,
85,92,78) - Second line: An integer representing the new score to add
Output:
- First line: The total number of scores
- Second line: The average score (as an integer, truncated)
Note: Always call .trim() on the entire input line and on each individual score token after splitting by comma. Input strings may contain invisible whitespace or newline characters that will cause a panic when parsing if not removed.
Try it yourself
use std::io;
fn main() {
// Read the comma-separated scores
let mut scores_input = String::new();
io::stdin().read_line(&mut scores_input).expect("Failed to read line");
// Read the new score to add
let mut new_score_input = String::new();
io::stdin().read_line(&mut new_score_input).expect("Failed to read line");
// TODO: Write your code below
// 1. Split scores_input by commas and convert to integers
// 2. Create a mutable vector from these scores
// 3. Convert new_score_input to an integer
// 4. Push the new score to the vector
// 5. Calculate the average of all scores
// Print the total number of scores
// println!("{}", total_scores);
// Print the average score
// println!("{}", average);
}All lessons in Logic & Flow
1Advanced Control Flow
The 'match' ExpressionMatching Multiple ValuesMatching RangesThe 'if let' ExpressionLoops as ExpressionsRecap - Simple Command Parser4Grouping Data with Structs
What is a Struct?Structs OverviewAccessing Struct FieldsMutable StructsStructs as Function ParametersTuple StructsRecap - Create a Book Struct7Handling Errors with 'Result'
What is a 'Result'?Using 'match' with 'Result'is_ok() and is_err()Shortcuts: unwrap and expectThe Question Mark Operator '?'Parsing Strings to NumbersRecap - Safe Division Function10Closures & Anonymous Functions
What is a Closure?Defining a Simple ClosureClosures with ParametersCapturing the EnvironmentRecap - Simple Adder Closure2Introduction to Vectors
What is a Vector?Creating a VectorAdding Elements with pushAccessing Vector ElementsIterating Over a VectorMutable IterationRemoving ElementsRecap - Basic Score Tracker5Key-Value Pairs with Hash Maps
What is a Hash Map?Creating a Hash MapInserting Key-Value PairsAccessing ValuesIterating Over a Hash MapUpdating a ValueRemoving a PairRecap - Word Counter8Project: Simple Item Inventory
Project SetupAdding an ItemChecking StockSelling an ItemPutting it all together