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:
fn early_return(name: &str, age: i32) -> Result<(), String> {
if name.is_empty() {
return Err("Name is empty".to_string());
}
if age < 0 {
return Err("Invalid age".to_string());
}
Ok(())
}
let result = validate_user(25, "[email protected]");
assert_eq!(result, Ok(()));
let result = validate_user(-1, "[email protected]");
assert_eq!(result, Err("Invalid age".to_string()));
let result = validate_user(25, "userexample.com");
assert_eq!(result, Err("Invalid email".to_string()));
return
keyword to exit the function early when an invalid condition is encountered.contains
method to check if the email contains an '@' symbol.