In many programs, validating user input is a common and crucial task. Ensuring that input data adheres to expected formats and constraints can prevent bugs, enhance security, and improve user experience. In this challenge, you will create a function that validates a user's age and email, returning early if any conditions are not met.
Your task is to implement a function validate_user(age: i32, email: &str) -> Result<(), String>
that validates the user's age and email. The function should follow these rules:
0
or greater than 120
, return an error with the message "Invalid age"
.'@'
symbol, return an error with the message "Invalid email"
.Ok(())
.Here's an example of how to use the early return technique:
return
keyword to exit the function early when an invalid condition is encountered.contains
method to check if the email contains an '@' symbol.