Divide and conquer approach in search for max element, take 2 Adopted some suggestions from the answer of my previous post. Here is the code:

trait MaxElem&lt;T&gt; {
    fn max_elem&lt;'a&gt;(&amp;'a self) -&gt; &amp;'a T;
}

impl&lt;T&gt; MaxElem&lt;T&gt; for [T] where T: Ord {
    fn max_elem&lt;'a&gt;(&amp;'a self) -&gt; &amp;'a T {
        max_elem_helper(self, 0, self.len())
    }
}


fn max_elem_helper&lt;'a, T&gt;(array: &amp;'a [T], left: usize, right: usize) -&gt; &amp;'a T 
    where T: Ord
{
    if right - left == 1 {
        return &amp;array[left];
    }
    let mid = (left + right) / 2;
    let max1 = max_elem_helper(array, left, mid);
    let max2 = max_elem_helper(array, mid, right);
    if max1 &gt; max2 {
        max1
    } else {
        max2
    }
}

#[cfg(test)]
mod test {
    use max_elem::MaxElem;
    #[test]
    fn test_max_elem() {
        let array = [11, 2, 9, 1, 3, 88]; 
        let m = array.max_elem(); 
        assert_eq!(*m, 88);
    }
}


All suggestions are welcome.
The code panics with a stack overflow if you provide an empty array:

#[test]
fn test_empty() {
    let array: [u8; 0] = [];
    array.max_elem();
}


thread 'test::test_empty' has overflowed its stack
fatal runtime error: stack overflow
error: Process didn't exit successfully: `/private/tmp/max/target/debug/max-426b0349be06fd52` (signal: 6, SIGABRT: process abort signal)

The tests don't work unless the crate is named max_elem. Instead, use use super::MaxElem instead.
You could choose to embed the helper method inside of the other function, giving a natural scoping (and preventing the need for the longer name):

impl&lt;T&gt; MaxElem&lt;T&gt; for [T]
    where T: Ord
{
    fn max_elem&lt;'a&gt;(&amp;'a self) -&gt; &amp;'a T {
        fn helper&lt;'a, T&gt;(array: &amp;'a [T], left: usize, right: usize) -&gt; &amp;'a T
            where T: Ord
        {
            // ...
        }

        helper(self, 0, self.len())
    }
}

Instead of passing in the entire slice and left / right; try splitting the slice into smaller parts via split_at. Then the slice itself tracks the beginning and the end.
The function cmp::max exists.




Overall, there's a nice functional implementation that weighs-in at about 20 lines.

use std::cmp;

trait MaxElem&lt;T&gt; {
    fn max_elem&lt;'a&gt;(&amp;'a self) -&gt; Option&lt;&amp;'a T&gt;;
}

impl&lt;T&gt; MaxElem&lt;T&gt; for [T] where T: Ord {
    fn max_elem&lt;'a&gt;(&amp;'a self) -&gt; Option&lt;&amp;'a T&gt; {
        let len = self.len();

        match len {
            0 | 1 =&gt; self.first(),
            _ =&gt; {
                let mid = len / 2;
                let (left, right) = self.split_at(mid);

                let left = left.max_elem();
                let right = right.max_elem();

                match (left, right) {
                    (Some(a), Some(b)) =&gt; Some(cmp::max(a, b)),
                    _ =&gt; left.or(right)
                }
            }
        }
    }
}

#[cfg(test)]
mod test {
    use super::MaxElem;

    #[test]
    fn test_max_elem() {
        let array = [11, 2, 9, 1, 3, 88];
        assert_eq!(Some(&amp;88), array.max_elem());
    }

    #[test]
    fn test_empty() {
        let array: [u8; 0] = [];
        assert_eq!(None, array.max_elem());
    }
}