mirror of
https://github.com/rust-lang/rustlings.git
synced 2024-12-29 00:00:04 +03:00
Complete if exercises
This commit is contained in:
parent
7cad97ec5b
commit
ef82b93271
|
@ -2,13 +2,12 @@
|
||||||
//
|
//
|
||||||
// Execute `rustlings hint if1` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint if1` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
pub fn bigger(a: i32, b: i32) -> i32 {
|
pub fn bigger(a: i32, b: i32) -> i32 {
|
||||||
// Complete this function to return the bigger number!
|
// Complete this function to return the bigger number!
|
||||||
// Do not use:
|
// Do not use:
|
||||||
// - another function call
|
// - another function call
|
||||||
// - additional variables
|
// - additional variables
|
||||||
|
if a > b {a} else {b}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Don't mind this for now :)
|
// Don't mind this for now :)
|
||||||
|
|
|
@ -5,13 +5,18 @@
|
||||||
//
|
//
|
||||||
// Execute `rustlings hint if2` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint if2` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
pub fn foo_if_fizz(fizzish: &str) -> &str {
|
pub fn foo_if_fizz(fizzish: &str) -> &str {
|
||||||
|
// &str is a string slice -
|
||||||
|
// Let's say the full string is a ribbon
|
||||||
|
// A string slice is a piece of the ribbon
|
||||||
|
// It's just a reference to the ribbon so doing things to the piece won't change the original ribbon.
|
||||||
|
|
||||||
if fizzish == "fizz" {
|
if fizzish == "fizz" {
|
||||||
"foo"
|
"foo"
|
||||||
|
} else if fizzish == "fuzz" {
|
||||||
|
"bar"
|
||||||
} else {
|
} else {
|
||||||
1
|
"baz"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,17 +2,17 @@
|
||||||
//
|
//
|
||||||
// Execute `rustlings hint if3` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint if3` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
pub fn animal_habitat(animal: &str) -> &'static str {
|
pub fn animal_habitat(animal: &str) -> &'static str {
|
||||||
|
// static is a lifetime, it means that the string will be valid for the entire duration of the program
|
||||||
|
// it's practically hardcoded into the program and always available
|
||||||
let identifier = if animal == "crab" {
|
let identifier = if animal == "crab" {
|
||||||
1
|
1
|
||||||
} else if animal == "gopher" {
|
} else if animal == "gopher" {
|
||||||
2.0
|
2
|
||||||
} else if animal == "snake" {
|
} else if animal == "snake" {
|
||||||
3
|
3
|
||||||
} else {
|
} else {
|
||||||
"Unknown"
|
0
|
||||||
};
|
};
|
||||||
|
|
||||||
// DO NOT CHANGE THIS STATEMENT BELOW
|
// DO NOT CHANGE THIS STATEMENT BELOW
|
||||||
|
|
Loading…
Reference in a new issue