mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-05 10:19:18 +00:00
Solve box and iterators
This commit is contained in:
parent
c7a99cfe94
commit
3e8508dd04
@ -16,11 +16,9 @@
|
||||
//
|
||||
// Execute `rustlings hint box1` for hints :)
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
#[derive(PartialEq, Debug)]
|
||||
pub enum List {
|
||||
Cons(i32, List),
|
||||
Cons(i32, Box<List>),
|
||||
Nil,
|
||||
}
|
||||
|
||||
@ -30,11 +28,11 @@ fn main() {
|
||||
}
|
||||
|
||||
pub fn create_empty_list() -> List {
|
||||
unimplemented!()
|
||||
List::Nil
|
||||
}
|
||||
|
||||
pub fn create_non_empty_list() -> List {
|
||||
unimplemented!()
|
||||
List::Cons(1, Box::new(List::Nil))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@ -7,8 +7,6 @@
|
||||
// Execute `rustlings hint iterators3` to get some hints!
|
||||
// Have fun :-)
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub enum DivisionError {
|
||||
NotDivisible(NotDivisibleError),
|
||||
@ -24,7 +22,15 @@ pub struct NotDivisibleError {
|
||||
// This function should calculate `a` divided by `b` if `a` is
|
||||
// evenly divisible by b.
|
||||
// Otherwise, it should return a suitable error.
|
||||
pub fn divide(a: i32, b: i32) -> Result<i32, DivisionError> {}
|
||||
pub fn divide(a: i32, b: i32) -> Result<i32, DivisionError> {
|
||||
if b == 0 {
|
||||
Err(DivisionError::DivideByZero)
|
||||
} else if a % b != 0 {
|
||||
Err(DivisionError::NotDivisible(NotDivisibleError{dividend: a, divisor: b}))
|
||||
} else {
|
||||
Ok(a / b)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
@ -1,7 +1,5 @@
|
||||
// iterators4.rs
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
pub fn factorial(num: u64) -> u64 {
|
||||
// Complete this function to return the factorial of num
|
||||
// Do not use:
|
||||
@ -12,6 +10,15 @@ pub fn factorial(num: u64) -> u64 {
|
||||
// For an extra challenge, don't use:
|
||||
// - recursion
|
||||
// Execute `rustlings hint iterators4` for hints.
|
||||
|
||||
// Recusive
|
||||
// match num {
|
||||
// 0 | 1 => 1,
|
||||
// num => num * factorial(num - 1),
|
||||
// }
|
||||
|
||||
// Functional
|
||||
(1..num+1).product()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user