1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/// Calculate the product of all elements in a collection.
/// If the collection is empty, returns 1 (multiplicative identity).
/// Works with any numeric type that implements `std::ops::Mul` and can be copied.
///
/// # Arguments
/// * `collection` - A slice of numbers.
///
/// # Returns
/// * `T` - The product of all numbers in the collection.
///
/// # Examples
/// ```rust
/// use lowdash::product;
///
/// // Integer product
/// let numbers = vec![1, 2, 3, 4, 5];
/// assert_eq!(product(&numbers), 120);
/// ```
///
/// ```rust
/// use lowdash::product;
///
/// // Float product
/// let numbers = vec![1.5, 2.0, 3.0];
/// assert_eq!(product(&numbers), 9.0);
/// ```
///
/// ```rust
/// use lowdash::product;
///
/// // Empty collection returns 1
/// let empty: Vec<i32> = vec![];
/// assert_eq!(product(&empty), 1);
/// ```
pub fn product<T>(collection: &[T]) -> T
where
T: std::ops::Mul<Output = T> + Copy + From<u8>,
{
if collection.is_empty() {
return T::from(1);
}
collection.iter().fold(T::from(1), |acc, &x| acc * x)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_product_integers() {
let numbers = vec![1, 2, 3, 4, 5];
assert_eq!(product(&numbers), 120);
}
#[test]
fn test_product_floats() {
let numbers = vec![1.5, 2.0, 3.0];
assert_eq!(product(&numbers), 9.0);
}
#[test]
fn test_product_empty() {
let empty: Vec<i32> = vec![];
assert_eq!(product(&empty), 1);
}
#[test]
fn test_product_single_element() {
let numbers = vec![42];
assert_eq!(product(&numbers), 42);
}
#[test]
fn test_product_with_zero() {
let numbers = vec![1, 2, 0, 4, 5];
assert_eq!(product(&numbers), 0);
}
#[test]
fn test_product_negative_numbers() {
let numbers = vec![-2, 3, -4];
assert_eq!(product(&numbers), 24);
}
#[test]
fn test_product_complex_floats() {
let numbers = vec![0.5, 2.5, -1.5];
assert_eq!(product(&numbers), -1.875);
}
}