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".
match
statement to map each number to its corresponding weekday.&'static str
denotes a string slice that lives for the entire duration of the programzelsazgh
pub fn weekday_from_number(day: u8) -> &'static str { // TODO: Implement the function here let myweekday =match day{ 1 => "Monday", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday", 6 => "Saturday", 7 => "Sunday", _ => "Invalid day number", }; myweekday}
VladyslavY
pub fn weekday_from_number(day: u8) -> &'static str { match day { 1..=7 => ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"][day as usize - 1], _ => "Invalid day number", }}
tiagombsilva
pub fn weekday_from_number(day: u8) -> &'static str { let weekday = match day { 1 => "Monday", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday", 6 => "Saturday", 7 => "Sunday", _ => "Invalid day number" }; weekday}
digitalresistor
pub fn weekday_from_number(day: u8) -> &'static str { match day { 1 => return "Monday", 2 => return "Tuesday", 3 => return "Wednesday", 4 => return "Thursday", 5 => return "Friday", 6 => return "Saturday", 7 => return "Sunday", _ => "Invalid day number" }}
jose-bernardo
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" }}
Maki-SIO
pub fn weekday_from_number(day: u8) -> &'static str { let mut res: &str = ""; match day { 1 => res = "Monday", 2 => res = "Tuesday", 3 => res = "Wednesday", 4 => res = "Thursday", 5 => res = "Friday", 6 => res = "Saturday", 7 => res = "Sunday", _ => res = "Invalid day number" } res}
carlos-quantexai
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", // Handle invalid input }}
radloffl
pub fn weekday_from_number(day: u8) -> &'static str { // TODO: Implement the function here let s = match day { 1 => "Monday", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday", 6 => "Saturday", 7 => "Sunday", _ => "Invalid day number", }; return s;}
just-mephit
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", }}
retinotopic
pub fn weekday_from_number(day: u8) -> &'static str { let daystr: &'static str; match day { 1 => daystr = "Monday", 2 => daystr = "Tuesday", 3 => daystr = "Wednesday", 4 => daystr = "Thursday", 5 => daystr = "Friday", 6 => daystr = "Saturday", 7 => daystr = "Sunday", _ => daystr = "Invalid day number", } return daystr;}
dslex35
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",}}
victorchukwuemeka
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" }}
jhq223
pub fn weekday_from_number(day: u8) -> &'static str { let resutl = match day { 1 => "Monday", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday", 6 => "Saturday", 7 => "Sunday", _ => "Invalid day number", }; resutl}
ThatphumCpre
use std::collections::HashMap;pub fn weekday_from_number(day: u8) -> &'static str { let daymap = HashMap::from([ (1, "Monday"), (2, "Tuesday"), (3, "Wednesday"), (4,"Thursday"), (5, "Friday"), (6, "Saturday"), (7, "Sunday"), ]); daymap.get(&day).unwrap_or(&"Invalid day number") }
francisco-cmyk
pub fn weekday_from_number(day: u8) -> &'static str { // TODO: Implement the function here let day_str = match day { 1 => "Monday", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday", 6 => "Saturday", 7 => "Sunday", _ => "Invalid day number", }; return day_str}
xbarnett
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" }}
mk-comm
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" }}
Johnchoi913
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" }}
Johnchoi913
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" }}
lishigushi
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", }}
majesticalcreature
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" }}
sander-b-postnl
pub fn weekday_from_number(day: u8) -> &'static str { match day { 1..=7 => ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"][day as usize - 1], _ => "Invalid day number", }}
matei
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", default => "Invalid day number" }}
edoardo-morosanu
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", }}
i5-650
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" }}
amassare
pub fn weekday_from_number(day: u8) -> &'static str { // TODO: Implement the function here if day<1||day>7{ return "Invalid day number"; } let weekday=["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]; weekday[day as usize -1]}
zavakid
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", }}
IdoPort
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" }}
RiskyRomero
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" }}
Ustin
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" }}
LaurentiuStoleriu
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" }}
shankun
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", }}
Parallaxes
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", }}
ogaca42
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", }}
otherJL0
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", }}
ayushrawat10
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", }}
pbjarterot
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" }}
CianciuStyles
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" }}
l3vith
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" }}
agegorin
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" }}
daanbouwman19
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" }}
damascussteel21
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" }}
alexromanov
pub fn weekday_from_number(day: u8) -> &'static str { // TODO: Implement the function here let weekday = match day { 1 => "Monday", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday", 6 => "Saturday", 7 => "Sunday", _ => "Invalid day number", }; weekday}
mynamesent
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" }}
kyhou
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" }}
oneopane
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", }}
AtJiawei
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", }}
Xinoi
pub fn weekday_from_number(day: u8) -> &'static str { // TODO: Implement the function here let weekday = match day { 1 => "Monday", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday", 6 => "Saturday", 7 => "Sunday", _ => "Invalid day number", }; weekday}
Aditeya
pub fn weekday_from_number(day: u8) -> &'static str { // TODO: Implement the function here let day = match day { 1 => "Monday", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday", 6 => "Saturday", 7 => "Sunday", _ => "Invalid day number", }; day}
edgarcnp
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" }}