I implement merge sort, which sorts an array of type T In my merge method, the algorithm requires the last element of the left and right list to be positive infinity . How can I get the maximum value that a given data type can store?
fn merge<T: PartialOrd + Copy + std::fmt::Debug>(p: usize, q: usize, r: usize, array: &mut Vec<T>) { let left_size: usize = q - p; let right_size: usize = r - q; let mut left: Vec<T> = Vec::new(); let mut right: Vec<T> = Vec::new(); for i in 0..left_size { left.push(array[p + i]); } for i in 0..right_size { right.push(array[q + i]); } left.push(T::max_value()); //where I would put the max value right.push(T::max_value()); //where I would put the max value let mut i: usize = 0; let mut j: usize = 0; for k in p..r { if left[i] <= right[j] { array[k] = left[i]; i += 1; } else { array[k] = right[j]; j += 1; } } }
source share