# Thoughts on a free \`cmp\` function

**URL:** <https://internals.rust-lang.org/t/thoughts-on-a-free-cmp-function/16504>\
**Category:** libs\
**Created:** [April 20, 2022, 9:59am UTC](https://internals.rust-lang.org/t/thoughts-on-a-free-cmp-function/16504 "2022-04-20T09:59:50Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![cameron1024](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cameron1024/32/8959_2.png) [@cameron1024](https://internals.rust-lang.org/u/cameron1024)\
**Post date:** [April 20, 2022, 9:59am UTC](https://internals.rust-lang.org/t/thoughts-on-a-free-cmp-function/16504/1 "2022-04-20T09:59:50Z")

</div>

TLDR: I'd like a function in std like `cmp(a, b)` that calls `a.cmp(b)`.

* * *

Recently, `std::iter::zip` was added for better readability/formatting when using iterators that are the result of lots of function calls. For example:

```rust
let a = vec![1, 2, 3].into_iter();
let b = vec![1, 2, 3].into_iter();

let zipped = a
    .map(|i| i * 2)
    .filter(|i| i % 3 == 0)
    .zip(b.map(|i| i * 2).filter(|i| i % 3 == 0));

// compared to

zip(
    a.map(|i| i * 2).filter(|i| i % 3 == 0),
    b.map(|i| i * 2).filter(|i| i % 3 == 0),
)

```

Recently I found myself sorting a lot of slices using `.sort_by()`, and this lead to a pretty similar situation with code like:

```rust
items.sort_by(|a, b| {
    a.config
        .properties
        .some_value
        .cmp(&b.config.properties.some_value)
});

```

However, with this `cmp` function I was able to get similar readability improvements as with `zip`.

```rust
fn cmp<T>(a: &T, b: &T) -> Ordering where T: Ord {
    a.cmp(b)
}

items.sort_by(|a, b| {
    cmp(
        &a.config.properties.some_value,
        &b.config.properties.some_value,
    )
})

```

There's probably scope for an equivalent for `partial_cmp`, though I suspect the "symmetry" benefits are less obvious if the two arguments might have different types 🤷

I'd be keen to see this in the standard library, and if other people also think it would be a good addition I'd be happy to open a PR for it. But of course if there are reasons why such a function isn't needed, or doesn't belong in std, I'd be more than happy to hear them too.

Thanks 😁

---

<div class="post-metadata">

**Author:** ![17cupsofcoffee](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/17cupsofcoffee/32/5487_2.png) [@17cupsofcoffee](https://internals.rust-lang.org/u/17cupsofcoffee)\
**Post date:** [April 20, 2022, 11:41am UTC](https://internals.rust-lang.org/t/thoughts-on-a-free-cmp-function/16504/2 "2022-04-20T11:41:18Z")

</div>

It's worth mentioning that you can approximate this by calling `Ord::cmp`:

```rust
use std::cmp::Ord;

items.sort_by(|a, b| {
    Ord::cmp(
        &a.config.properties.some_value,
        &b.config.properties.some_value,
    )
})

```

But you can't do `use std::cmp::Ord::cmp`, so there could still be an argument made for having a free function that does this.

---

<div class="post-metadata">

**Author:** ![Jules-Bertholet](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jules-bertholet/32/10671_2.png) [@Jules-Bertholet](https://internals.rust-lang.org/u/Jules-Bertholet)\
**Post date:** [April 20, 2022, 2:58pm UTC](https://internals.rust-lang.org/t/thoughts-on-a-free-cmp-function/16504/3 "2022-04-20T14:58:56Z")

</div>

> [@17cupsofcoffee](#):
>
> you can't do `use std::cmp::Ord::cmp`

Maybe this should be allowed?

---

<div class="post-metadata">

**Author:** ![cuviper](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cuviper/32/1897_2.png) [@cuviper](https://internals.rust-lang.org/u/cuviper)\
**Post date:** [April 20, 2022, 3:22pm UTC](https://internals.rust-lang.org/t/thoughts-on-a-free-cmp-function/16504/4 "2022-04-20T15:22:57Z")

</div>

`Ord` is in the prelude, so you normally don't need to import it. But if you must use the path, you can write the call in full: `<_ as std::cmp::Ord>::cmp`

I feel like there was an RFC to `use` methods, but I can't find it now...

edit: found an issue: [rust-lang/rfcs#1995](https://github.com/rust-lang/rfcs/issues/1995)

---

<div class="post-metadata">

**Author:** ![jdahlstrom](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jdahlstrom/32/3351_2.png) [@jdahlstrom](https://internals.rust-lang.org/u/jdahlstrom)\
**Post date:** [April 20, 2022, 3:56pm UTC](https://internals.rust-lang.org/t/thoughts-on-a-free-cmp-function/16504/5 "2022-04-20T15:56:08Z")

</div>

> ```rust
> items.sort_by(|a, b| {
> cmp(
> &a.config.properties.some_value,
> &b.config.properties.some_value,
> )
> })
> 
> ```

What others said about `Ord::cmp`; also if your use case is literally like this where you're just doing a map/projection and calling `cmp`, the `sort_by_key` method is a perfect fit and saves you from potential copypaste bugs:

```rust
items.sort_by_key(|a| a.config.properties.some_value)

```

(although there's the problem that you can't return by reference from the key function due to lifetime issues ☹ )

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)\
**Post date:** [April 20, 2022, 4:33pm UTC](https://internals.rust-lang.org/t/thoughts-on-a-free-cmp-function/16504/6 "2022-04-20T16:33:50Z")

</div>

> [@17cupsofcoffee](#):
>
> But you can't do `use std::cmp::Ord::cmp` , so there could still be an argument made for having a free function that does this.

As **Jules-Bertholet** said, I'd love to see a proposal to allow this. This isn't the only kind of thing where it'd be nice -- [https://doc.rust-lang.org/nightly/std/default/fn.default.html](https://doc.rust-lang.org/nightly/std/default/fn.default.html) is another thing that should just be `use Default::default;` instead of needing a function.

There are some issues to work through, but I think many of them are the same as already exist for things like `use Result::Ok;`, so hopefully wouldn't be too bad.

(`zip` is a bit different here, since `iter::zip` takes `IntoIterator` on _both_ sides, and thus `zip([1, 2, 3], 10..)` works even though `[1, 2, 3].zip(10..)` doesn't.)

---

<div class="post-metadata">

**Author:** ![cameron1024](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cameron1024/32/8959_2.png) [@cameron1024](https://internals.rust-lang.org/u/cameron1024)\
**Post date:** [April 22, 2022, 12:02pm UTC](https://internals.rust-lang.org/t/thoughts-on-a-free-cmp-function/16504/7 "2022-04-22T12:02:43Z")

</div>

I think for now, it's definitely easier to just stick to using `Ord::cmp()`, `sort_by_key` IIRC doesn't work well for `String` (I remember running into lifetime issues a while ago and stumbled upon an issue that said it would need GATs to work with non-Copy types, but I might me misremembering).

Thanks for the suggestions 🙂

---

<div class="post-metadata">

**Author:** ![RagibHasin](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ragibhasin/32/9029_2.png) [@RagibHasin](https://internals.rust-lang.org/u/RagibHasin)\
**Post date:** [April 28, 2022, 5:16pm UTC](https://internals.rust-lang.org/t/thoughts-on-a-free-cmp-function/16504/8 "2022-04-28T17:16:09Z")

</div>

Is there any progress made on this RFC? It seems that it would keep modules clean from free functions like `default()` which just internally calls some other function.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [July 27, 2022, 5:17pm UTC](https://internals.rust-lang.org/t/thoughts-on-a-free-cmp-function/16504/9 "2022-07-27T17:17:07Z")

</div>

Closed on July 27, 2022, 5:17pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
