mirror of https://github.com/01-edu/public.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
eslopfer
548ee49fef
|
2 years ago | |
---|---|---|
.. | ||
README.md | 2 years ago |
README.md
check_user_name
Instructions
Sometimes it is more desirable to catch the failure of some parts of a program instead of just calling panic.
For this exercise you will have to create a tool that manages users' access levels.
You will have to create an AccessLevel
enum which could be Guest
, Normal
, Admin
.
You will also have to create a User
struct which has:
- Fields:
name
:String
acessLevel
:enum
- Associated functions:
new
: which initializes the struct.send_name
: which takes onlyself
as argument and returns anOption<&str>
withNone
if the user is aGuest
or thename
if theAccessLevel
has any of the other options.
- Other functions:
check_user_name
: which takes aUser
, callssend_name
and returns atuple
withtrue
and the username
ifsend_name
returns the name orfalse
and"Error: User is guest"
if not.
Expected Functions and Data Structures
pub enum AccessLevel {}
pub struct User {}
impl User {
pub fn new(name: String, level: AccessLevel) -> User {}
pub fn send_name(&self) -> Option<&str> {}
}
pub fn check_user_name(user: &User) -> (bool, &str) {}
Usage
Here is a program to test your function:
use check_user_name::*;
fn main() {
let user0 = User::new("Didier".to_string(), AccessLevel::Admin);
println!("{:?}", check_user_name(&user0));
let user1 = User::new("Mary".to_string(), AccessLevel::Normal);
println!("{:?}", check_user_name(&user1));
let user2 = User::new("John".to_string(), AccessLevel::Guest);
println!("{:?}", check_user_name(&user2));
}
And its output:
$ cargo run
(true, "Didier")
(true, "Mary")
(false, "ERROR: User is guest")
$