ultralytics_template_rust/lib.rs
1// Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
2
3use std::ops::Add;
4
5/// Adds two values together.
6///
7/// The function works with any type that supports the `+` operator and is `Copy`, making it usable
8/// with common numeric primitives such as integers or floating-point numbers.
9///
10/// # Examples
11/// ```
12/// use ultralytics_template_rust::add_numbers;
13///
14/// let sum = add_numbers(1_i32, 2_i32);
15/// assert_eq!(sum, 3);
16///
17/// let float_sum = add_numbers(1.5_f64, 2.0_f64);
18/// assert!((float_sum - 3.5_f64).abs() < f64::EPSILON);
19/// ```
20pub fn add_numbers<T>(a: T, b: T) -> T
21where
22 T: Add<Output = T> + Copy,
23{
24 a + b
25}
26
27/// Generates a human-readable summary of the example addition.
28///
29/// This mirrors the simple entry point showcased by the CLI in `src/main.rs` and is exposed so
30/// integration tests can validate user-visible behavior without shelling out.
31pub fn run_example() -> String {
32 let a = 1;
33 let b = 2;
34 let y = add_numbers(a, b);
35 format!("Added {a} + {b} = {y}")
36}
37
38#[cfg(test)]
39mod tests {
40 use super::*;
41
42 #[test]
43 fn adds_integers() {
44 assert_eq!(add_numbers(2, 3), 5);
45 assert_eq!(add_numbers(-1, 1), 0);
46 assert_eq!(add_numbers(-1, -1), -2);
47 }
48
49 #[test]
50 fn adds_floats() {
51 let result = add_numbers(1.5_f32, 2.25_f32);
52 assert!((result - 3.75_f32).abs() < f32::EPSILON);
53 }
54
55 #[test]
56 fn produces_example_output() {
57 let output = run_example();
58 assert_eq!(output, "Added 1 + 2 = 3");
59 }
60}