smart point

This commit is contained in:
jiangfangping 2024-07-25 10:20:42 +08:00
parent e77ba8dcf2
commit 46314667ce
5 changed files with 17 additions and 14 deletions

View File

@ -13,7 +13,7 @@ enum DivisionError {
fn divide(a: i64, b: i64) -> Result<i64, DivisionError> { fn divide(a: i64, b: i64) -> Result<i64, DivisionError> {
if b == 0 { if b == 0 {
Err(DivisionError::DivideByZero) Err(DivisionError::DivideByZero)
} else if (a == i64::MIN && b == -1) { } else if a == i64::MIN && b == -1 {
Err(DivisionError::IntegerOverflow) Err(DivisionError::IntegerOverflow)
} else if a % b == 0 { } else if a % b == 0 {
Ok(a / b) Ok(a / b)
@ -24,14 +24,14 @@ fn divide(a: i64, b: i64) -> Result<i64, DivisionError> {
// TODO: Add the correct return type and complete the function body. // TODO: Add the correct return type and complete the function body.
// Desired output: `Ok([1, 11, 1426, 3])` // Desired output: `Ok([1, 11, 1426, 3])`
fn result_with_list() -> Result<Vec<i32>, DivisionError> { fn result_with_list() -> Result<Vec<i64>, DivisionError> {
let numbers = [27, 297, 38502, 81]; let numbers = [27, 297, 38502, 81];
numbers.into_iter().map(|n| divide(n, 27)).collect() numbers.into_iter().map(|n| divide(n, 27)).collect()
} }
// TODO: Add the correct return type and complete the function body. // TODO: Add the correct return type and complete the function body.
// Desired output: `[Ok(1), Ok(11), Ok(1426), Ok(3)]` // Desired output: `[Ok(1), Ok(11), Ok(1426), Ok(3)]`
fn list_of_results() -> Vec<Result<i32, DivisionError>> { fn list_of_results() -> Vec<Result<i64, DivisionError>> {
let numbers = [27, 297, 38502, 81]; let numbers = [27, 297, 38502, 81];
numbers.into_iter().map(|n| divide(n, 27)).collect() numbers.into_iter().map(|n| divide(n, 27)).collect()
} }

View File

@ -23,13 +23,13 @@ fn main() {
let numbers: Vec<_> = (0..100u32).collect(); let numbers: Vec<_> = (0..100u32).collect();
// TODO: Define `shared_numbers` by using `Arc`. // TODO: Define `shared_numbers` by using `Arc`.
// let shared_numbers = ???; let shared_numbers = Arc::new(numbers);
let mut join_handles = Vec::new(); let mut join_handles = Vec::new();
for offset in 0..8 { for offset in 0..8 {
// TODO: Define `child_numbers` using `shared_numbers`. // TODO: Define `child_numbers` using `shared_numbers`.
// let child_numbers = ???; let child_numbers = Arc::clone(&shared_numbers);
let handle = thread::spawn(move || { let handle = thread::spawn(move || {
let sum: u32 = child_numbers.iter().filter(|&&n| n % 8 == offset).sum(); let sum: u32 = child_numbers.iter().filter(|&&n| n % 8 == offset).sum();

View File

@ -12,18 +12,18 @@
// TODO: Use a `Box` in the enum definition to make the code compile. // TODO: Use a `Box` in the enum definition to make the code compile.
#[derive(PartialEq, Debug)] #[derive(PartialEq, Debug)]
enum List { enum List {
Cons(i32, List), Cons(i32, Box<List>),
Nil, Nil,
} }
// TODO: Create an empty cons list. // TODO: Create an empty cons list.
fn create_empty_list() -> List { fn create_empty_list() -> List {
todo!() List::Nil
} }
// TODO: Create a non-empty cons list. // TODO: Create a non-empty cons list.
fn create_non_empty_list() -> List { fn create_non_empty_list() -> List {
todo!() List::Cons(32, Box::new(List::Nil))
} }
fn main() { fn main() {

View File

@ -39,7 +39,7 @@ mod tests {
let mut input = Cow::from(&vec); let mut input = Cow::from(&vec);
abs_all(&mut input); abs_all(&mut input);
// TODO: Replace `todo!()` with `Cow::Owned(_)` or `Cow::Borrowed(_)`. // TODO: Replace `todo!()` with `Cow::Owned(_)` or `Cow::Borrowed(_)`.
assert!(matches!(input, todo!())); assert!(matches!(input, Cow::Borrowed(_)));
} }
#[test] #[test]
@ -52,7 +52,7 @@ mod tests {
let mut input = Cow::from(vec); let mut input = Cow::from(vec);
abs_all(&mut input); abs_all(&mut input);
// TODO: Replace `todo!()` with `Cow::Owned(_)` or `Cow::Borrowed(_)`. // TODO: Replace `todo!()` with `Cow::Owned(_)` or `Cow::Borrowed(_)`.
assert!(matches!(input, todo!())); assert!(matches!(input, Cow::Owned(_)));
} }
#[test] #[test]
@ -64,6 +64,6 @@ mod tests {
let mut input = Cow::from(vec); let mut input = Cow::from(vec);
abs_all(&mut input); abs_all(&mut input);
// TODO: Replace `todo!()` with `Cow::Owned(_)` or `Cow::Borrowed(_)`. // TODO: Replace `todo!()` with `Cow::Owned(_)` or `Cow::Borrowed(_)`.
assert!(matches!(input, todo!())); assert!(matches!(input, Cow::Owned(_)));
} }
} }

View File

@ -61,17 +61,17 @@ mod tests {
jupiter.details(); jupiter.details();
// TODO // TODO
let saturn = Planet::Saturn(Rc::new(Sun)); let saturn = Planet::Saturn(Rc::clone(&sun));
println!("reference count = {}", Rc::strong_count(&sun)); // 7 references println!("reference count = {}", Rc::strong_count(&sun)); // 7 references
saturn.details(); saturn.details();
// TODO // TODO
let uranus = Planet::Uranus(Rc::new(Sun)); let uranus = Planet::Uranus(Rc::clone(&sun));
println!("reference count = {}", Rc::strong_count(&sun)); // 8 references println!("reference count = {}", Rc::strong_count(&sun)); // 8 references
uranus.details(); uranus.details();
// TODO // TODO
let neptune = Planet::Neptune(Rc::new(Sun)); let neptune = Planet::Neptune(Rc::clone(&sun));
println!("reference count = {}", Rc::strong_count(&sun)); // 9 references println!("reference count = {}", Rc::strong_count(&sun)); // 9 references
neptune.details(); neptune.details();
@ -93,12 +93,15 @@ mod tests {
println!("reference count = {}", Rc::strong_count(&sun)); // 4 references println!("reference count = {}", Rc::strong_count(&sun)); // 4 references
// TODO // TODO
drop(earth);
println!("reference count = {}", Rc::strong_count(&sun)); // 3 references println!("reference count = {}", Rc::strong_count(&sun)); // 3 references
// TODO // TODO
drop(venus);
println!("reference count = {}", Rc::strong_count(&sun)); // 2 references println!("reference count = {}", Rc::strong_count(&sun)); // 2 references
// TODO // TODO
drop(mercury);
println!("reference count = {}", Rc::strong_count(&sun)); // 1 reference println!("reference count = {}", Rc::strong_count(&sun)); // 1 reference
assert_eq!(Rc::strong_count(&sun), 1); assert_eq!(Rc::strong_count(&sun), 1);