In this challenge, you will use Rust's pattern matching capabilities to determine the name of a weekday given its corresponding number. Each day of the week will be represented by a number from 1 to 7, where 1 corresponds to Monday and 7 corresponds to Sunday.
This is a common task that can be very useful in various applications, such as scheduling systems or calendar utilities. You will practice working with Rust's match
statement to map numbers to their respective weekdays.
Your task is to implement a function, weekday_from_number(day: u8) -> &'static str
, that takes a number as input and returns the corresponding weekday as a string. If the input number is not within the range of 1 to 7, the function should return "Invalid day number".
let result = weekday_from_number(1);
assert_eq!(result, "Monday");
let result = weekday_from_number(5);
assert_eq!(result, "Friday");
let result = weekday_from_number(8);
assert_eq!(result, "Invalid day number");
match
statement to map each number to its corresponding weekday.&'static str
denotes a string slice that lives for the entire duration of the programpub fn weekday_from_number(day: u8) -> &'static str { match day { 1 => "Monday", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday", 6 => "Saturday", 7 => "Sunday", _ => "Invalid day number" }}
pub fn weekday_from_number(day: u8) -> &'static str { // TODO: Implement the function here match day { 1 => "Monday", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday", 6 => "Saturday", 7 => "Sunday", _ => "Invalid day number" }}
pub fn weekday_from_number(day: u8) -> &'static str { // TODO: Implement the function here If the input number is 1, the function should return "Monday".// If the input number is 2, the function should return "Tuesday".// If the input number is 3, the function should return "Wednesday".// If the input number is 4, the function should return "Thursday".// If the input number is 5, the function should return "Friday".// If the input number is 6, the function should return "Saturday".// If the input number is 7, the function should return "Sunday".// If the input number is not between 1 and 7, the function should return "Invalid day number". match day{ 1 => "Monday", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday", 6 => "Saturday", 7 => "Sunday", _ => "Invalid day number", }}
pub fn weekday_from_number(day: u8) -> &'static str { // TODO: Implement the function here match day { 1 => "Monday", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday", 6 => "Saturday", 7 => "Sunday", _ => "Invalid day number", }}
pub fn weekday_from_number(day: u8) -> &'static str { match day { 1 => "Monday", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday", 6 => "Saturday", 7 => "Sunday", _ => "Invalid day number", }}
pub fn weekday_from_number(day: u8) -> &'static str { match day { 1 => "Monday", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday", 6 => "Saturday", 7 => "Sunday", _ => "Invalid day number" }}
pub fn weekday_from_number(day: u8) -> &'static str { // TODO: Implement the function here match day { 1 => "Monday", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday", 6 => "Saturday", 7 => "Sunday", _ => "Invalid day number", }}
pub fn weekday_from_number(day: u8) -> &'static str { // TODO: Implement the function here match day { 1 => "Monday", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday", 6 => "Saturday", 7 => "Sunday", _ => "Invalid day number", }}
pub fn weekday_from_number(day: u8) -> &'static str { match day { 1 => "Monday", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday", 6 => "Saturday", 7 => "Sunday", _ => "Invalid day number", }}
pub fn weekday_from_number(day: u8) -> &'static str { return match day { 1 => "Monday", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday", 6 => "Saturday", 7 => "Sunday", _ => "Invalid day number" }}
pub fn weekday_from_number(day: u8) -> &'static str { // TODO: Implement the function here match day { 1 => "Monday", 2 => "Tuesday", 3=> "Wednesday", 4=> "Thursday", 5=> "Friday", 6=> "Saturday", 7=> "Sunday", _=> "Invalid day number" }}
pub fn weekday_from_number(day: u8) -> &'static str { // TODO: Implement the function here match day { 1 => "Monday", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday", 6 => "Saturday", 7 => "Sunday", _ => "Invalid day number" }}
pub fn weekday_from_number(day: u8) -> &'static str { match day { 1 => "Monday", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday", 6 => "Saturday", 7 => "Sunday", _ => "Invalid day number" }}
pub fn weekday_from_number(day: u8) -> &'static str { // TODO: Implement the function here match day { 1 =>"Monday", 2 =>"Tuesday", 3 =>"Wednesday", 4 => "Thursday", 5 => "Friday", 6 => "Saturday", 7 => "Sunday", _ => "Invalid day number", }}
pub fn weekday_from_number(day: u8) -> &'static str { // TODO: Implement the function here match day { 1 => "Monday", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday", 6 => "Saturday", 7 => "Sunday", _ => "Invalid day number" }}
pub fn weekday_from_number(day: u8) -> &'static str { match day { 1 => "Monday", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday", 6 => "Saturday", 7 => "Sunday", _ => "Invalid day number", }}
pub fn weekday_from_number(day: u8) -> &'static str { // TODO: Implement the function here match day { 1 => "Monday", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday", 6 => "Saturday", 7 => "Sunday", _ => "Invalid day number", }}
pub fn weekday_from_number(day: u8) -> &'static str { match day { 1 => "Monday", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday", 6 => "Saturday", 7 => "Sunday", _ => "Invalid day number", }}
pub fn weekday_from_number(day: u8) -> &'static str { // TODO: Implement the function match day{ 1=>"Monday", 2=>"Tuesday", 3=>"Wednesday", 4=>"Thursday", 5=>"Friday", 6=>"Saturday", 7=>"Sunday", _=>"Invalid day number" }}
pub fn weekday_from_number(day: u8) -> &'static str { match day { 1 => "Monday", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday", 6 => "Saturday", 7 => "Sunday", _ => "Invalid day number" }}
pub fn weekday_from_number(day: u8) -> &'static str { // TODO: Implement the function here match day { 1 => "Monday", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday", 6 => "Saturday", 7 => "Sunday", _ => "Invalid day number" }}
pub fn weekday_from_number(day: u8) -> &'static str { // TODO: Implement the function here match day { 1 => "Monday", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday", 6 => "Saturday", 7 => "Sunday", _ => "Invalid day number", }}
pub fn weekday_from_number(day: u8) -> &'static str { // TODO: Implement the function here match day { 1 => "Monday", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday", 6 => "Saturday", 7 => "Sunday", _ => "Invalid day number", }}
pub fn weekday_from_number(day: u8) -> &'static str { // TODO: Implement the function here match day { 1 => "Monday", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday", 6 => "Saturday", 7 => "Sunday", _ => "Invalid day number", }}
pub fn weekday_from_number(day: u8) -> &'static str { // TODO: Implement the function here match day { 1 => "Monday", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday", 6 => "Saturday", 7 => "Sunday", _ => "Invalid day number" }}
pub fn weekday_from_number(day: u8) -> &'static str { // TODO: Implement the function here let arr = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]; match day { s if s < 1 || s > 7 => return "Invalid day number", _ => return arr[(day - 1) as usize], }}
pub fn weekday_from_number(day: u8) -> &'static str { // TODO: Implement the function here let ret = match day { 1 => "Monday", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday", 6 => "Saturday", 7 => "Sunday", _ => "Invalid day number", }; return ret;}
pub fn weekday_from_number(day: u8) -> &'static str { // TODO: Implement the function here match day { 1 => return "Monday", 2 => return "Tuesday", 3 => return "Wednesday", 4 => return "Thursday", 5 => return "Friday", 6 => return "Saturday", 7 => return "Sunday", _ => return "Invalid day number", }}
pub fn weekday_from_number(day: u8) -> &'static str { match day { 1 => "Monday", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday", 6 => "Saturday", 7 => "Sunday", _ => "Invalid day number", }}
pub fn weekday_from_number(day: u8) -> &'static str { match day { 1 => "Monday", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday", 6 => "Saturday", 7 => "Sunday", _ => "Invalid day number", }}
pub fn weekday_from_number(day: u8) -> &'static str { // TODO: Implement the function here let day_as_string = match day { 1 => "Monday", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday", 6 => "Saturday", 7 => "Sunday", _ => "Invalid day number" }; day_as_string}
pub fn weekday_from_number(day: u8) -> &'static str { // TODO: Implement the function here match day { 1 =>"Monday", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday", 6 => "Saturday", 7 => "Sunday", _ => "Invalid day number", }}
pub fn weekday_from_number(day: u8) -> &'static str { match day{ 1 => "Monday", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday", 6 => "Saturday", 7 => "Sunday", _ => "Invalid day number", } // TODO: Implement the function here}
pub fn weekday_from_number(day: u8) -> &'static str { // TODO: Implement the function here match day { 1 => "Monday", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday", 6 => "Saturday", 7 => "Sunday", _=>"Invalid day number" }}
pub fn weekday_from_number(day: u8) -> &'static str { // TODO: Implement the function here match day { 1 => "Monday", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday", 6 => "Saturday", 7 => "Sunday", _ => "Invalid day number" }}
pub fn weekday_from_number(day: u8) -> &'static str { // TODO: Implement the function here match day { 1 => "Monday", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday", 6 => "Saturday", 7 => "Sunday", 0 | 8.. => "Invalid day number" }}
pub fn weekday_from_number(day: u8) -> &'static str { match day { 1 => "Monday", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday", 6 => "Saturday", 7 => "Sunday", 0 | 8.. => "Invalid day number" }}
pub fn weekday_from_number(day: u8) -> &'static str { // TODO: Implement the function here match day { 1 => "Monday", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday", 6 => "Saturday", 7 => "Sunday", _ => "Invalid day number", }}
pub fn weekday_from_number(day: u8) -> &'static str { match day { 1 => "Monday", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday", 6 => "Saturday", 7 => "Sunday", _ => "Invalid day number", }}
pub fn weekday_from_number(day: u8) -> &'static str { // TODO: Implement the function here match day { 1 => "Monday", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday", 6 => "Saturday", 7 => "Sunday", _ => "Invalid day number", }}
pub fn weekday_from_number(day: u8) -> &'static str { match day { 1 => "Monday", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday", 6 => "Saturday", 7 => "Sunday", _ => "Invalid day number", }}
pub fn weekday_from_number(day: u8) -> &'static str { match day { 1 => "Monday", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday", 6 => "Saturday", 7 => "Sunday", _ => "Invalid day number", }}
pub fn weekday_from_number(day: u8) -> &'static str { // TODO: Implement the function here match day { 1 => "Monday", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday", 6 => "Saturday", 7 => "Sunday", _ => "Invalid day number", }}
pub fn weekday_from_number(day: u8) -> &'static str { // TODO: Implement the function here match day { 1 => return "Monday", 2 => return "Tuesday", 3 => return "Wednesday", 4 => return "Thursday", 5 => return "Friday", 6 => return "Saturday", 7 => return "Sunday", _ => return "Invalid day number" } }
pub fn weekday_from_number(day: u8) -> &'static str { // TODO: Implement the function here match day { 1 => "Monday", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday", 6 => "Saturday", 7 => "Sunday", _ => "Invalid day number", }}
pub fn weekday_from_number(day: u8) -> &'static str { match day { 1 => "Monday", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday", 6 => "Saturday", 7 => "Sunday", _ => "Invalid day number", }}
pub fn weekday_from_number(day: u8) -> &'static str { match day { 1 => "Monday", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday", 6 => "Saturday", 7 => "Sunday", _ => "Invalid day number", }}
pub fn weekday_from_number(day: u8) -> &'static str { // TODO: Implement the function here match day { 1 => "Monday", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday", 6 => "Saturday", 7 => "Sunday", _ => "Invalid day number" }}
pub fn weekday_from_number(day: u8) -> &'static str { // TODO: Implement the function here match day { 1 => "Monday", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday", 6 => "Saturday", 7 => "Sunday", _ => "Invalid day number", }}
pub fn weekday_from_number(day: u8) -> &'static str { match day { 1 => "Monday", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday", 6 => "Saturday", 7 => "Sunday", _ => "Invalid day number", }}