Recap - User Profile Validator
Part of the Logic & Flow section of Coddy's Rust journey — lesson 65 of 66.
Challenge
EasyYou will receive three inputs representing a user profile. The first input is a username, the second is an age, and the third is an email address. Validate the profile according to the following rules and print the appropriate message:
Validation Rules:
- Username must be at least 3 characters long
- Age must be between 13 and 120 (inclusive)
- Email must contain the
@symbol
Requirements:
- Read the username input and trim whitespace
- Read the age input, trim whitespace, and parse it to
i32 - Read the email input and trim whitespace
- Check if the username length is at least 3 characters
- Check if the age is in the range 13 to 120 (use
matchwith ranges) - Check if the email contains the
@character (use the.contains()method) - If all validations pass, print:
Valid profile - If the username is too short, print:
Invalid username - If the age is out of range, print:
Invalid age - If the email doesn't contain
@, print:Invalid email - Check validations in order: username first, then age, then email
Input:
- First line: A username (e.g.,
alice123) - Second line: An age (e.g.,
25) - Third line: An email address (e.g.,
alice@example.com)
Output:
- One of:
Valid profile,Invalid username,Invalid age, orInvalid email
Try it yourself
use std::io;
fn main() {
// Read username input
let mut username = String::new();
io::stdin().read_line(&mut username).expect("Failed to read line");
let username = username.trim();
// Read age input
let mut age_input = String::new();
io::stdin().read_line(&mut age_input).expect("Failed to read line");
let age: i32 = age_input.trim().parse().expect("Failed to parse age");
// Read email input
let mut email = String::new();
io::stdin().read_line(&mut email).expect("Failed to read line");
let email = email.trim();
// TODO: Write your code below to validate the profile
// Check username length, age range, and email format
// Print the appropriate message based on validation results
}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