Skip to content
Snippets Groups Projects
Commit 158196b4 authored by Fabio Vandewaeter's avatar Fabio Vandewaeter
Browse files

save

parent d21b83c7
No related branches found
No related tags found
No related merge requests found
......@@ -54,7 +54,7 @@ fn main() {
if input.trim() == password {
println!("Good");
} else {
println!("Wrong");
eprintln!("Wrong");
}
}
```
......@@ -64,19 +64,87 @@ fn main() {
### 2.2.1. Simple function
```rs
/// Check an input for a predetermined secret password
///
/// Returns `Ok(())` if the input matches the password or `Err(())` otherwise.
pub fn check_password(input: &str) -> Result<(), ()> {
let password = "password";
if input.trim() == password {
Ok(())
} else {
Err(())
}
}
```
###
### 2.2.2. Pattern matching
```rs
use std::io;
/// Check an input for a predetermined secret password
///
/// Returns `Ok(())` if the input matches the password or `Err(())` otherwise.
pub fn check_password(input: &str) -> Result<(), ()> {
let password = "password";
if input.trim() == password {
Ok(())
} else {
Err(())
}
}
fn main() {
println!("Enter password:");
let mut input = String::new();
io::stdin().read_line(&mut input).expect("Failed to read line");
match check_password(&input) {
Ok(()) => println!("Good"),
Err(()) => eprintln!("Wrong"),
}
}
```
###
### 2.2.3. Enumeration
```rs
use std::io;
pub enum PasswordError {
Empty,
Wrong
}
/// Check an input for a predetermined secret password
///
/// Returns `Ok(())` if the input matches the password or `Err(())` otherwise.
pub fn check_password(input: &str) -> Result<(), PasswordError> {
let password = "password";
let trimmed_input = input.trim();
if trimmed_input.is_empty(){
Err(PasswordError::Empty)
} else if trimmed_input.trim() == password {
Ok(())
} else {
Err(PasswordError::Wrong)
}
}
fn main() {
println!("Enter password:");
let mut input = String::new();
io::stdin().read_line(&mut input).expect("Failed to read line");
match check_password(&input) {
Ok(()) => println!("Good"),
Err(PasswordError::Wrong) => eprintln!("Error: Wrong password"),
Err(PasswordError::Empty) => eprintln!("Error: password is empty")
}
}
```
###
### 2.2.4. Structured types
```rs
```
......
use std::io;
pub enum PasswordError {
Empty,
Wrong
}
/// A type that can be used to check if a given input is a valid password
pub struct PasswordChecker {
secret: String,
}
/// Check an input for a predetermined secret password
///
/// Returns `Ok(())` if the input matches the password or `Err(())` otherwise.
pub fn check_password(input: &str) -> Result<(), ()> {
let password = "password";
if input.trim() == password {
Ok(())
} else {
Err(())
impl PasswordChecker {
pub fn new(secret: &str) -> Self {
Self {secret: secret.to_string()}
}
pub fn check(&self, input: &str) -> Result<(), PasswordError> {
let trimmed_input = input.trim();
if trimmed_input.is_empty(){
Err(PasswordError::Empty)
} else if trimmed_input.trim() == self.secret{
Ok(())
} else {
Err(PasswordError::Wrong)
}
}
}
......@@ -17,9 +31,12 @@ fn main() {
println!("Enter password:");
let mut input = String::new();
io::stdin().read_line(&mut input).expect("Failed to read line");
let checker = PasswordChecker::new("SuperSecret");
match check_password(&input) {
match checker.check(&input) {
Ok(()) => println!("Good"),
Err(()) => println!("Wrong"),
Err(PasswordError::Wrong) => eprintln!("Error: Wrong password"),
Err(PasswordError::Empty) => eprintln!("Error: password is empty")
}
}
\ No newline at end of file
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment