1 // Rust-101, Part 08: Associated Types, Modules
2 // ============================================
9 // Add with carry, returning the sum and the carry
10 fn overflowing_add(a: u64, b: u64, carry: bool) -> (u64, bool) {
11 let sum = u64::wrapping_add(a, b);
12 if sum >= a { // first addition did not overflow
14 } else { // first addition *did* overflow
20 fn test_overflowing_add() {
21 assert_eq!(overflowing_add(10, 100, false), (110, false));
22 assert_eq!(overflowing_add(10, 100, true), (111, false));
23 assert_eq!(overflowing_add(1 << 63, 1 << 63, false), (0, true));
24 assert_eq!(overflowing_add(1 << 63, 1 << 63, true), (1, true));
25 assert_eq!(overflowing_add(1 << 63, (1 << 63) -1 , true), (0, true));
28 impl ops::Add for BigInt {
30 fn add(self, rhs: BigInt) -> Self::Output {
31 let mut result_vec:Vec<u64> = Vec::with_capacity(cmp::max(self.data.len(), rhs.data.len()));
36 // [index](main.html) | [previous](part07.html) | [next](main.html)