-
Notifications
You must be signed in to change notification settings - Fork 13.8k
Add is_sorted
to Iterator
and [T]
#55045
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
8dea0d0
02477f6
ce47dde
ccc027e
67729b4
54f1124
b4766f8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# `is_sorted` | ||
|
||
The tracking issue for this feature is: [#53485] | ||
|
||
[#53485]: https://github.com/rust-lang/rust/issues/53485 | ||
|
||
------------------------ | ||
|
||
Add the methods `is_sorted`, `is_sorted_by` and `is_sorted_by_key` to `[T]`; | ||
add the methods `is_sorted`, `is_sorted_by` and `is_sorted_by_key` to | ||
`Iterator`. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2235,3 +2235,16 @@ fn test_monad_laws_associativity() { | |
assert_eq!((0..10).flat_map(f).flat_map(g).sum::<usize>(), | ||
(0..10).flat_map(|x| f(x).flat_map(g)).sum::<usize>()); | ||
} | ||
|
||
#[test] | ||
fn test_is_sorted() { | ||
assert!([1, 2, 2, 9].iter().is_sorted()); | ||
assert!(![1, 3, 2].iter().is_sorted()); | ||
assert!([0].iter().is_sorted()); | ||
assert!(std::iter::empty::<i32>().is_sorted()); | ||
assert!(![0.0, 1.0, std::f32::NAN].iter().is_sorted()); | ||
|
||
assert!([-2, -1, 0, 3].iter().is_sorted()); | ||
assert!(![-2i32, -1, 0, 3].iter().is_sorted_by_key(|n| n.abs())); | ||
assert!(!["c", "bb", "aaa"].iter().is_sorted()); | ||
assert!(["c", "bb", "aaa"].iter().is_sorted_by_key(|s| s.len())); | ||
} | ||
kleimkuhler marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1317,3 +1317,18 @@ fn test_copy_within_panics_src_inverted() { | |
// 2 is greater than 1, so this range is invalid. | ||
bytes.copy_within(2..1, 0); | ||
} | ||
|
||
#[test] | ||
fn test_is_sorted() { | ||
let empty: [i32; 0] = []; | ||
|
||
assert!([1, 2, 2, 9].is_sorted()); | ||
assert!(![1, 3, 2].is_sorted()); | ||
assert!([0].is_sorted()); | ||
assert!(empty.is_sorted()); | ||
assert!(![0.0, 1.0, std::f32::NAN].is_sorted()); | ||
assert!([-2, -1, 0, 3].is_sorted()); | ||
assert!(![-2i32, -1, 0, 3].is_sorted_by_key(|n| n.abs())); | ||
assert!(!["c", "bb", "aaa"].is_sorted()); | ||
assert!(["c", "bb", "aaa"].is_sorted_by_key(|s| s.len())); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
fn main() { | ||
// Assert `Iterator` methods are feature gated | ||
assert!([1, 2, 2, 9].iter().is_sorted()); | ||
//~^ ERROR: use of unstable library feature 'is_sorted': new API | ||
assert!(![-2i32, -1, 0, 3].iter().is_sorted_by_key(|n| n.abs())); | ||
//~^ ERROR: use of unstable library feature 'is_sorted': new API | ||
|
||
// Assert `[T]` methods are feature gated | ||
assert!([1, 2, 2, 9].is_sorted()); | ||
//~^ ERROR: use of unstable library feature 'is_sorted': new API | ||
assert!(![-2i32, -1, 0, 3].is_sorted_by_key(|n| n.abs())); | ||
//~^ ERROR: use of unstable library feature 'is_sorted': new API | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
error[E0658]: use of unstable library feature 'is_sorted': new API (see issue #53485) | ||
--> $DIR/feature-gate-is_sorted.rs:3:33 | ||
| | ||
LL | assert!([1, 2, 2, 9].iter().is_sorted()); | ||
| ^^^^^^^^^ | ||
| | ||
= help: add #![feature(is_sorted)] to the crate attributes to enable | ||
|
||
error[E0658]: use of unstable library feature 'is_sorted': new API (see issue #53485) | ||
--> $DIR/feature-gate-is_sorted.rs:5:39 | ||
| | ||
LL | assert!(![-2i32, -1, 0, 3].iter().is_sorted_by_key(|n| n.abs())); | ||
| ^^^^^^^^^^^^^^^^ | ||
| | ||
= help: add #![feature(is_sorted)] to the crate attributes to enable | ||
|
||
error[E0658]: use of unstable library feature 'is_sorted': new API (see issue #53485) | ||
--> $DIR/feature-gate-is_sorted.rs:9:26 | ||
| | ||
LL | assert!([1, 2, 2, 9].is_sorted()); | ||
| ^^^^^^^^^ | ||
| | ||
= help: add #![feature(is_sorted)] to the crate attributes to enable | ||
|
||
error[E0658]: use of unstable library feature 'is_sorted': new API (see issue #53485) | ||
--> $DIR/feature-gate-is_sorted.rs:11:32 | ||
| | ||
LL | assert!(![-2i32, -1, 0, 3].is_sorted_by_key(|n| n.abs())); | ||
| ^^^^^^^^^^^^^^^^ | ||
| | ||
= help: add #![feature(is_sorted)] to the crate attributes to enable | ||
|
||
error: aborting due to 4 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0658`. |
Uh oh!
There was an error while loading. Please reload this page.