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.sander-b-postnl
struct Email(());struct Age(());impl Email { fn parse(email: &str) -> Result<Self, String> { match email.contains('@') { true => Ok(Self(())), // Usually return the string instead of (). false => Err("Invalid email".to_string()), } }}impl Age { fn parse(age: i32) -> Result<Self, String> { match age { 0..=120 => Ok(Self(())), // Usually return the i32 instead of (). _ => Err("Invalid age".to_string()), } }}pub fn validate_user(age: i32, email: &str) -> Result<(), String> { // Use new type pattern to make this more idiomatic to Rust. // Usually you store the value, however in this case I chose to let it return // a unit struct. Age::parse(age)?; Email::parse(email)?; Ok(())}
savasozturk
pub fn validate_user(age: i32, email: &str) -> Result<(), String> { if age > 120 || age < 0 { return Err("Invalid age".to_string()); } if !email.contains("@") { return Err("Invalid email".to_string()); } return Ok(());}
zavakid
pub fn validate_user(age: i32, email: &str) -> Result<(), String> { // Implement here if age < 0 || age > 120 { return Err("Invalid age".to_string()); } else if !email.contains('@') { return Err("Invalid email".to_string()); } else { Ok(()) }}
mk-comm
pub fn validate_user(age: i32, email: &str) -> Result<(), String> { // Implement here if age < 1 || age >120 { return Err("Invalid age".to_string()) } else if !email.contains("@") { return Err("Invalid email".to_string()) } else { return Ok(()) };}
shmaxuti
pub fn validate_user(age: i32, email: &str) -> Result<(), String> { if age < 0 || age > 120 { return Err(String::from("Invalid age")); } if !email.contains('@') { return Err(String::from("Invalid email")); } Ok(())}
carlos-quantexai
pub fn validate_user(age: i32, email: &str) -> Result<(), String> { // Implement here if age < 0 || age > 120 { return Err("Invalid age".to_string()); } else if !email.contains('@') { return Err("Invalid email".to_string()); } else { Ok(()) }}
jose-bernardo
pub fn validate_user(age: i32, email: &str) -> Result<(), String> { // Implement here if age < 0 || age > 120 { return Err("Invalid age".to_string()); } if !email.contains('@') { return Err("Invalid email".to_string()); } Ok(())}
Mxn-ptr
pub fn validate_user(age: i32, email: &str) -> Result<(), String> { // Implement here if age < 0 || age > 120 { return Err("Invalid age".to_string()); } if !email.contains("@") { return Err("Invalid email".to_string()); } Ok(())}
hinphansa
pub fn validate_user(age: i32, email: &str) -> Result<(), String> { if age < 0 || age > 120 { return Err("Invalid age".to_string()); } if !email.contains('@') { return Err("Invalid email".to_string()); } Ok(())}
funny233-github
pub fn validate_user(age: i32, email: &str) -> Result<(), String> { // Implement here if age < 0 || age > 120 { return Err(format!("Invalid age")); } if email.find('@').is_none() { return Err(format!("Invalid email")); } Ok(())}
digitalresistor
pub fn validate_user(age: i32, email: &str) -> Result<(), String> { if age > 120 || age < 0 { return Err("Invalid age".to_string()); } if !email.contains("@") { return Err("Invalid email".to_string()); } Ok(())}
majesticalcreature
pub fn validate_user(age: i32, email: &str) -> Result<(), String> { // Implement here if age < 0 || age > 120 { return Err(String::from("Invalid age")); } if !email.contains("@") { return Err(String::from("Invalid email")); } Ok(())}
jhq223
pub fn validate_user(age: i32, email: &str) -> Result<(), String> { if age < 0 || age > 120 { return Err("Invalid age".to_string()); } if !email.contains("@") { return Err("Invalid email".to_string()); } Ok(())}
xbarnett
pub fn validate_user(age: i32, email: &str) -> Result<(), String> { if age < 0 || age > 120 { return Err(String::from("Invalid age")); } if !email.contains('@') { return Err(String::from("Invalid email")); } Ok(())}
Ignition
pub fn validate_user(age: i32, email: &str) -> Result<(), String> { if age < 0 || age > 120 { return Err("Invalid age".to_string()); } if !email.contains('@') { return Err("Invalid email".to_string()); } Ok(())}
matei
pub fn validate_user(age: i32, email: &str) -> Result<(), String> { if age < 0 || age > 120 { return Err("Invalid age".to_string()); } if !email.contains("@") { return Err("Invalid email".to_string()); } return Ok(());}
oneopane
pub fn validate_user(age: i32, email: &str) -> Result<(), String> { if age < 0 || age > 120 { return Err("Invalid age".to_string()) } if !email.contains("@") { return Err("Invalid email".to_string()) } Ok(())}
CianciuStyles
pub fn validate_user(age: i32, email: &str) -> Result<(), String> { // Implement here if age < 0 || age > 120 { return Err("Invalid age".to_string()); } if !email.contains('@') { return Err("Invalid email".to_string()); } Ok(())}
pbjarterot
pub fn validate_user(age: i32, email: &str) -> Result<(), String> { // Implement here if age < 0 || age > 120 { return Err("Invalid age".to_string()); } if !email.contains("@") { return Err("Invalid email".to_string()); } return Ok(())}
kyhou
pub fn validate_user(age: i32, email: &str) -> Result<(), String> { if !(0..120).contains(&age) { return Err("Invalid age".to_string()); } if !email.contains('@') { return Err("Invalid email".to_string()); } Ok(())}
ayushrawat10
pub fn validate_user(age: i32, email: &str) -> Result<(), String> { if age < 0 || age > 120 { return Err("Invalid age".to_string()); } if !email.contains("@") { return Err("Invalid email".to_string()); } Ok(())}
Aditeya
pub fn validate_user(age: i32, email: &str) -> Result<(), String> { // Implement here if age < 0 || age > 120 { return Err("Invalid age".into()); } if !email.contains("@") { return Err("Invalid email".into()); } Ok(())}
masteryachty
pub fn validate_user(age: i32, email: &str) -> Result<(), String> { if age < 0 || age > 120 { return Err("Invalid age".to_string()); } if email.is_empty() || !email.contains("@") { return Err("Invalid email".to_string()); } Ok(())}
morigs
pub fn validate_user(age: i32, email: &str) -> Result<(), String> { if age < 0 || age > 120 { return Err("Invalid age".to_string()); } if !email.contains("@") { return Err("Invalid email".to_string()); } Ok(())}
damascussteel21
pub fn validate_user(age: i32, email: &str) -> Result<(), String> { // Implement here if age < 0 || age > 120 { Err(String::from("Invalid age")) } else if !email.chars().any(|c| c == '@') { Err(String::from("Invalid email")) } else { Ok(()) }}
edgarcnp
pub fn validate_user(age: i32, email: &str) -> Result<(), String> { if age < 0 || age > 120 { return Err("Invalid age".to_string()); } if !email.contains('@') { return Err("Invalid email".to_string()); } Ok(())}
jtruong04
pub fn validate_user(age: i32, email: &str) -> Result<(), String> { // Implement here if age < 0 || age > 120 { return Err("Invalid age".to_string()); } if !email.contains('@') { return Err("Invalid email".to_string()); } Ok(())}
jaswgreen
pub fn validate_user(age: i32, email: &str) -> Result<(), String> { if age < 0 || age > 120 { return Err("Invalid age".to_string()); } if !email.contains("@") { return Err("Invalid email".to_string()); } Ok(())}
CarrotTarrot
pub fn validate_user(age: i32, email: &str) -> Result<(), String> { if age < 0 || age > 120 { return Err("Invalid age".into()) } if !email.contains("@") { return Err("Invalid email".into()) } Ok(())}
Sommos
pub fn validate_user(age: i32, email: &str) -> Result<(), String> { // Implement here if age < 0 || age > 120 { return Err(String::from("Invalid age")); } if !email.contains("@") { return Err(String::from("Invalid email")); } Ok(())}
xyres137
pub fn validate_user(age: i32, email: &str) -> Result<(), String> { if age < 0 || age > 120 { return Err(String::from("Invalid age")); } if !&email.contains("@") { return Err(String::from("Invalid email")); } Ok(())}
tukantje
pub fn validate_user(age: i32, email: &str) -> Result<(), String> { if age > 120 || age < 0 { return Err("Invalid age".to_string()); } if !email.contains('@') { return Err("Invalid email".to_string()); } Ok(())}
wischi-chr
pub fn validate_user(age: i32, email: &str) -> Result<(), String> { if age < 0 || age > 120 { return Err("Invalid age".to_string()); } if !email.contains('@') { return Err("Invalid email".to_string()); } Ok(())}
hilias
pub fn validate_user(age: i32, email: &str) -> Result<(), String> { // Implement here if age < 0 || age > 120 { Err("Invalid age".to_string()) } else if !email.chars().any(|c| c == '@') { Err("Invalid email".to_string()) } else { Ok(()) }}
qcabanes-hobby
pub fn validate_user(age: i32, email: &str) -> Result<(), String> { if age < 0 || age > 120 { return Err("Invalid age".to_string()); } if !email.contains("@") { return Err("Invalid email".to_string()); } Ok(())}
TaiPoole
pub fn validate_user(age: i32, email: &str) -> Result<(), String> { if age < 0 || age > 120 { return Err("Invalid age".to_string()) } if !email.chars().any(|x| x == '@') { return Err("Invalid email".to_string()) } Ok(())}
TomBaston
pub fn validate_user(age: i32, email: &str) -> Result<(), String> { if age < 0 || age > 120 { return Err("Invalid age".into()); } if !email.contains('@') { return Err("Invalid email".into()); } Ok(())}
swandyr
pub fn validate_user(age: i32, email: &str) -> Result<(), String> { // Implement here if age < 0 || age > 120 { return Err("Invalid age".into()); } if !email.contains("@") { return Err("Invalid email".into()); } Ok(())}
0x006E
pub fn validate_user(age: i32, email: &str) -> Result<(), String> { if age < 0 || age > 120 { return Err("Invalid age".to_string()); } if !email.contains("@") { return Err("Invalid email".to_string()); } Ok(())}
vineelkovvuri
pub fn validate_user(age: i32, email: &str) -> Result<(), String> { // Implement here if age < 0 || age > 120 { return Err("Invalid age".to_string()); } if !email.contains("@") { return Err("Invalid email".to_string()); } Ok(())}
Andrey2ec
pub fn validate_user(age: i32, email: &str) -> Result<(), String> { // Implement here match (age,email.contains("@")) { (age, _) if age < 0 || age > 120 => Err("Invalid age".to_string()), (_, false) => Err("Invalid email".to_string()), _ => Ok(()) } }
elhananby
pub fn validate_user(age: i32, email: &str) -> Result<(), String> { if !(0..=120).contains(&age) { return Err("Invalid age".to_string()); } if !email.contains("@") { return Err("Invalid email".to_string()); } // if name.is_empty() { // return Err("Name is empty".to_string()); // } Ok(())}
2daysagokid
pub fn validate_user(age: i32, email: &str) -> Result<(), String> { match (age,email.contains("@")) { (age, _) if age < 0 || age > 120 => Err("Invalid age".to_string()), (_, false) => Err("Invalid email".to_string()), _ => Ok(()) }}
dantekelly
pub fn validate_user(age: i32, email: &str) -> Result<(), String> { // Implement here match age { _ if age < 0 || age > 120 => Err("Invalid age".to_string()), _ if !email.contains("@") => Err("Invalid email".to_string()), _ => Ok(()) }}
philippthiers
pub fn validate_user(age: i32, email: &str) -> Result<(), String> { // Implement here if age < 0 || age > 120 { return Err("Invalid age".to_string()); } if !email.to_string().contains("@") { return Err("Invalid email".to_string()); } Ok(())}
SRVng
pub fn validate_user(age: i32, email: &str) -> Result<(), String> { if age.le(&0) || age.gt(&120) { return Err("Invalid age".to_string()) } if !email.contains("@") { return Err("Invalid email".to_string()) } Ok(())}
dantekelly
pub fn validate_user(age: i32, email: &str) -> Result<(), String> { // Implement here match (age, email) { _ if age < 0 || age > 120 => Err("Invalid age".to_string()), _ if !email.contains("@") => Err("Invalid email".to_string()), _ => Ok(()) }}
manavhirani
pub fn validate_user(age: i32, email: &str) -> Result<(), String> { // Implement here if age < 0 || age > 120 { Err("Invalid age".to_string()) } else if !email.contains("@") { Err("Invalid email".to_string()) } else { Ok(()) }}
jenny07007
pub fn validate_user(age: i32, email: &str) -> Result<(), String> { match age { 0..=120 => {}, _ => return Err("Invalid age".to_string()), } match email.contains('@') { true => {}, false => return Err("Invalid email".to_string()), } Ok(())}
denbailey
pub fn validate_user(age: i32, email: &str) -> Result<(), String> { // Implement here if email.is_empty(){ return Err("Email is Empty".to_string()); } if age < 0 || age > 120 { return Err("Invalid age".to_string()); } if email.contains('@') == false { return Err("Invalid email".to_string()); } Ok(())}