Writing slice compare in a more compact way I have this code snippet comparing two slice references:

  fn compare(&amp;self, a: &amp;[u8], b: &amp;[u8]) -&gt; i64 {
    let mut p1 = a.as_ptr() as *mut u8;
    let mut p2 = b.as_ptr() as *mut u8;

    let mut min_len = cmp::min(a.len(), b.len());

    let mut ret = 0;
    while min_len &gt; 0 {
      unsafe {
        if *p1 != *p2 {
          ret = *p1 as i64 - *p2 as i64;
          break;
        } else {
          p1 = p1.offset(1);
          p2 = p2.offset(1);
        }
      }
      min_len = min_len - 1;
    }

    if ret == 0 {
      if a.len() &lt; b.len() {ret = -1;}
      else if a.len() &gt; b.len() {ret = 1;}
    }

    return ret;
  }


I think I wrote it down in C style. I would like to write this in Rust style.

I could replace 'ret' with std::cmp::Ordering. What else could I do?
What about this:

fn compare(a: &amp;[u8], b: &amp;[u8]) -&gt; cmp::Ordering {
    for (ai, bi) in a.iter().zip(b.iter()) {
        match ai.cmp(&amp;bi) {
            Ordering::Equal =&gt; continue,
            ord =&gt; return ord
        }
    }

    /* if every single element was equal, compare length */
    a.len().cmp(&amp;b.len())
}


I've removed the self argument, and used the cmp-module to do the actual comparison and zip() as @alexv already suggested.