Skip to content

Commit 6233f3f

Browse files
committed
alloc: Added as_slice method to BinaryHeap collection
1 parent da5f7f1 commit 6233f3f

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

library/alloc/src/collections/binary_heap.rs

+23
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,29 @@ impl<T> BinaryHeap<T> {
889889
self.data.shrink_to(min_capacity)
890890
}
891891

892+
/// Returns a slice of all values in the underlying vector, in arbitrary
893+
/// order.
894+
///
895+
/// # Examples
896+
///
897+
/// Basic usage:
898+
///
899+
/// ```
900+
/// #![feature(binary_heap_as_slice)]
901+
/// use std::collections::BinaryHeap;
902+
/// let heap = BinaryHeap::from(vec![1, 2, 3, 4, 5, 6, 7]);
903+
/// let slice = heap.as_slice();
904+
///
905+
/// // Will print in some order
906+
/// for x in slice {
907+
/// println!("{}", x);
908+
/// }
909+
/// ```
910+
#[unstable(feature = "binary_heap_as_slice", issue = "82331")]
911+
pub fn as_slice(&self) -> &[T] {
912+
self.data.as_slice()
913+
}
914+
892915
/// Consumes the `BinaryHeap` and returns the underlying vector
893916
/// in arbitrary order.
894917
///

library/alloc/tests/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#![feature(binary_heap_drain_sorted)]
1616
#![feature(slice_ptr_get)]
1717
#![feature(binary_heap_retain)]
18+
#![feature(binary_heap_as_slice)]
1819
#![feature(inplace_iteration)]
1920
#![feature(iter_map_while)]
2021
#![feature(int_bits_const)]

0 commit comments

Comments
 (0)