1 // ***Remember to enable/add this part in `main.rs`!***
3 // Rust-101, Part 08: Associated Types, Modules
4 // ============================================
11 // Add with carry, returning the sum and the carry
12 fn overflowing_add(a: u64, b: u64, carry: bool) -> (u64, bool) {
13 let sum = u64::wrapping_add(a, b);
14 if sum >= a { // first addition did not overflow
16 } else { // first addition *did* overflow
22 fn test_overflowing_add() {
23 assert_eq!(overflowing_add(10, 100, false), (110, false));
24 assert_eq!(overflowing_add(10, 100, true), (111, false));
25 assert_eq!(overflowing_add(1 << 63, 1 << 63, false), (0, true));
26 assert_eq!(overflowing_add(1 << 63, 1 << 63, true), (1, true));
27 assert_eq!(overflowing_add(1 << 63, (1 << 63) -1 , true), (0, true));
30 impl ops::Add for BigInt {
32 fn add(self, rhs: BigInt) -> Self::Output {
33 let mut result_vec:Vec<u64> = Vec::with_capacity(cmp::max(self.data.len(), rhs.data.len()));
38 // [index](main.html) | [previous](part07.html) | [next](main.html)