Skip to content

Instantly share code, notes, and snippets.

@dhirabayashi
Created June 12, 2022 11:37
Show Gist options
  • Save dhirabayashi/fc62f4afa1423cac53c688caae4c9f97 to your computer and use it in GitHub Desktop.
Save dhirabayashi/fc62f4afa1423cac53c688caae4c9f97 to your computer and use it in GitHub Desktop.
fn main() {
for i in 1..=12 {
println!("On the {} day of Christmas", to_ordinal(i));
println!("My true love sent to me");
for j in (2..=i).rev() {
match j {
12 => println!("12 drummers drumming"),
11 => println!("Eleven pipers piping"),
10 => println!("Ten lords a-leaping"),
9 => println!("Nine ladies dancing"),
8 => println!("Eight maids a-milking"),
7 => println!("Seven swans a-swimming"),
6 => println!("Six geese a-laying"),
5 => println!("Five golden rings (five golden rings)"),
4 => println!("Four calling birds"),
3 => println!("Three French hens"),
2 => println!("Two turtle-doves"),
unknown => println!("unknown value: {}", unknown)
}
}
println!("{} partridge in pear tree\n", if i == 1 { "A" } else { "And a" })
}
println!("And a partridge in pear tree");
}
fn to_ordinal(n: i32) -> &'static str {
match n {
1 => "first",
2 => "second",
3 => "third",
4 => "fourth",
5 => "fifth",
6 => "sixth",
7 => "seventh",
8 => "eighth",
9 => "ninth",
10 => "tenth",
11 => "11th",
12 => "12th",
_more => "unknown"
}
}
fn main() {
println!("fib {}", fibonacci(8));
}
fn fibonacci(n: i32) -> i32 {
if n == 0 {
return 0
}
if n == 1 {
return 1
}
fibonacci(n - 2) + fibonacci(n - 1)
}
fn main() {
let celsius = 25.0;
let fahrenheit = 70.0;
println!("{}℃ is {}℉", celsius, celsius_to_fahrenheit(celsius));
println!("{}℉ is {}℃", fahrenheit, fahrenheit_to_celsius(fahrenheit));
}
fn celsius_to_fahrenheit(celsius: f64) -> f64 {
celsius * 1.8 + 32.0
}
fn fahrenheit_to_celsius(fahrenheit: f64) -> f64 {
(fahrenheit - 32.0) / 1.8
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment