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