]> git.lizzy.rs Git - rust.git/commitdiff
doc: guarantee call order for sort_by_cached_key
authorMario Carneiro <di.gama@gmail.com>
Thu, 7 Oct 2021 10:33:00 +0000 (03:33 -0700)
committerGitHub <noreply@github.com>
Thu, 7 Oct 2021 10:33:00 +0000 (03:33 -0700)
`slice::sort_by_cached_key` takes a caching function
`f: impl FnMut(&T) -> K`, which means that the order that calls to the
caching function are made is user-visible. This adds a clause to the
documentation to promise the current behavior, which is that `f` is
called on all elements of the slice from left to right, unless the slice
has len < 2 in which case `f` is not called.

library/alloc/src/slice.rs

index 4c8ea6902ff14eea756de30a4f409ef97173d57f..58c35dfa3611e029c083c34c36c6f61f3d9dcfd3 100644 (file)
@@ -374,7 +374,8 @@ pub fn sort_by_key<K, F>(&mut self, mut f: F)
     /// During sorting, the key function is called only once per element.
     ///
     /// This sort is stable (i.e., does not reorder equal elements) and *O*(*m* \* *n* + *n* \* log(*n*))
-    /// worst-case, where the key function is *O*(*m*).
+    /// worst-case, where the key function is *O*(*m*). If the slice requires sorting,
+    /// the key function is called on all elements of the slice in the original order.
     ///
     /// For simple key functions (e.g., functions that are property accesses or
     /// basic operations), [`sort_by_key`](slice::sort_by_key) is likely to be