23

I'm having difficulty with a seemingly trivial sort by String field. Repro below:

struct Dummy {
    x: String,
    y: i8
}

fn main() {
    let mut dummies: Vec<Dummy> = Vec::new();
    dummies.push(Dummy { x: "a".to_string(), y: 1 });
    dummies.push(Dummy { x: "b".to_string(), y: 2 });

    dummies.sort_by_key(|d| d.x); // error[E0507]: cannot move out of borrowed content
    dummies.sort_by_key(|d| d.y); // This is fine
}

Can someone please explain what exactly is going wrong and how to fix it?

1

3 Answers 3

29

First, let's look at your original error message, then we'll go through a few fixes and try to understand everything.

In the closure that you use in dummies.sort_by_key(|d| d.x);, d is a reference to a Dummy instance. However, the field access d.x is the String itself. If you wanted to return that String, you'd have to give ownership of it to whatever called the closure. But since d was just a reference, you can't pass ownership of its data.

One easy fix is to simply clone the string as dummies.sort_by_key(|d| d.x.clone());. This makes a copy of the string before returning it in the closure (this is Andra's solution). This works perfectly, but if performance or memory use is an issue, we can avoid the clone.

The idea here is that using the string as the key is wasteful. Really, all we need to know is which of two strings is smaller. If we use the string as a key, then every time the sort function needs to compare two Dummys, it calls the key function on each one and the strings are passed to a (very short) function that simply compares them. If we did the comparison in the same context as the borrow, we'd be able to simply pass the result of the comparison on, rather than the strings.

The solution is the sort_by method on slices. This allows us to take references to two Dummys and decide if one is smaller than the other. For example we can use it like dummies.sort_by(|d1, d2| d1.x.cmp(&d2.x)); (full example here)

Addendum

Why can't we use sort_by_key without cloning the Strings? Surely there must be some clever way of using string slices and lifetimes to do it.

Let's look at the signature of the sort_by_key function.

pub fn sort_by_key<K, F>(&mut self, f: F) where
    F: FnMut(&T) -> K,
    K: Ord, 

The interesting part of this function is not what is there, but what isn't there. The type parameter K doesn't depend on the lifetime of the reference passed to f.

As the slice is sorted, the key function gets repeatedly called with a reference to a Dummy instance. Since the slice is sorted a little between each call, the lifetime of the reference must be very short. If it were longer, it'd get invalidated the next time the elements of the slice were moved around. However, K can't depend on that lifetime. That means that whatever our key function is, it can't return anything that depends on the current location of the Dummy (e.g. a string slice, a reference, or any other clever construction1).

However, we could make K depend on the lifetime of whatever is passed to it. The idea here is what's called Higher-Rank Trait Bounds. These currently only work with lifetimes (though in theory they could be extended to all type parameters). We could posit another slice method with signature

fn sort_by_key_hrtb<T, F, K>(slice: &mut [T], f: F)
where
    F: Fn(&T) -> &K,
    K: Ord,

Why does this make things work? In F: Fn(&T) -> &K,, the lifetime of the output reference is implicitly the same as (or longer than) the lifetime of the input reference. Desugared, this is F: for<'a> Fn(&'a T) -> &'a K,, which says that f should be able to take a reference with any lifetime 'a and return a reference with lifetime (greater than or equal to) 'a. Now we have a method that works exactly how you wanted to use it (except for a pesky &2). (playground link)


  1. Actually, there is one (unsafe) clever construction that probably works, but I haven't vetted it. You can use a wrapper around a raw pointer to a String and then impl Ord for that wrapper so that it dereferences the pointer to do the comparison.3 The return type for the key function would be *const String, so we don't need any lifetimes. This is inherently unsafe though, and I definitely wouldn't recommend it. A (probably) working example is here.

  2. The only reason we need to use &mut dummies here is that sort_by_key_hrtb isn't actually a slice method. If it were, dummies would be automatically borrowed and dereferenced into a slice, so we could call the function like dummies.sort_by_key_hrtb(|d| &d.x);.

  3. Why a wrapper instead of just a pointer? *const T implements Ord, but it does so by comparing the addresses rather than the underlying value (if any), which isn't what we want here.

3
  • 1
    "Why can't we use sort_by_key without cloning the Strings?" - we can't use the reference here because underlying element is supposed to be moved. The only workaround is to use &str instead of String in Dummy (or something similar)
    – Laney
    May 13, 2019 at 6:09
  • 1
    @Laney, yup. I'm almost done writing that part of the answer.
    – SCappella
    May 13, 2019 at 6:13
  • Thanks for the explanation May 23, 2019 at 18:43
1

The sort_by_key function takes ownership of the key:

pub fn sort_by_key<K, F>(&mut self, f: F)

This is why you are getting error E0507.

A simple fix will be to store a reference on your struct so sort_by_key will not take the ownership of your key.

Then you need to had a lifetime to the referred value so she can be dropped when your struct is gone.

struct Dummy<'a> {
    x: &'a str,
    y: i8,
}

fn main() {
    let mut dummies: Vec<Dummy> = Vec::new();
    dummies.push(Dummy { x: "a", y: 1 });
    dummies.push(Dummy { x: "b", y: 2 });

    dummies.sort_by_key(|d| d.x);
    dummies.sort_by_key(|d| d.y);
}
1
0

I think it's because it's trying to move the String from one struct to another struct.

This works fine

struct Dummy {
    x: String,
    y: i8
}

fn main() {
    let mut dummies: Vec<Dummy> = Vec::new();
    dummies.push(Dummy { x: "a".to_string(), y: 1 });
    dummies.push(Dummy { x: "b".to_string(), y: 2 });

    dummies.sort_by_key(|d| d.x.clone()); // Clone the string
    dummies.sort_by_key(|d| d.y); // This is fine
}

The behavior might looks something like this

struct Dummy {
    x: String,
    y: i8
}

fn main() {
    let mut dummies: Vec<Dummy> = Vec::new();
    dummies.push(Dummy { x: "a".to_string(), y: 1 });
    dummies.push(Dummy { x: "b".to_string(), y: 2 });
    let mut temp = Dummy{ x: "c".to_string(), y: 3 };
    temp.x = dummies[0].x; // Error[E0507]: cannot move out of borrowed content
}

Using clone() like the example above

struct Dummy {
    x: String,
    y: i8
}

fn main() {
    let mut dummies: Vec<Dummy> = Vec::new();
    dummies.push(Dummy { x: "a".to_string(), y: 1 });
    dummies.push(Dummy { x: "b".to_string(), y: 2 });
    let mut temp = Dummy{ x: "c".to_string(), y: 3 };
    temp.x = dummies[0].x.clone(); // Fine
}
6
  • Is it possible to do the sorting using string slices (or avoiding the clone in some other way)?
    – SCappella
    May 13, 2019 at 4:46
  • Yes, take a look at this play.rust-lang.org/…
    – Andra
    May 13, 2019 at 4:51
  • What if the strings aren't static? (edit: I also meant that we would still use OP's Dummy, which has Strings. The string slice part was referring to passing a string slice in sort_by_key)
    – SCappella
    May 13, 2019 at 4:52
  • 1
    @User: I don't think you can do it with sort_by_key, because you would want to return a reference to the string, but you can't do that because sort_by_key is already borrowing the Dummy object mutably. However, you can do it with sort_by. play.rust-lang.org/… May 13, 2019 at 4:57
  • @BenjaminLindley Yeah I'm in the process of writing an answer to that effect (and your solution is exactly the same as mine =p).
    – SCappella
    May 13, 2019 at 4:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.