let host = self.host;
let compiler = builder.compiler(stage, host);
- builder.ensure(tool::Rls { compiler, target: self.host, extra_features: Vec::new() });
+ let build_result = builder.ensure(tool::Rls {
+ compiler,
+ target: self.host,
+ extra_features: Vec::new(),
+ });
+ if build_result.is_none() {
+ eprintln!("failed to test rls: could not build");
+ return;
+ }
+
let mut cargo = tool::prepare_tool_cargo(builder,
compiler,
host,
let host = self.host;
let compiler = builder.compiler(stage, host);
- builder.ensure(tool::Rustfmt { compiler, target: self.host, extra_features: Vec::new() });
+ let build_result = builder.ensure(tool::Rustfmt {
+ compiler,
+ target: self.host,
+ extra_features: Vec::new(),
+ });
+ if build_result.is_none() {
+ eprintln!("failed to test rustfmt: could not build");
+ return;
+ }
+
let mut cargo = tool::prepare_tool_cargo(builder,
compiler,
host,
`#[unstable]` attribute to any dependent crate that doesn't have another stability attribute. This
allows `rustdoc` to be able to generate documentation for the compiler crates and the standard
library, as an equivalent command-line argument is provided to `rustc` when building those crates.
+
+### `doc_alias` feature
+
+This feature allows you to add alias(es) to an item when using the `rustdoc` search through the
+`doc(alias)` attribute. Example:
+
+```rust,no_run
+#![feature(doc_alias)]
+
+#[doc(alias = "x")]
+#[doc(alias = "big")]
+pub struct BigX;
+```
+
+Then, when looking for it through the `rustdoc` search, if you enter "x" or
+"big", search will show the `BigX` struct first.
--- /dev/null
+# `doc_alias`
+
+The tracking issue for this feature is: [#50146]
+
+[#50146]: https://github.com/rust-lang/rust/issues/50146
+
+------------------------
+
+You can add alias(es) to an item when using the `rustdoc` search through the
+`doc(alias)` attribute. Example:
+
+```rust,no_run
+#![feature(doc_alias)]
+
+#[doc(alias = "x")]
+#[doc(alias = "big")]
+pub struct BigX;
+```
+
+Then, when looking for it through the `rustdoc` search, if you enter "x" or
+"big", search will show the `BigX` struct first.
+
+Note that this feature is currently hidden behind the `feature(doc_alias)` gate.
#![deny(missing_debug_implementations)]
#![cfg_attr(test, allow(deprecated))] // rand
-#![cfg_attr(not(test), feature(core_float))]
+#![cfg_attr(all(not(test), stage0), feature(float_internals))]
#![cfg_attr(not(test), feature(exact_size_is_empty))]
#![cfg_attr(not(test), feature(generator_trait))]
#![cfg_attr(test, feature(rand, test))]
#![feature(collections_range)]
#![feature(const_fn)]
#![feature(core_intrinsics)]
+#![cfg_attr(stage0, feature(core_slice_ext))]
+#![cfg_attr(stage0, feature(core_str_ext))]
#![feature(custom_attribute)]
#![feature(dropck_eyepatch)]
#![feature(exact_size_is_empty)]
use core::mem::size_of;
use core::mem;
use core::ptr;
-use core::slice as core_slice;
+#[cfg(stage0)] use core::slice::SliceExt;
use core::{u8, u16, u32};
use borrow::{Borrow, BorrowMut, ToOwned};
}
}
-#[lang = "slice"]
+#[cfg_attr(stage0, lang = "slice")]
+#[cfg_attr(not(stage0), lang = "slice_alloc")]
#[cfg(not(test))]
impl<T> [T] {
- /// Returns the number of elements in the slice.
- ///
- /// # Examples
- ///
- /// ```
- /// let a = [1, 2, 3];
- /// assert_eq!(a.len(), 3);
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn len(&self) -> usize {
- core_slice::SliceExt::len(self)
- }
-
- /// Returns `true` if the slice has a length of 0.
- ///
- /// # Examples
- ///
- /// ```
- /// let a = [1, 2, 3];
- /// assert!(!a.is_empty());
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn is_empty(&self) -> bool {
- core_slice::SliceExt::is_empty(self)
- }
-
- /// Returns the first element of the slice, or `None` if it is empty.
- ///
- /// # Examples
- ///
- /// ```
- /// let v = [10, 40, 30];
- /// assert_eq!(Some(&10), v.first());
- ///
- /// let w: &[i32] = &[];
- /// assert_eq!(None, w.first());
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn first(&self) -> Option<&T> {
- core_slice::SliceExt::first(self)
- }
-
- /// Returns a mutable pointer to the first element of the slice, or `None` if it is empty.
- ///
- /// # Examples
- ///
- /// ```
- /// let x = &mut [0, 1, 2];
- ///
- /// if let Some(first) = x.first_mut() {
- /// *first = 5;
- /// }
- /// assert_eq!(x, &[5, 1, 2]);
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn first_mut(&mut self) -> Option<&mut T> {
- core_slice::SliceExt::first_mut(self)
- }
-
- /// Returns the first and all the rest of the elements of the slice, or `None` if it is empty.
- ///
- /// # Examples
- ///
- /// ```
- /// let x = &[0, 1, 2];
- ///
- /// if let Some((first, elements)) = x.split_first() {
- /// assert_eq!(first, &0);
- /// assert_eq!(elements, &[1, 2]);
- /// }
- /// ```
- #[stable(feature = "slice_splits", since = "1.5.0")]
- #[inline]
- pub fn split_first(&self) -> Option<(&T, &[T])> {
- core_slice::SliceExt::split_first(self)
- }
-
- /// Returns the first and all the rest of the elements of the slice, or `None` if it is empty.
- ///
- /// # Examples
- ///
- /// ```
- /// let x = &mut [0, 1, 2];
- ///
- /// if let Some((first, elements)) = x.split_first_mut() {
- /// *first = 3;
- /// elements[0] = 4;
- /// elements[1] = 5;
- /// }
- /// assert_eq!(x, &[3, 4, 5]);
- /// ```
- #[stable(feature = "slice_splits", since = "1.5.0")]
- #[inline]
- pub fn split_first_mut(&mut self) -> Option<(&mut T, &mut [T])> {
- core_slice::SliceExt::split_first_mut(self)
- }
-
- /// Returns the last and all the rest of the elements of the slice, or `None` if it is empty.
- ///
- /// # Examples
- ///
- /// ```
- /// let x = &[0, 1, 2];
- ///
- /// if let Some((last, elements)) = x.split_last() {
- /// assert_eq!(last, &2);
- /// assert_eq!(elements, &[0, 1]);
- /// }
- /// ```
- #[stable(feature = "slice_splits", since = "1.5.0")]
- #[inline]
- pub fn split_last(&self) -> Option<(&T, &[T])> {
- core_slice::SliceExt::split_last(self)
-
- }
-
- /// Returns the last and all the rest of the elements of the slice, or `None` if it is empty.
- ///
- /// # Examples
- ///
- /// ```
- /// let x = &mut [0, 1, 2];
- ///
- /// if let Some((last, elements)) = x.split_last_mut() {
- /// *last = 3;
- /// elements[0] = 4;
- /// elements[1] = 5;
- /// }
- /// assert_eq!(x, &[4, 5, 3]);
- /// ```
- #[stable(feature = "slice_splits", since = "1.5.0")]
- #[inline]
- pub fn split_last_mut(&mut self) -> Option<(&mut T, &mut [T])> {
- core_slice::SliceExt::split_last_mut(self)
- }
-
- /// Returns the last element of the slice, or `None` if it is empty.
- ///
- /// # Examples
- ///
- /// ```
- /// let v = [10, 40, 30];
- /// assert_eq!(Some(&30), v.last());
- ///
- /// let w: &[i32] = &[];
- /// assert_eq!(None, w.last());
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn last(&self) -> Option<&T> {
- core_slice::SliceExt::last(self)
- }
-
- /// Returns a mutable pointer to the last item in the slice.
- ///
- /// # Examples
- ///
- /// ```
- /// let x = &mut [0, 1, 2];
- ///
- /// if let Some(last) = x.last_mut() {
- /// *last = 10;
- /// }
- /// assert_eq!(x, &[0, 1, 10]);
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn last_mut(&mut self) -> Option<&mut T> {
- core_slice::SliceExt::last_mut(self)
- }
-
- /// Returns a reference to an element or subslice depending on the type of
- /// index.
- ///
- /// - If given a position, returns a reference to the element at that
- /// position or `None` if out of bounds.
- /// - If given a range, returns the subslice corresponding to that range,
- /// or `None` if out of bounds.
- ///
- /// # Examples
- ///
- /// ```
- /// let v = [10, 40, 30];
- /// assert_eq!(Some(&40), v.get(1));
- /// assert_eq!(Some(&[10, 40][..]), v.get(0..2));
- /// assert_eq!(None, v.get(3));
- /// assert_eq!(None, v.get(0..4));
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn get<I>(&self, index: I) -> Option<&I::Output>
- where I: SliceIndex<Self>
- {
- core_slice::SliceExt::get(self, index)
- }
-
- /// Returns a mutable reference to an element or subslice depending on the
- /// type of index (see [`get`]) or `None` if the index is out of bounds.
- ///
- /// [`get`]: #method.get
- ///
- /// # Examples
- ///
- /// ```
- /// let x = &mut [0, 1, 2];
- ///
- /// if let Some(elem) = x.get_mut(1) {
- /// *elem = 42;
- /// }
- /// assert_eq!(x, &[0, 42, 2]);
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn get_mut<I>(&mut self, index: I) -> Option<&mut I::Output>
- where I: SliceIndex<Self>
- {
- core_slice::SliceExt::get_mut(self, index)
- }
-
- /// Returns a reference to an element or subslice, without doing bounds
- /// checking.
- ///
- /// This is generally not recommended, use with caution! For a safe
- /// alternative see [`get`].
- ///
- /// [`get`]: #method.get
- ///
- /// # Examples
- ///
- /// ```
- /// let x = &[1, 2, 4];
- ///
- /// unsafe {
- /// assert_eq!(x.get_unchecked(1), &2);
- /// }
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub unsafe fn get_unchecked<I>(&self, index: I) -> &I::Output
- where I: SliceIndex<Self>
- {
- core_slice::SliceExt::get_unchecked(self, index)
- }
-
- /// Returns a mutable reference to an element or subslice, without doing
- /// bounds checking.
- ///
- /// This is generally not recommended, use with caution! For a safe
- /// alternative see [`get_mut`].
- ///
- /// [`get_mut`]: #method.get_mut
- ///
- /// # Examples
- ///
- /// ```
- /// let x = &mut [1, 2, 4];
- ///
- /// unsafe {
- /// let elem = x.get_unchecked_mut(1);
- /// *elem = 13;
- /// }
- /// assert_eq!(x, &[1, 13, 4]);
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub unsafe fn get_unchecked_mut<I>(&mut self, index: I) -> &mut I::Output
- where I: SliceIndex<Self>
- {
- core_slice::SliceExt::get_unchecked_mut(self, index)
- }
-
- /// Returns a raw pointer to the slice's buffer.
- ///
- /// The caller must ensure that the slice outlives the pointer this
- /// function returns, or else it will end up pointing to garbage.
- ///
- /// Modifying the container referenced by this slice may cause its buffer
- /// to be reallocated, which would also make any pointers to it invalid.
- ///
- /// # Examples
- ///
- /// ```
- /// let x = &[1, 2, 4];
- /// let x_ptr = x.as_ptr();
- ///
- /// unsafe {
- /// for i in 0..x.len() {
- /// assert_eq!(x.get_unchecked(i), &*x_ptr.offset(i as isize));
- /// }
- /// }
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn as_ptr(&self) -> *const T {
- core_slice::SliceExt::as_ptr(self)
- }
-
- /// Returns an unsafe mutable pointer to the slice's buffer.
- ///
- /// The caller must ensure that the slice outlives the pointer this
- /// function returns, or else it will end up pointing to garbage.
- ///
- /// Modifying the container referenced by this slice may cause its buffer
- /// to be reallocated, which would also make any pointers to it invalid.
- ///
- /// # Examples
- ///
- /// ```
- /// let x = &mut [1, 2, 4];
- /// let x_ptr = x.as_mut_ptr();
- ///
- /// unsafe {
- /// for i in 0..x.len() {
- /// *x_ptr.offset(i as isize) += 2;
- /// }
- /// }
- /// assert_eq!(x, &[3, 4, 6]);
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn as_mut_ptr(&mut self) -> *mut T {
- core_slice::SliceExt::as_mut_ptr(self)
- }
-
- /// Swaps two elements in the slice.
- ///
- /// # Arguments
- ///
- /// * a - The index of the first element
- /// * b - The index of the second element
- ///
- /// # Panics
- ///
- /// Panics if `a` or `b` are out of bounds.
- ///
- /// # Examples
- ///
- /// ```
- /// let mut v = ["a", "b", "c", "d"];
- /// v.swap(1, 3);
- /// assert!(v == ["a", "d", "c", "b"]);
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn swap(&mut self, a: usize, b: usize) {
- core_slice::SliceExt::swap(self, a, b)
- }
-
- /// Reverses the order of elements in the slice, in place.
- ///
- /// # Examples
- ///
- /// ```
- /// let mut v = [1, 2, 3];
- /// v.reverse();
- /// assert!(v == [3, 2, 1]);
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn reverse(&mut self) {
- core_slice::SliceExt::reverse(self)
- }
-
- /// Returns an iterator over the slice.
- ///
- /// # Examples
- ///
- /// ```
- /// let x = &[1, 2, 4];
- /// let mut iterator = x.iter();
- ///
- /// assert_eq!(iterator.next(), Some(&1));
- /// assert_eq!(iterator.next(), Some(&2));
- /// assert_eq!(iterator.next(), Some(&4));
- /// assert_eq!(iterator.next(), None);
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn iter(&self) -> Iter<T> {
- core_slice::SliceExt::iter(self)
- }
-
- /// Returns an iterator that allows modifying each value.
- ///
- /// # Examples
- ///
- /// ```
- /// let x = &mut [1, 2, 4];
- /// for elem in x.iter_mut() {
- /// *elem += 2;
- /// }
- /// assert_eq!(x, &[3, 4, 6]);
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn iter_mut(&mut self) -> IterMut<T> {
- core_slice::SliceExt::iter_mut(self)
- }
-
- /// Returns an iterator over all contiguous windows of length
- /// `size`. The windows overlap. If the slice is shorter than
- /// `size`, the iterator returns no values.
- ///
- /// # Panics
- ///
- /// Panics if `size` is 0.
- ///
- /// # Examples
- ///
- /// ```
- /// let slice = ['r', 'u', 's', 't'];
- /// let mut iter = slice.windows(2);
- /// assert_eq!(iter.next().unwrap(), &['r', 'u']);
- /// assert_eq!(iter.next().unwrap(), &['u', 's']);
- /// assert_eq!(iter.next().unwrap(), &['s', 't']);
- /// assert!(iter.next().is_none());
- /// ```
- ///
- /// If the slice is shorter than `size`:
- ///
- /// ```
- /// let slice = ['f', 'o', 'o'];
- /// let mut iter = slice.windows(4);
- /// assert!(iter.next().is_none());
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn windows(&self, size: usize) -> Windows<T> {
- core_slice::SliceExt::windows(self, size)
- }
-
- /// Returns an iterator over `chunk_size` elements of the slice at a
- /// time. The chunks are slices and do not overlap. If `chunk_size` does
- /// not divide the length of the slice, then the last chunk will
- /// not have length `chunk_size`.
- ///
- /// See [`exact_chunks`] for a variant of this iterator that returns chunks
- /// of always exactly `chunk_size` elements.
- ///
- /// # Panics
- ///
- /// Panics if `chunk_size` is 0.
- ///
- /// # Examples
- ///
- /// ```
- /// let slice = ['l', 'o', 'r', 'e', 'm'];
- /// let mut iter = slice.chunks(2);
- /// assert_eq!(iter.next().unwrap(), &['l', 'o']);
- /// assert_eq!(iter.next().unwrap(), &['r', 'e']);
- /// assert_eq!(iter.next().unwrap(), &['m']);
- /// assert!(iter.next().is_none());
- /// ```
- ///
- /// [`exact_chunks`]: #method.exact_chunks
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn chunks(&self, chunk_size: usize) -> Chunks<T> {
- core_slice::SliceExt::chunks(self, chunk_size)
- }
-
- /// Returns an iterator over `chunk_size` elements of the slice at a
- /// time. The chunks are slices and do not overlap. If `chunk_size` does
- /// not divide the length of the slice, then the last up to `chunk_size-1`
- /// elements will be omitted.
- ///
- /// Due to each chunk having exactly `chunk_size` elements, the compiler
- /// can often optimize the resulting code better than in the case of
- /// [`chunks`].
- ///
- /// # Panics
- ///
- /// Panics if `chunk_size` is 0.
- ///
- /// # Examples
- ///
- /// ```
- /// #![feature(exact_chunks)]
- ///
- /// let slice = ['l', 'o', 'r', 'e', 'm'];
- /// let mut iter = slice.exact_chunks(2);
- /// assert_eq!(iter.next().unwrap(), &['l', 'o']);
- /// assert_eq!(iter.next().unwrap(), &['r', 'e']);
- /// assert!(iter.next().is_none());
- /// ```
- ///
- /// [`chunks`]: #method.chunks
- #[unstable(feature = "exact_chunks", issue = "47115")]
- #[inline]
- pub fn exact_chunks(&self, chunk_size: usize) -> ExactChunks<T> {
- core_slice::SliceExt::exact_chunks(self, chunk_size)
- }
-
- /// Returns an iterator over `chunk_size` elements of the slice at a time.
- /// The chunks are mutable slices, and do not overlap. If `chunk_size` does
- /// not divide the length of the slice, then the last chunk will not
- /// have length `chunk_size`.
- ///
- /// See [`exact_chunks_mut`] for a variant of this iterator that returns chunks
- /// of always exactly `chunk_size` elements.
- ///
- /// # Panics
- ///
- /// Panics if `chunk_size` is 0.
- ///
- /// # Examples
- ///
- /// ```
- /// let v = &mut [0, 0, 0, 0, 0];
- /// let mut count = 1;
- ///
- /// for chunk in v.chunks_mut(2) {
- /// for elem in chunk.iter_mut() {
- /// *elem += count;
- /// }
- /// count += 1;
- /// }
- /// assert_eq!(v, &[1, 1, 2, 2, 3]);
- /// ```
- ///
- /// [`exact_chunks_mut`]: #method.exact_chunks_mut
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn chunks_mut(&mut self, chunk_size: usize) -> ChunksMut<T> {
- core_slice::SliceExt::chunks_mut(self, chunk_size)
- }
-
- /// Returns an iterator over `chunk_size` elements of the slice at a time.
- /// The chunks are mutable slices, and do not overlap. If `chunk_size` does
- /// not divide the length of the slice, then the last up to `chunk_size-1`
- /// elements will be omitted.
- ///
- ///
- /// Due to each chunk having exactly `chunk_size` elements, the compiler
- /// can often optimize the resulting code better than in the case of
- /// [`chunks_mut`].
- ///
- /// # Panics
- ///
- /// Panics if `chunk_size` is 0.
- ///
- /// # Examples
- ///
- /// ```
- /// #![feature(exact_chunks)]
- ///
- /// let v = &mut [0, 0, 0, 0, 0];
- /// let mut count = 1;
- ///
- /// for chunk in v.exact_chunks_mut(2) {
- /// for elem in chunk.iter_mut() {
- /// *elem += count;
- /// }
- /// count += 1;
- /// }
- /// assert_eq!(v, &[1, 1, 2, 2, 0]);
- /// ```
- ///
- /// [`chunks_mut`]: #method.chunks_mut
- #[unstable(feature = "exact_chunks", issue = "47115")]
- #[inline]
- pub fn exact_chunks_mut(&mut self, chunk_size: usize) -> ExactChunksMut<T> {
- core_slice::SliceExt::exact_chunks_mut(self, chunk_size)
- }
-
- /// Divides one slice into two at an index.
- ///
- /// The first will contain all indices from `[0, mid)` (excluding
- /// the index `mid` itself) and the second will contain all
- /// indices from `[mid, len)` (excluding the index `len` itself).
- ///
- /// # Panics
- ///
- /// Panics if `mid > len`.
- ///
- /// # Examples
- ///
- /// ```
- /// let v = [1, 2, 3, 4, 5, 6];
- ///
- /// {
- /// let (left, right) = v.split_at(0);
- /// assert!(left == []);
- /// assert!(right == [1, 2, 3, 4, 5, 6]);
- /// }
- ///
- /// {
- /// let (left, right) = v.split_at(2);
- /// assert!(left == [1, 2]);
- /// assert!(right == [3, 4, 5, 6]);
- /// }
- ///
- /// {
- /// let (left, right) = v.split_at(6);
- /// assert!(left == [1, 2, 3, 4, 5, 6]);
- /// assert!(right == []);
- /// }
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn split_at(&self, mid: usize) -> (&[T], &[T]) {
- core_slice::SliceExt::split_at(self, mid)
- }
-
- /// Divides one mutable slice into two at an index.
- ///
- /// The first will contain all indices from `[0, mid)` (excluding
- /// the index `mid` itself) and the second will contain all
- /// indices from `[mid, len)` (excluding the index `len` itself).
- ///
- /// # Panics
- ///
- /// Panics if `mid > len`.
- ///
- /// # Examples
- ///
- /// ```
- /// let mut v = [1, 0, 3, 0, 5, 6];
- /// // scoped to restrict the lifetime of the borrows
- /// {
- /// let (left, right) = v.split_at_mut(2);
- /// assert!(left == [1, 0]);
- /// assert!(right == [3, 0, 5, 6]);
- /// left[1] = 2;
- /// right[1] = 4;
- /// }
- /// assert!(v == [1, 2, 3, 4, 5, 6]);
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn split_at_mut(&mut self, mid: usize) -> (&mut [T], &mut [T]) {
- core_slice::SliceExt::split_at_mut(self, mid)
- }
-
- /// Returns an iterator over subslices separated by elements that match
- /// `pred`. The matched element is not contained in the subslices.
- ///
- /// # Examples
- ///
- /// ```
- /// let slice = [10, 40, 33, 20];
- /// let mut iter = slice.split(|num| num % 3 == 0);
- ///
- /// assert_eq!(iter.next().unwrap(), &[10, 40]);
- /// assert_eq!(iter.next().unwrap(), &[20]);
- /// assert!(iter.next().is_none());
- /// ```
- ///
- /// If the first element is matched, an empty slice will be the first item
- /// returned by the iterator. Similarly, if the last element in the slice
- /// is matched, an empty slice will be the last item returned by the
- /// iterator:
- ///
- /// ```
- /// let slice = [10, 40, 33];
- /// let mut iter = slice.split(|num| num % 3 == 0);
- ///
- /// assert_eq!(iter.next().unwrap(), &[10, 40]);
- /// assert_eq!(iter.next().unwrap(), &[]);
- /// assert!(iter.next().is_none());
- /// ```
- ///
- /// If two matched elements are directly adjacent, an empty slice will be
- /// present between them:
- ///
- /// ```
- /// let slice = [10, 6, 33, 20];
- /// let mut iter = slice.split(|num| num % 3 == 0);
- ///
- /// assert_eq!(iter.next().unwrap(), &[10]);
- /// assert_eq!(iter.next().unwrap(), &[]);
- /// assert_eq!(iter.next().unwrap(), &[20]);
- /// assert!(iter.next().is_none());
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn split<F>(&self, pred: F) -> Split<T, F>
- where F: FnMut(&T) -> bool
- {
- core_slice::SliceExt::split(self, pred)
- }
-
- /// Returns an iterator over mutable subslices separated by elements that
- /// match `pred`. The matched element is not contained in the subslices.
- ///
- /// # Examples
- ///
- /// ```
- /// let mut v = [10, 40, 30, 20, 60, 50];
- ///
- /// for group in v.split_mut(|num| *num % 3 == 0) {
- /// group[0] = 1;
- /// }
- /// assert_eq!(v, [1, 40, 30, 1, 60, 1]);
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn split_mut<F>(&mut self, pred: F) -> SplitMut<T, F>
- where F: FnMut(&T) -> bool
- {
- core_slice::SliceExt::split_mut(self, pred)
- }
-
- /// Returns an iterator over subslices separated by elements that match
- /// `pred`, starting at the end of the slice and working backwards.
- /// The matched element is not contained in the subslices.
- ///
- /// # Examples
- ///
- /// ```
- ///
- /// let slice = [11, 22, 33, 0, 44, 55];
- /// let mut iter = slice.rsplit(|num| *num == 0);
- ///
- /// assert_eq!(iter.next().unwrap(), &[44, 55]);
- /// assert_eq!(iter.next().unwrap(), &[11, 22, 33]);
- /// assert_eq!(iter.next(), None);
- /// ```
- ///
- /// As with `split()`, if the first or last element is matched, an empty
- /// slice will be the first (or last) item returned by the iterator.
- ///
- /// ```
- /// let v = &[0, 1, 1, 2, 3, 5, 8];
- /// let mut it = v.rsplit(|n| *n % 2 == 0);
- /// assert_eq!(it.next().unwrap(), &[]);
- /// assert_eq!(it.next().unwrap(), &[3, 5]);
- /// assert_eq!(it.next().unwrap(), &[1, 1]);
- /// assert_eq!(it.next().unwrap(), &[]);
- /// assert_eq!(it.next(), None);
- /// ```
- #[stable(feature = "slice_rsplit", since = "1.27.0")]
- #[inline]
- pub fn rsplit<F>(&self, pred: F) -> RSplit<T, F>
- where F: FnMut(&T) -> bool
- {
- core_slice::SliceExt::rsplit(self, pred)
- }
-
- /// Returns an iterator over mutable subslices separated by elements that
- /// match `pred`, starting at the end of the slice and working
- /// backwards. The matched element is not contained in the subslices.
- ///
- /// # Examples
- ///
- /// ```
- /// let mut v = [100, 400, 300, 200, 600, 500];
- ///
- /// let mut count = 0;
- /// for group in v.rsplit_mut(|num| *num % 3 == 0) {
- /// count += 1;
- /// group[0] = count;
- /// }
- /// assert_eq!(v, [3, 400, 300, 2, 600, 1]);
- /// ```
- ///
- #[stable(feature = "slice_rsplit", since = "1.27.0")]
- #[inline]
- pub fn rsplit_mut<F>(&mut self, pred: F) -> RSplitMut<T, F>
- where F: FnMut(&T) -> bool
- {
- core_slice::SliceExt::rsplit_mut(self, pred)
- }
-
- /// Returns an iterator over subslices separated by elements that match
- /// `pred`, limited to returning at most `n` items. The matched element is
- /// not contained in the subslices.
- ///
- /// The last element returned, if any, will contain the remainder of the
- /// slice.
- ///
- /// # Examples
- ///
- /// Print the slice split once by numbers divisible by 3 (i.e. `[10, 40]`,
- /// `[20, 60, 50]`):
- ///
- /// ```
- /// let v = [10, 40, 30, 20, 60, 50];
- ///
- /// for group in v.splitn(2, |num| *num % 3 == 0) {
- /// println!("{:?}", group);
- /// }
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn splitn<F>(&self, n: usize, pred: F) -> SplitN<T, F>
- where F: FnMut(&T) -> bool
- {
- core_slice::SliceExt::splitn(self, n, pred)
- }
-
- /// Returns an iterator over subslices separated by elements that match
- /// `pred`, limited to returning at most `n` items. The matched element is
- /// not contained in the subslices.
- ///
- /// The last element returned, if any, will contain the remainder of the
- /// slice.
- ///
- /// # Examples
- ///
- /// ```
- /// let mut v = [10, 40, 30, 20, 60, 50];
- ///
- /// for group in v.splitn_mut(2, |num| *num % 3 == 0) {
- /// group[0] = 1;
- /// }
- /// assert_eq!(v, [1, 40, 30, 1, 60, 50]);
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn splitn_mut<F>(&mut self, n: usize, pred: F) -> SplitNMut<T, F>
- where F: FnMut(&T) -> bool
- {
- core_slice::SliceExt::splitn_mut(self, n, pred)
- }
-
- /// Returns an iterator over subslices separated by elements that match
- /// `pred` limited to returning at most `n` items. This starts at the end of
- /// the slice and works backwards. The matched element is not contained in
- /// the subslices.
- ///
- /// The last element returned, if any, will contain the remainder of the
- /// slice.
- ///
- /// # Examples
- ///
- /// Print the slice split once, starting from the end, by numbers divisible
- /// by 3 (i.e. `[50]`, `[10, 40, 30, 20]`):
- ///
- /// ```
- /// let v = [10, 40, 30, 20, 60, 50];
- ///
- /// for group in v.rsplitn(2, |num| *num % 3 == 0) {
- /// println!("{:?}", group);
- /// }
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn rsplitn<F>(&self, n: usize, pred: F) -> RSplitN<T, F>
- where F: FnMut(&T) -> bool
- {
- core_slice::SliceExt::rsplitn(self, n, pred)
- }
-
- /// Returns an iterator over subslices separated by elements that match
- /// `pred` limited to returning at most `n` items. This starts at the end of
- /// the slice and works backwards. The matched element is not contained in
- /// the subslices.
- ///
- /// The last element returned, if any, will contain the remainder of the
- /// slice.
- ///
- /// # Examples
- ///
- /// ```
- /// let mut s = [10, 40, 30, 20, 60, 50];
- ///
- /// for group in s.rsplitn_mut(2, |num| *num % 3 == 0) {
- /// group[0] = 1;
- /// }
- /// assert_eq!(s, [1, 40, 30, 20, 60, 1]);
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn rsplitn_mut<F>(&mut self, n: usize, pred: F) -> RSplitNMut<T, F>
- where F: FnMut(&T) -> bool
- {
- core_slice::SliceExt::rsplitn_mut(self, n, pred)
- }
-
- /// Returns `true` if the slice contains an element with the given value.
- ///
- /// # Examples
- ///
- /// ```
- /// let v = [10, 40, 30];
- /// assert!(v.contains(&30));
- /// assert!(!v.contains(&50));
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- pub fn contains(&self, x: &T) -> bool
- where T: PartialEq
- {
- core_slice::SliceExt::contains(self, x)
- }
-
- /// Returns `true` if `needle` is a prefix of the slice.
- ///
- /// # Examples
- ///
- /// ```
- /// let v = [10, 40, 30];
- /// assert!(v.starts_with(&[10]));
- /// assert!(v.starts_with(&[10, 40]));
- /// assert!(!v.starts_with(&[50]));
- /// assert!(!v.starts_with(&[10, 50]));
- /// ```
- ///
- /// Always returns `true` if `needle` is an empty slice:
- ///
- /// ```
- /// let v = &[10, 40, 30];
- /// assert!(v.starts_with(&[]));
- /// let v: &[u8] = &[];
- /// assert!(v.starts_with(&[]));
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- pub fn starts_with(&self, needle: &[T]) -> bool
- where T: PartialEq
- {
- core_slice::SliceExt::starts_with(self, needle)
- }
-
- /// Returns `true` if `needle` is a suffix of the slice.
- ///
- /// # Examples
- ///
- /// ```
- /// let v = [10, 40, 30];
- /// assert!(v.ends_with(&[30]));
- /// assert!(v.ends_with(&[40, 30]));
- /// assert!(!v.ends_with(&[50]));
- /// assert!(!v.ends_with(&[50, 30]));
- /// ```
- ///
- /// Always returns `true` if `needle` is an empty slice:
- ///
- /// ```
- /// let v = &[10, 40, 30];
- /// assert!(v.ends_with(&[]));
- /// let v: &[u8] = &[];
- /// assert!(v.ends_with(&[]));
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- pub fn ends_with(&self, needle: &[T]) -> bool
- where T: PartialEq
- {
- core_slice::SliceExt::ends_with(self, needle)
- }
-
- /// Binary searches this sorted slice for a given element.
- ///
- /// If the value is found then `Ok` is returned, containing the
- /// index of the matching element; if the value is not found then
- /// `Err` is returned, containing the index where a matching
- /// element could be inserted while maintaining sorted order.
- ///
- /// # Examples
- ///
- /// Looks up a series of four elements. The first is found, with a
- /// uniquely determined position; the second and third are not
- /// found; the fourth could match any position in `[1, 4]`.
- ///
- /// ```
- /// let s = [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55];
- ///
- /// assert_eq!(s.binary_search(&13), Ok(9));
- /// assert_eq!(s.binary_search(&4), Err(7));
- /// assert_eq!(s.binary_search(&100), Err(13));
- /// let r = s.binary_search(&1);
- /// assert!(match r { Ok(1...4) => true, _ => false, });
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- pub fn binary_search(&self, x: &T) -> Result<usize, usize>
- where T: Ord
- {
- core_slice::SliceExt::binary_search(self, x)
- }
-
- /// Binary searches this sorted slice with a comparator function.
- ///
- /// The comparator function should implement an order consistent
- /// with the sort order of the underlying slice, returning an
- /// order code that indicates whether its argument is `Less`,
- /// `Equal` or `Greater` the desired target.
- ///
- /// If a matching value is found then returns `Ok`, containing
- /// the index for the matched element; if no match is found then
- /// `Err` is returned, containing the index where a matching
- /// element could be inserted while maintaining sorted order.
- ///
- /// # Examples
- ///
- /// Looks up a series of four elements. The first is found, with a
- /// uniquely determined position; the second and third are not
- /// found; the fourth could match any position in `[1, 4]`.
- ///
- /// ```
- /// let s = [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55];
- ///
- /// let seek = 13;
- /// assert_eq!(s.binary_search_by(|probe| probe.cmp(&seek)), Ok(9));
- /// let seek = 4;
- /// assert_eq!(s.binary_search_by(|probe| probe.cmp(&seek)), Err(7));
- /// let seek = 100;
- /// assert_eq!(s.binary_search_by(|probe| probe.cmp(&seek)), Err(13));
- /// let seek = 1;
- /// let r = s.binary_search_by(|probe| probe.cmp(&seek));
- /// assert!(match r { Ok(1...4) => true, _ => false, });
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn binary_search_by<'a, F>(&'a self, f: F) -> Result<usize, usize>
- where F: FnMut(&'a T) -> Ordering
- {
- core_slice::SliceExt::binary_search_by(self, f)
- }
-
- /// Binary searches this sorted slice with a key extraction function.
- ///
- /// Assumes that the slice is sorted by the key, for instance with
- /// [`sort_by_key`] using the same key extraction function.
- ///
- /// If a matching value is found then returns `Ok`, containing the
- /// index for the matched element; if no match is found then `Err`
- /// is returned, containing the index where a matching element could
- /// be inserted while maintaining sorted order.
- ///
- /// [`sort_by_key`]: #method.sort_by_key
- ///
- /// # Examples
- ///
- /// Looks up a series of four elements in a slice of pairs sorted by
- /// their second elements. The first is found, with a uniquely
- /// determined position; the second and third are not found; the
- /// fourth could match any position in `[1, 4]`.
- ///
- /// ```
- /// let s = [(0, 0), (2, 1), (4, 1), (5, 1), (3, 1),
- /// (1, 2), (2, 3), (4, 5), (5, 8), (3, 13),
- /// (1, 21), (2, 34), (4, 55)];
- ///
- /// assert_eq!(s.binary_search_by_key(&13, |&(a,b)| b), Ok(9));
- /// assert_eq!(s.binary_search_by_key(&4, |&(a,b)| b), Err(7));
- /// assert_eq!(s.binary_search_by_key(&100, |&(a,b)| b), Err(13));
- /// let r = s.binary_search_by_key(&1, |&(a,b)| b);
- /// assert!(match r { Ok(1...4) => true, _ => false, });
- /// ```
- #[stable(feature = "slice_binary_search_by_key", since = "1.10.0")]
- #[inline]
- pub fn binary_search_by_key<'a, B, F>(&'a self, b: &B, f: F) -> Result<usize, usize>
- where F: FnMut(&'a T) -> B,
- B: Ord
- {
- core_slice::SliceExt::binary_search_by_key(self, b, f)
- }
+ #[cfg(stage0)]
+ slice_core_methods!();
/// Sorts the slice.
///
sort_by_key!(usize, self, f)
}
- /// Sorts the slice, but may not preserve the order of equal elements.
- ///
- /// This sort is unstable (i.e. may reorder equal elements), in-place (i.e. does not allocate),
- /// and `O(n log n)` worst-case.
- ///
- /// # Current implementation
- ///
- /// The current algorithm is based on [pattern-defeating quicksort][pdqsort] by Orson Peters,
- /// which combines the fast average case of randomized quicksort with the fast worst case of
- /// heapsort, while achieving linear time on slices with certain patterns. It uses some
- /// randomization to avoid degenerate cases, but with a fixed seed to always provide
- /// deterministic behavior.
- ///
- /// It is typically faster than stable sorting, except in a few special cases, e.g. when the
- /// slice consists of several concatenated sorted sequences.
- ///
- /// # Examples
- ///
- /// ```
- /// let mut v = [-5, 4, 1, -3, 2];
- ///
- /// v.sort_unstable();
- /// assert!(v == [-5, -3, 1, 2, 4]);
- /// ```
- ///
- /// [pdqsort]: https://github.com/orlp/pdqsort
- #[stable(feature = "sort_unstable", since = "1.20.0")]
- #[inline]
- pub fn sort_unstable(&mut self)
- where T: Ord
- {
- core_slice::SliceExt::sort_unstable(self);
- }
-
- /// Sorts the slice with a comparator function, but may not preserve the order of equal
- /// elements.
- ///
- /// This sort is unstable (i.e. may reorder equal elements), in-place (i.e. does not allocate),
- /// and `O(n log n)` worst-case.
- ///
- /// # Current implementation
- ///
- /// The current algorithm is based on [pattern-defeating quicksort][pdqsort] by Orson Peters,
- /// which combines the fast average case of randomized quicksort with the fast worst case of
- /// heapsort, while achieving linear time on slices with certain patterns. It uses some
- /// randomization to avoid degenerate cases, but with a fixed seed to always provide
- /// deterministic behavior.
- ///
- /// It is typically faster than stable sorting, except in a few special cases, e.g. when the
- /// slice consists of several concatenated sorted sequences.
- ///
- /// # Examples
- ///
- /// ```
- /// let mut v = [5, 4, 1, 3, 2];
- /// v.sort_unstable_by(|a, b| a.cmp(b));
- /// assert!(v == [1, 2, 3, 4, 5]);
- ///
- /// // reverse sorting
- /// v.sort_unstable_by(|a, b| b.cmp(a));
- /// assert!(v == [5, 4, 3, 2, 1]);
- /// ```
- ///
- /// [pdqsort]: https://github.com/orlp/pdqsort
- #[stable(feature = "sort_unstable", since = "1.20.0")]
- #[inline]
- pub fn sort_unstable_by<F>(&mut self, compare: F)
- where F: FnMut(&T, &T) -> Ordering
- {
- core_slice::SliceExt::sort_unstable_by(self, compare);
- }
-
- /// Sorts the slice with a key extraction function, but may not preserve the order of equal
- /// elements.
- ///
- /// This sort is unstable (i.e. may reorder equal elements), in-place (i.e. does not allocate),
- /// and `O(m n log(m n))` worst-case, where the key function is `O(m)`.
- ///
- /// # Current implementation
- ///
- /// The current algorithm is based on [pattern-defeating quicksort][pdqsort] by Orson Peters,
- /// which combines the fast average case of randomized quicksort with the fast worst case of
- /// heapsort, while achieving linear time on slices with certain patterns. It uses some
- /// randomization to avoid degenerate cases, but with a fixed seed to always provide
- /// deterministic behavior.
- ///
- /// # Examples
- ///
- /// ```
- /// let mut v = [-5i32, 4, 1, -3, 2];
- ///
- /// v.sort_unstable_by_key(|k| k.abs());
- /// assert!(v == [1, 2, -3, 4, -5]);
- /// ```
- ///
- /// [pdqsort]: https://github.com/orlp/pdqsort
- #[stable(feature = "sort_unstable", since = "1.20.0")]
- #[inline]
- pub fn sort_unstable_by_key<K, F>(&mut self, f: F)
- where F: FnMut(&T) -> K, K: Ord
- {
- core_slice::SliceExt::sort_unstable_by_key(self, f);
- }
-
- /// Rotates the slice in-place such that the first `mid` elements of the
- /// slice move to the end while the last `self.len() - mid` elements move to
- /// the front. After calling `rotate_left`, the element previously at index
- /// `mid` will become the first element in the slice.
- ///
- /// # Panics
- ///
- /// This function will panic if `mid` is greater than the length of the
- /// slice. Note that `mid == self.len()` does _not_ panic and is a no-op
- /// rotation.
- ///
- /// # Complexity
- ///
- /// Takes linear (in `self.len()`) time.
- ///
- /// # Examples
- ///
- /// ```
- /// let mut a = ['a', 'b', 'c', 'd', 'e', 'f'];
- /// a.rotate_left(2);
- /// assert_eq!(a, ['c', 'd', 'e', 'f', 'a', 'b']);
- /// ```
- ///
- /// Rotating a subslice:
- ///
- /// ```
- /// let mut a = ['a', 'b', 'c', 'd', 'e', 'f'];
- /// a[1..5].rotate_left(1);
- /// assert_eq!(a, ['a', 'c', 'd', 'e', 'b', 'f']);
- /// ```
- #[stable(feature = "slice_rotate", since = "1.26.0")]
- pub fn rotate_left(&mut self, mid: usize) {
- core_slice::SliceExt::rotate_left(self, mid);
- }
-
- /// Rotates the slice in-place such that the first `self.len() - k`
- /// elements of the slice move to the end while the last `k` elements move
- /// to the front. After calling `rotate_right`, the element previously at
- /// index `self.len() - k` will become the first element in the slice.
- ///
- /// # Panics
- ///
- /// This function will panic if `k` is greater than the length of the
- /// slice. Note that `k == self.len()` does _not_ panic and is a no-op
- /// rotation.
- ///
- /// # Complexity
- ///
- /// Takes linear (in `self.len()`) time.
- ///
- /// # Examples
- ///
- /// ```
- /// let mut a = ['a', 'b', 'c', 'd', 'e', 'f'];
- /// a.rotate_right(2);
- /// assert_eq!(a, ['e', 'f', 'a', 'b', 'c', 'd']);
- /// ```
- ///
- /// Rotate a subslice:
- ///
- /// ```
- /// let mut a = ['a', 'b', 'c', 'd', 'e', 'f'];
- /// a[1..5].rotate_right(1);
- /// assert_eq!(a, ['a', 'e', 'b', 'c', 'd', 'f']);
- /// ```
- #[stable(feature = "slice_rotate", since = "1.26.0")]
- pub fn rotate_right(&mut self, k: usize) {
- core_slice::SliceExt::rotate_right(self, k);
- }
-
- /// Copies the elements from `src` into `self`.
- ///
- /// The length of `src` must be the same as `self`.
- ///
- /// If `src` implements `Copy`, it can be more performant to use
- /// [`copy_from_slice`].
- ///
- /// # Panics
- ///
- /// This function will panic if the two slices have different lengths.
- ///
- /// # Examples
- ///
- /// Cloning two elements from a slice into another:
- ///
- /// ```
- /// let src = [1, 2, 3, 4];
- /// let mut dst = [0, 0];
- ///
- /// dst.clone_from_slice(&src[2..]);
- ///
- /// assert_eq!(src, [1, 2, 3, 4]);
- /// assert_eq!(dst, [3, 4]);
- /// ```
- ///
- /// Rust enforces that there can only be one mutable reference with no
- /// immutable references to a particular piece of data in a particular
- /// scope. Because of this, attempting to use `clone_from_slice` on a
- /// single slice will result in a compile failure:
- ///
- /// ```compile_fail
- /// let mut slice = [1, 2, 3, 4, 5];
- ///
- /// slice[..2].clone_from_slice(&slice[3..]); // compile fail!
- /// ```
- ///
- /// To work around this, we can use [`split_at_mut`] to create two distinct
- /// sub-slices from a slice:
- ///
- /// ```
- /// let mut slice = [1, 2, 3, 4, 5];
- ///
- /// {
- /// let (left, right) = slice.split_at_mut(2);
- /// left.clone_from_slice(&right[1..]);
- /// }
- ///
- /// assert_eq!(slice, [4, 5, 3, 4, 5]);
- /// ```
- ///
- /// [`copy_from_slice`]: #method.copy_from_slice
- /// [`split_at_mut`]: #method.split_at_mut
- #[stable(feature = "clone_from_slice", since = "1.7.0")]
- pub fn clone_from_slice(&mut self, src: &[T]) where T: Clone {
- core_slice::SliceExt::clone_from_slice(self, src)
- }
-
- /// Copies all elements from `src` into `self`, using a memcpy.
- ///
- /// The length of `src` must be the same as `self`.
- ///
- /// If `src` does not implement `Copy`, use [`clone_from_slice`].
- ///
- /// # Panics
- ///
- /// This function will panic if the two slices have different lengths.
- ///
- /// # Examples
- ///
- /// Copying two elements from a slice into another:
- ///
- /// ```
- /// let src = [1, 2, 3, 4];
- /// let mut dst = [0, 0];
- ///
- /// dst.copy_from_slice(&src[2..]);
- ///
- /// assert_eq!(src, [1, 2, 3, 4]);
- /// assert_eq!(dst, [3, 4]);
- /// ```
- ///
- /// Rust enforces that there can only be one mutable reference with no
- /// immutable references to a particular piece of data in a particular
- /// scope. Because of this, attempting to use `copy_from_slice` on a
- /// single slice will result in a compile failure:
- ///
- /// ```compile_fail
- /// let mut slice = [1, 2, 3, 4, 5];
- ///
- /// slice[..2].copy_from_slice(&slice[3..]); // compile fail!
- /// ```
- ///
- /// To work around this, we can use [`split_at_mut`] to create two distinct
- /// sub-slices from a slice:
- ///
- /// ```
- /// let mut slice = [1, 2, 3, 4, 5];
- ///
- /// {
- /// let (left, right) = slice.split_at_mut(2);
- /// left.copy_from_slice(&right[1..]);
- /// }
- ///
- /// assert_eq!(slice, [4, 5, 3, 4, 5]);
- /// ```
- ///
- /// [`clone_from_slice`]: #method.clone_from_slice
- /// [`split_at_mut`]: #method.split_at_mut
- #[stable(feature = "copy_from_slice", since = "1.9.0")]
- pub fn copy_from_slice(&mut self, src: &[T]) where T: Copy {
- core_slice::SliceExt::copy_from_slice(self, src)
- }
-
- /// Swaps all elements in `self` with those in `other`.
- ///
- /// The length of `other` must be the same as `self`.
- ///
- /// # Panics
- ///
- /// This function will panic if the two slices have different lengths.
- ///
- /// # Example
- ///
- /// Swapping two elements across slices:
- ///
- /// ```
- /// let mut slice1 = [0, 0];
- /// let mut slice2 = [1, 2, 3, 4];
- ///
- /// slice1.swap_with_slice(&mut slice2[2..]);
- ///
- /// assert_eq!(slice1, [3, 4]);
- /// assert_eq!(slice2, [1, 2, 0, 0]);
- /// ```
- ///
- /// Rust enforces that there can only be one mutable reference to a
- /// particular piece of data in a particular scope. Because of this,
- /// attempting to use `swap_with_slice` on a single slice will result in
- /// a compile failure:
- ///
- /// ```compile_fail
- /// let mut slice = [1, 2, 3, 4, 5];
- /// slice[..2].swap_with_slice(&mut slice[3..]); // compile fail!
- /// ```
- ///
- /// To work around this, we can use [`split_at_mut`] to create two distinct
- /// mutable sub-slices from a slice:
- ///
- /// ```
- /// let mut slice = [1, 2, 3, 4, 5];
- ///
- /// {
- /// let (left, right) = slice.split_at_mut(2);
- /// left.swap_with_slice(&mut right[1..]);
- /// }
- ///
- /// assert_eq!(slice, [4, 5, 3, 1, 2]);
- /// ```
- ///
- /// [`split_at_mut`]: #method.split_at_mut
- #[stable(feature = "swap_with_slice", since = "1.27.0")]
- pub fn swap_with_slice(&mut self, other: &mut [T]) {
- core_slice::SliceExt::swap_with_slice(self, other)
- }
-
/// Copies `self` into a new `Vec`.
///
/// # Examples
}
}
-#[lang = "slice_u8"]
+#[cfg_attr(stage0, lang = "slice_u8")]
+#[cfg_attr(not(stage0), lang = "slice_u8_alloc")]
#[cfg(not(test))]
impl [u8] {
- /// Checks if all bytes in this slice are within the ASCII range.
- #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
- #[inline]
- pub fn is_ascii(&self) -> bool {
- self.iter().all(|b| b.is_ascii())
- }
-
/// Returns a vector containing a copy of this slice where each byte
/// is mapped to its ASCII upper case equivalent.
///
me
}
- /// Checks that two slices are an ASCII case-insensitive match.
- ///
- /// Same as `to_ascii_lowercase(a) == to_ascii_lowercase(b)`,
- /// but without allocating and copying temporaries.
- #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
- #[inline]
- pub fn eq_ignore_ascii_case(&self, other: &[u8]) -> bool {
- self.len() == other.len() &&
- self.iter().zip(other).all(|(a, b)| {
- a.eq_ignore_ascii_case(b)
- })
- }
-
- /// Converts this slice to its ASCII upper case equivalent in-place.
- ///
- /// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
- /// but non-ASCII letters are unchanged.
- ///
- /// To return a new uppercased value without modifying the existing one, use
- /// [`to_ascii_uppercase`].
- ///
- /// [`to_ascii_uppercase`]: #method.to_ascii_uppercase
- #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
- #[inline]
- pub fn make_ascii_uppercase(&mut self) {
- for byte in self {
- byte.make_ascii_uppercase();
- }
- }
-
- /// Converts this slice to its ASCII lower case equivalent in-place.
- ///
- /// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
- /// but non-ASCII letters are unchanged.
- ///
- /// To return a new lowercased value without modifying the existing one, use
- /// [`to_ascii_lowercase`].
- ///
- /// [`to_ascii_lowercase`]: #method.to_ascii_lowercase
- #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
- #[inline]
- pub fn make_ascii_lowercase(&mut self) {
- for byte in self {
- byte.make_ascii_lowercase();
- }
- }
+ #[cfg(stage0)]
+ slice_u8_core_methods!();
}
////////////////////////////////////////////////////////////////////////////////
use core::fmt;
use core::str as core_str;
+#[cfg(stage0)] use core::str::StrExt;
use core::str::pattern::Pattern;
use core::str::pattern::{Searcher, ReverseSearcher, DoubleEndedSearcher};
use core::mem;
pub use core::str::SplitWhitespace;
#[stable(feature = "rust1", since = "1.0.0")]
pub use core::str::pattern;
-
+#[stable(feature = "encode_utf16", since = "1.8.0")]
+pub use core::str::EncodeUtf16;
#[unstable(feature = "slice_concat_ext",
reason = "trait should not have to exist",
}
// `len` calculation may overflow but push_str will check boundaries
- let len = self.iter().map(|s| s.borrow().len()).sum();
- let mut result = String::with_capacity(len);
-
- for s in self {
- result.push_str(s.borrow())
- }
-
- result
- }
-
- fn join(&self, sep: &str) -> String {
- if self.is_empty() {
- return String::new();
- }
-
- // concat is faster
- if sep.is_empty() {
- return self.concat();
- }
-
- // this is wrong without the guarantee that `self` is non-empty
- // `len` calculation may overflow but push_str but will check boundaries
- let len = sep.len() * (self.len() - 1) +
- self.iter().map(|s| s.borrow().len()).sum::<usize>();
- let mut result = String::with_capacity(len);
- let mut first = true;
-
- for s in self {
- if first {
- first = false;
- } else {
- result.push_str(sep);
- }
- result.push_str(s.borrow());
- }
- result
- }
-
- fn connect(&self, sep: &str) -> String {
- self.join(sep)
- }
-}
-
-/// An iterator of [`u16`] over the string encoded as UTF-16.
-///
-/// [`u16`]: ../../std/primitive.u16.html
-///
-/// This struct is created by the [`encode_utf16`] method on [`str`].
-/// See its documentation for more.
-///
-/// [`encode_utf16`]: ../../std/primitive.str.html#method.encode_utf16
-/// [`str`]: ../../std/primitive.str.html
-#[derive(Clone)]
-#[stable(feature = "encode_utf16", since = "1.8.0")]
-pub struct EncodeUtf16<'a> {
- chars: Chars<'a>,
- extra: u16,
-}
-
-#[stable(feature = "collection_debug", since = "1.17.0")]
-impl<'a> fmt::Debug for EncodeUtf16<'a> {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- f.pad("EncodeUtf16 { .. }")
- }
-}
-
-#[stable(feature = "encode_utf16", since = "1.8.0")]
-impl<'a> Iterator for EncodeUtf16<'a> {
- type Item = u16;
-
- #[inline]
- fn next(&mut self) -> Option<u16> {
- if self.extra != 0 {
- let tmp = self.extra;
- self.extra = 0;
- return Some(tmp);
- }
-
- let mut buf = [0; 2];
- self.chars.next().map(|ch| {
- let n = ch.encode_utf16(&mut buf).len();
- if n == 2 {
- self.extra = buf[1];
- }
- buf[0]
- })
- }
-
- #[inline]
- fn size_hint(&self) -> (usize, Option<usize>) {
- let (low, high) = self.chars.size_hint();
- // every char gets either one u16 or two u16,
- // so this iterator is between 1 or 2 times as
- // long as the underlying iterator.
- (low, high.and_then(|n| n.checked_mul(2)))
- }
-}
-
-#[stable(feature = "fused", since = "1.26.0")]
-impl<'a> FusedIterator for EncodeUtf16<'a> {}
-
-#[stable(feature = "rust1", since = "1.0.0")]
-impl Borrow<str> for String {
- #[inline]
- fn borrow(&self) -> &str {
- &self[..]
- }
-}
-
-#[stable(feature = "rust1", since = "1.0.0")]
-impl ToOwned for str {
- type Owned = String;
- fn to_owned(&self) -> String {
- unsafe { String::from_utf8_unchecked(self.as_bytes().to_owned()) }
- }
-
- fn clone_into(&self, target: &mut String) {
- let mut b = mem::replace(target, String::new()).into_bytes();
- self.as_bytes().clone_into(&mut b);
- *target = unsafe { String::from_utf8_unchecked(b) }
- }
-}
-
-/// Methods for string slices.
-#[lang = "str"]
-#[cfg(not(test))]
-impl str {
- /// Returns the length of `self`.
- ///
- /// This length is in bytes, not [`char`]s or graphemes. In other words,
- /// it may not be what a human considers the length of the string.
- ///
- /// [`char`]: primitive.char.html
- ///
- /// # Examples
- ///
- /// Basic usage:
- ///
- /// ```
- /// let len = "foo".len();
- /// assert_eq!(3, len);
- ///
- /// let len = "ƒoo".len(); // fancy f!
- /// assert_eq!(4, len);
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn len(&self) -> usize {
- core_str::StrExt::len(self)
- }
-
- /// Returns `true` if `self` has a length of zero bytes.
- ///
- /// # Examples
- ///
- /// Basic usage:
- ///
- /// ```
- /// let s = "";
- /// assert!(s.is_empty());
- ///
- /// let s = "not empty";
- /// assert!(!s.is_empty());
- /// ```
- #[inline]
- #[stable(feature = "rust1", since = "1.0.0")]
- pub fn is_empty(&self) -> bool {
- core_str::StrExt::is_empty(self)
- }
-
- /// Checks that `index`-th byte lies at the start and/or end of a
- /// UTF-8 code point sequence.
- ///
- /// The start and end of the string (when `index == self.len()`) are
- /// considered to be
- /// boundaries.
- ///
- /// Returns `false` if `index` is greater than `self.len()`.
- ///
- /// # Examples
- ///
- /// ```
- /// let s = "Löwe 老虎 Léopard";
- /// assert!(s.is_char_boundary(0));
- /// // start of `老`
- /// assert!(s.is_char_boundary(6));
- /// assert!(s.is_char_boundary(s.len()));
- ///
- /// // second byte of `ö`
- /// assert!(!s.is_char_boundary(2));
- ///
- /// // third byte of `老`
- /// assert!(!s.is_char_boundary(8));
- /// ```
- #[stable(feature = "is_char_boundary", since = "1.9.0")]
- #[inline]
- pub fn is_char_boundary(&self, index: usize) -> bool {
- core_str::StrExt::is_char_boundary(self, index)
- }
-
- /// Converts a string slice to a byte slice. To convert the byte slice back
- /// into a string slice, use the [`str::from_utf8`] function.
- ///
- /// [`str::from_utf8`]: ./str/fn.from_utf8.html
- ///
- /// # Examples
- ///
- /// Basic usage:
- ///
- /// ```
- /// let bytes = "bors".as_bytes();
- /// assert_eq!(b"bors", bytes);
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline(always)]
- pub fn as_bytes(&self) -> &[u8] {
- core_str::StrExt::as_bytes(self)
- }
-
- /// Converts a mutable string slice to a mutable byte slice. To convert the
- /// mutable byte slice back into a mutable string slice, use the
- /// [`str::from_utf8_mut`] function.
- ///
- /// [`str::from_utf8_mut`]: ./str/fn.from_utf8_mut.html
- ///
- /// # Examples
- ///
- /// Basic usage:
- ///
- /// ```
- /// let mut s = String::from("Hello");
- /// let bytes = unsafe { s.as_bytes_mut() };
- ///
- /// assert_eq!(b"Hello", bytes);
- /// ```
- ///
- /// Mutability:
- ///
- /// ```
- /// let mut s = String::from("🗻∈🌏");
- ///
- /// unsafe {
- /// let bytes = s.as_bytes_mut();
- ///
- /// bytes[0] = 0xF0;
- /// bytes[1] = 0x9F;
- /// bytes[2] = 0x8D;
- /// bytes[3] = 0x94;
- /// }
- ///
- /// assert_eq!("🍔∈🌏", s);
- /// ```
- #[stable(feature = "str_mut_extras", since = "1.20.0")]
- #[inline(always)]
- pub unsafe fn as_bytes_mut(&mut self) -> &mut [u8] {
- core_str::StrExt::as_bytes_mut(self)
- }
-
- /// Converts a string slice to a raw pointer.
- ///
- /// As string slices are a slice of bytes, the raw pointer points to a
- /// [`u8`]. This pointer will be pointing to the first byte of the string
- /// slice.
- ///
- /// [`u8`]: primitive.u8.html
- ///
- /// # Examples
- ///
- /// Basic usage:
- ///
- /// ```
- /// let s = "Hello";
- /// let ptr = s.as_ptr();
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn as_ptr(&self) -> *const u8 {
- core_str::StrExt::as_ptr(self)
- }
-
- /// Returns a subslice of `str`.
- ///
- /// This is the non-panicking alternative to indexing the `str`. Returns
- /// [`None`] whenever equivalent indexing operation would panic.
- ///
- /// [`None`]: option/enum.Option.html#variant.None
- ///
- /// # Examples
- ///
- /// ```
- /// let v = String::from("🗻∈🌏");
- ///
- /// assert_eq!(Some("🗻"), v.get(0..4));
- ///
- /// // indices not on UTF-8 sequence boundaries
- /// assert!(v.get(1..).is_none());
- /// assert!(v.get(..8).is_none());
- ///
- /// // out of bounds
- /// assert!(v.get(..42).is_none());
- /// ```
- #[stable(feature = "str_checked_slicing", since = "1.20.0")]
- #[inline]
- pub fn get<I: SliceIndex<str>>(&self, i: I) -> Option<&I::Output> {
- core_str::StrExt::get(self, i)
- }
-
- /// Returns a mutable subslice of `str`.
- ///
- /// This is the non-panicking alternative to indexing the `str`. Returns
- /// [`None`] whenever equivalent indexing operation would panic.
- ///
- /// [`None`]: option/enum.Option.html#variant.None
- ///
- /// # Examples
- ///
- /// ```
- /// let mut v = String::from("hello");
- /// // correct length
- /// assert!(v.get_mut(0..5).is_some());
- /// // out of bounds
- /// assert!(v.get_mut(..42).is_none());
- /// assert_eq!(Some("he"), v.get_mut(0..2).map(|v| &*v));
- ///
- /// assert_eq!("hello", v);
- /// {
- /// let s = v.get_mut(0..2);
- /// let s = s.map(|s| {
- /// s.make_ascii_uppercase();
- /// &*s
- /// });
- /// assert_eq!(Some("HE"), s);
- /// }
- /// assert_eq!("HEllo", v);
- /// ```
- #[stable(feature = "str_checked_slicing", since = "1.20.0")]
- #[inline]
- pub fn get_mut<I: SliceIndex<str>>(&mut self, i: I) -> Option<&mut I::Output> {
- core_str::StrExt::get_mut(self, i)
- }
-
- /// Returns a unchecked subslice of `str`.
- ///
- /// This is the unchecked alternative to indexing the `str`.
- ///
- /// # Safety
- ///
- /// Callers of this function are responsible that these preconditions are
- /// satisfied:
- ///
- /// * The starting index must come before the ending index;
- /// * Indexes must be within bounds of the original slice;
- /// * Indexes must lie on UTF-8 sequence boundaries.
- ///
- /// Failing that, the returned string slice may reference invalid memory or
- /// violate the invariants communicated by the `str` type.
- ///
- /// # Examples
- ///
- /// ```
- /// let v = "🗻∈🌏";
- /// unsafe {
- /// assert_eq!("🗻", v.get_unchecked(0..4));
- /// assert_eq!("∈", v.get_unchecked(4..7));
- /// assert_eq!("🌏", v.get_unchecked(7..11));
- /// }
- /// ```
- #[stable(feature = "str_checked_slicing", since = "1.20.0")]
- #[inline]
- pub unsafe fn get_unchecked<I: SliceIndex<str>>(&self, i: I) -> &I::Output {
- core_str::StrExt::get_unchecked(self, i)
- }
-
- /// Returns a mutable, unchecked subslice of `str`.
- ///
- /// This is the unchecked alternative to indexing the `str`.
- ///
- /// # Safety
- ///
- /// Callers of this function are responsible that these preconditions are
- /// satisfied:
- ///
- /// * The starting index must come before the ending index;
- /// * Indexes must be within bounds of the original slice;
- /// * Indexes must lie on UTF-8 sequence boundaries.
- ///
- /// Failing that, the returned string slice may reference invalid memory or
- /// violate the invariants communicated by the `str` type.
- ///
- /// # Examples
- ///
- /// ```
- /// let mut v = String::from("🗻∈🌏");
- /// unsafe {
- /// assert_eq!("🗻", v.get_unchecked_mut(0..4));
- /// assert_eq!("∈", v.get_unchecked_mut(4..7));
- /// assert_eq!("🌏", v.get_unchecked_mut(7..11));
- /// }
- /// ```
- #[stable(feature = "str_checked_slicing", since = "1.20.0")]
- #[inline]
- pub unsafe fn get_unchecked_mut<I: SliceIndex<str>>(&mut self, i: I) -> &mut I::Output {
- core_str::StrExt::get_unchecked_mut(self, i)
- }
-
- /// Creates a string slice from another string slice, bypassing safety
- /// checks.
- ///
- /// This is generally not recommended, use with caution! For a safe
- /// alternative see [`str`] and [`Index`].
- ///
- /// [`str`]: primitive.str.html
- /// [`Index`]: ops/trait.Index.html
- ///
- /// This new slice goes from `begin` to `end`, including `begin` but
- /// excluding `end`.
- ///
- /// To get a mutable string slice instead, see the
- /// [`slice_mut_unchecked`] method.
- ///
- /// [`slice_mut_unchecked`]: #method.slice_mut_unchecked
- ///
- /// # Safety
- ///
- /// Callers of this function are responsible that three preconditions are
- /// satisfied:
- ///
- /// * `begin` must come before `end`.
- /// * `begin` and `end` must be byte positions within the string slice.
- /// * `begin` and `end` must lie on UTF-8 sequence boundaries.
- ///
- /// # Examples
- ///
- /// Basic usage:
- ///
- /// ```
- /// let s = "Löwe 老虎 Léopard";
- ///
- /// unsafe {
- /// assert_eq!("Löwe 老虎 Léopard", s.slice_unchecked(0, 21));
- /// }
- ///
- /// let s = "Hello, world!";
- ///
- /// unsafe {
- /// assert_eq!("world", s.slice_unchecked(7, 12));
- /// }
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub unsafe fn slice_unchecked(&self, begin: usize, end: usize) -> &str {
- core_str::StrExt::slice_unchecked(self, begin, end)
- }
-
- /// Creates a string slice from another string slice, bypassing safety
- /// checks.
- /// This is generally not recommended, use with caution! For a safe
- /// alternative see [`str`] and [`IndexMut`].
- ///
- /// [`str`]: primitive.str.html
- /// [`IndexMut`]: ops/trait.IndexMut.html
- ///
- /// This new slice goes from `begin` to `end`, including `begin` but
- /// excluding `end`.
- ///
- /// To get an immutable string slice instead, see the
- /// [`slice_unchecked`] method.
- ///
- /// [`slice_unchecked`]: #method.slice_unchecked
- ///
- /// # Safety
- ///
- /// Callers of this function are responsible that three preconditions are
- /// satisfied:
- ///
- /// * `begin` must come before `end`.
- /// * `begin` and `end` must be byte positions within the string slice.
- /// * `begin` and `end` must lie on UTF-8 sequence boundaries.
- #[stable(feature = "str_slice_mut", since = "1.5.0")]
- #[inline]
- pub unsafe fn slice_mut_unchecked(&mut self, begin: usize, end: usize) -> &mut str {
- core_str::StrExt::slice_mut_unchecked(self, begin, end)
- }
-
- /// Divide one string slice into two at an index.
- ///
- /// The argument, `mid`, should be a byte offset from the start of the
- /// string. It must also be on the boundary of a UTF-8 code point.
- ///
- /// The two slices returned go from the start of the string slice to `mid`,
- /// and from `mid` to the end of the string slice.
- ///
- /// To get mutable string slices instead, see the [`split_at_mut`]
- /// method.
- ///
- /// [`split_at_mut`]: #method.split_at_mut
- ///
- /// # Panics
- ///
- /// Panics if `mid` is not on a UTF-8 code point boundary, or if it is
- /// beyond the last code point of the string slice.
- ///
- /// # Examples
- ///
- /// Basic usage:
- ///
- /// ```
- /// let s = "Per Martin-Löf";
- ///
- /// let (first, last) = s.split_at(3);
- ///
- /// assert_eq!("Per", first);
- /// assert_eq!(" Martin-Löf", last);
- /// ```
- #[inline]
- #[stable(feature = "str_split_at", since = "1.4.0")]
- pub fn split_at(&self, mid: usize) -> (&str, &str) {
- core_str::StrExt::split_at(self, mid)
- }
-
- /// Divide one mutable string slice into two at an index.
- ///
- /// The argument, `mid`, should be a byte offset from the start of the
- /// string. It must also be on the boundary of a UTF-8 code point.
- ///
- /// The two slices returned go from the start of the string slice to `mid`,
- /// and from `mid` to the end of the string slice.
- ///
- /// To get immutable string slices instead, see the [`split_at`] method.
- ///
- /// [`split_at`]: #method.split_at
- ///
- /// # Panics
- ///
- /// Panics if `mid` is not on a UTF-8 code point boundary, or if it is
- /// beyond the last code point of the string slice.
- ///
- /// # Examples
- ///
- /// Basic usage:
- ///
- /// ```
- /// let mut s = "Per Martin-Löf".to_string();
- /// {
- /// let (first, last) = s.split_at_mut(3);
- /// first.make_ascii_uppercase();
- /// assert_eq!("PER", first);
- /// assert_eq!(" Martin-Löf", last);
- /// }
- /// assert_eq!("PER Martin-Löf", s);
- /// ```
- #[inline]
- #[stable(feature = "str_split_at", since = "1.4.0")]
- pub fn split_at_mut(&mut self, mid: usize) -> (&mut str, &mut str) {
- core_str::StrExt::split_at_mut(self, mid)
- }
-
- /// Returns an iterator over the [`char`]s of a string slice.
- ///
- /// As a string slice consists of valid UTF-8, we can iterate through a
- /// string slice by [`char`]. This method returns such an iterator.
- ///
- /// It's important to remember that [`char`] represents a Unicode Scalar
- /// Value, and may not match your idea of what a 'character' is. Iteration
- /// over grapheme clusters may be what you actually want.
- ///
- /// [`char`]: primitive.char.html
- ///
- /// # Examples
- ///
- /// Basic usage:
- ///
- /// ```
- /// let word = "goodbye";
- ///
- /// let count = word.chars().count();
- /// assert_eq!(7, count);
- ///
- /// let mut chars = word.chars();
- ///
- /// assert_eq!(Some('g'), chars.next());
- /// assert_eq!(Some('o'), chars.next());
- /// assert_eq!(Some('o'), chars.next());
- /// assert_eq!(Some('d'), chars.next());
- /// assert_eq!(Some('b'), chars.next());
- /// assert_eq!(Some('y'), chars.next());
- /// assert_eq!(Some('e'), chars.next());
- ///
- /// assert_eq!(None, chars.next());
- /// ```
- ///
- /// Remember, [`char`]s may not match your human intuition about characters:
- ///
- /// ```
- /// let y = "y̆";
- ///
- /// let mut chars = y.chars();
- ///
- /// assert_eq!(Some('y'), chars.next()); // not 'y̆'
- /// assert_eq!(Some('\u{0306}'), chars.next());
- ///
- /// assert_eq!(None, chars.next());
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn chars(&self) -> Chars {
- core_str::StrExt::chars(self)
- }
- /// Returns an iterator over the [`char`]s of a string slice, and their
- /// positions.
- ///
- /// As a string slice consists of valid UTF-8, we can iterate through a
- /// string slice by [`char`]. This method returns an iterator of both
- /// these [`char`]s, as well as their byte positions.
- ///
- /// The iterator yields tuples. The position is first, the [`char`] is
- /// second.
- ///
- /// [`char`]: primitive.char.html
- ///
- /// # Examples
- ///
- /// Basic usage:
- ///
- /// ```
- /// let word = "goodbye";
- ///
- /// let count = word.char_indices().count();
- /// assert_eq!(7, count);
- ///
- /// let mut char_indices = word.char_indices();
- ///
- /// assert_eq!(Some((0, 'g')), char_indices.next());
- /// assert_eq!(Some((1, 'o')), char_indices.next());
- /// assert_eq!(Some((2, 'o')), char_indices.next());
- /// assert_eq!(Some((3, 'd')), char_indices.next());
- /// assert_eq!(Some((4, 'b')), char_indices.next());
- /// assert_eq!(Some((5, 'y')), char_indices.next());
- /// assert_eq!(Some((6, 'e')), char_indices.next());
- ///
- /// assert_eq!(None, char_indices.next());
- /// ```
- ///
- /// Remember, [`char`]s may not match your human intuition about characters:
- ///
- /// ```
- /// let yes = "y̆es";
- ///
- /// let mut char_indices = yes.char_indices();
- ///
- /// assert_eq!(Some((0, 'y')), char_indices.next()); // not (0, 'y̆')
- /// assert_eq!(Some((1, '\u{0306}')), char_indices.next());
- ///
- /// // note the 3 here - the last character took up two bytes
- /// assert_eq!(Some((3, 'e')), char_indices.next());
- /// assert_eq!(Some((4, 's')), char_indices.next());
- ///
- /// assert_eq!(None, char_indices.next());
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn char_indices(&self) -> CharIndices {
- core_str::StrExt::char_indices(self)
- }
-
- /// An iterator over the bytes of a string slice.
- ///
- /// As a string slice consists of a sequence of bytes, we can iterate
- /// through a string slice by byte. This method returns such an iterator.
- ///
- /// # Examples
- ///
- /// Basic usage:
- ///
- /// ```
- /// let mut bytes = "bors".bytes();
- ///
- /// assert_eq!(Some(b'b'), bytes.next());
- /// assert_eq!(Some(b'o'), bytes.next());
- /// assert_eq!(Some(b'r'), bytes.next());
- /// assert_eq!(Some(b's'), bytes.next());
- ///
- /// assert_eq!(None, bytes.next());
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn bytes(&self) -> Bytes {
- core_str::StrExt::bytes(self)
- }
-
- /// Split a string slice by whitespace.
- ///
- /// The iterator returned will return string slices that are sub-slices of
- /// the original string slice, separated by any amount of whitespace.
- ///
- /// 'Whitespace' is defined according to the terms of the Unicode Derived
- /// Core Property `White_Space`.
- ///
- /// # Examples
- ///
- /// Basic usage:
- ///
- /// ```
- /// let mut iter = "A few words".split_whitespace();
- ///
- /// assert_eq!(Some("A"), iter.next());
- /// assert_eq!(Some("few"), iter.next());
- /// assert_eq!(Some("words"), iter.next());
- ///
- /// assert_eq!(None, iter.next());
- /// ```
- ///
- /// All kinds of whitespace are considered:
- ///
- /// ```
- /// let mut iter = " Mary had\ta\u{2009}little \n\t lamb".split_whitespace();
- /// assert_eq!(Some("Mary"), iter.next());
- /// assert_eq!(Some("had"), iter.next());
- /// assert_eq!(Some("a"), iter.next());
- /// assert_eq!(Some("little"), iter.next());
- /// assert_eq!(Some("lamb"), iter.next());
- ///
- /// assert_eq!(None, iter.next());
- /// ```
- #[stable(feature = "split_whitespace", since = "1.1.0")]
- #[inline]
- pub fn split_whitespace(&self) -> SplitWhitespace {
- StrExt::split_whitespace(self)
- }
-
- /// An iterator over the lines of a string, as string slices.
- ///
- /// Lines are ended with either a newline (`\n`) or a carriage return with
- /// a line feed (`\r\n`).
- ///
- /// The final line ending is optional.
- ///
- /// # Examples
- ///
- /// Basic usage:
- ///
- /// ```
- /// let text = "foo\r\nbar\n\nbaz\n";
- /// let mut lines = text.lines();
- ///
- /// assert_eq!(Some("foo"), lines.next());
- /// assert_eq!(Some("bar"), lines.next());
- /// assert_eq!(Some(""), lines.next());
- /// assert_eq!(Some("baz"), lines.next());
- ///
- /// assert_eq!(None, lines.next());
- /// ```
- ///
- /// The final line ending isn't required:
- ///
- /// ```
- /// let text = "foo\nbar\n\r\nbaz";
- /// let mut lines = text.lines();
- ///
- /// assert_eq!(Some("foo"), lines.next());
- /// assert_eq!(Some("bar"), lines.next());
- /// assert_eq!(Some(""), lines.next());
- /// assert_eq!(Some("baz"), lines.next());
- ///
- /// assert_eq!(None, lines.next());
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn lines(&self) -> Lines {
- core_str::StrExt::lines(self)
- }
-
- /// An iterator over the lines of a string.
- #[stable(feature = "rust1", since = "1.0.0")]
- #[rustc_deprecated(since = "1.4.0", reason = "use lines() instead now")]
- #[inline]
- #[allow(deprecated)]
- pub fn lines_any(&self) -> LinesAny {
- core_str::StrExt::lines_any(self)
- }
-
- /// Returns an iterator of `u16` over the string encoded as UTF-16.
- ///
- /// # Examples
- ///
- /// Basic usage:
- ///
- /// ```
- /// let text = "Zażółć gęślą jaźń";
- ///
- /// let utf8_len = text.len();
- /// let utf16_len = text.encode_utf16().count();
- ///
- /// assert!(utf16_len <= utf8_len);
- /// ```
- #[stable(feature = "encode_utf16", since = "1.8.0")]
- pub fn encode_utf16(&self) -> EncodeUtf16 {
- EncodeUtf16 { chars: self[..].chars(), extra: 0 }
- }
-
- /// Returns `true` if the given pattern matches a sub-slice of
- /// this string slice.
- ///
- /// Returns `false` if it does not.
- ///
- /// # Examples
- ///
- /// Basic usage:
- ///
- /// ```
- /// let bananas = "bananas";
- ///
- /// assert!(bananas.contains("nana"));
- /// assert!(!bananas.contains("apples"));
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn contains<'a, P: Pattern<'a>>(&'a self, pat: P) -> bool {
- core_str::StrExt::contains(self, pat)
- }
-
- /// Returns `true` if the given pattern matches a prefix of this
- /// string slice.
- ///
- /// Returns `false` if it does not.
- ///
- /// # Examples
- ///
- /// Basic usage:
- ///
- /// ```
- /// let bananas = "bananas";
- ///
- /// assert!(bananas.starts_with("bana"));
- /// assert!(!bananas.starts_with("nana"));
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- pub fn starts_with<'a, P: Pattern<'a>>(&'a self, pat: P) -> bool {
- core_str::StrExt::starts_with(self, pat)
- }
-
- /// Returns `true` if the given pattern matches a suffix of this
- /// string slice.
- ///
- /// Returns `false` if it does not.
- ///
- /// # Examples
- ///
- /// Basic usage:
- ///
- /// ```
- /// let bananas = "bananas";
- ///
- /// assert!(bananas.ends_with("anas"));
- /// assert!(!bananas.ends_with("nana"));
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- pub fn ends_with<'a, P: Pattern<'a>>(&'a self, pat: P) -> bool
- where P::Searcher: ReverseSearcher<'a>
- {
- core_str::StrExt::ends_with(self, pat)
- }
-
- /// Returns the byte index of the first character of this string slice that
- /// matches the pattern.
- ///
- /// Returns [`None`] if the pattern doesn't match.
- ///
- /// The pattern can be a `&str`, [`char`], or a closure that determines if
- /// a character matches.
- ///
- /// [`char`]: primitive.char.html
- /// [`None`]: option/enum.Option.html#variant.None
- ///
- /// # Examples
- ///
- /// Simple patterns:
- ///
- /// ```
- /// let s = "Löwe 老虎 Léopard";
- ///
- /// assert_eq!(s.find('L'), Some(0));
- /// assert_eq!(s.find('é'), Some(14));
- /// assert_eq!(s.find("Léopard"), Some(13));
- /// ```
- ///
- /// More complex patterns using point-free style and closures:
- ///
- /// ```
- /// let s = "Löwe 老虎 Léopard";
- ///
- /// assert_eq!(s.find(char::is_whitespace), Some(5));
- /// assert_eq!(s.find(char::is_lowercase), Some(1));
- /// assert_eq!(s.find(|c: char| c.is_whitespace() || c.is_lowercase()), Some(1));
- /// assert_eq!(s.find(|c: char| (c < 'o') && (c > 'a')), Some(4));
- /// ```
- ///
- /// Not finding the pattern:
- ///
- /// ```
- /// let s = "Löwe 老虎 Léopard";
- /// let x: &[_] = &['1', '2'];
- ///
- /// assert_eq!(s.find(x), None);
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn find<'a, P: Pattern<'a>>(&'a self, pat: P) -> Option<usize> {
- core_str::StrExt::find(self, pat)
- }
-
- /// Returns the byte index of the last character of this string slice that
- /// matches the pattern.
- ///
- /// Returns [`None`] if the pattern doesn't match.
- ///
- /// The pattern can be a `&str`, [`char`], or a closure that determines if
- /// a character matches.
- ///
- /// [`char`]: primitive.char.html
- /// [`None`]: option/enum.Option.html#variant.None
- ///
- /// # Examples
- ///
- /// Simple patterns:
- ///
- /// ```
- /// let s = "Löwe 老虎 Léopard";
- ///
- /// assert_eq!(s.rfind('L'), Some(13));
- /// assert_eq!(s.rfind('é'), Some(14));
- /// ```
- ///
- /// More complex patterns with closures:
- ///
- /// ```
- /// let s = "Löwe 老虎 Léopard";
- ///
- /// assert_eq!(s.rfind(char::is_whitespace), Some(12));
- /// assert_eq!(s.rfind(char::is_lowercase), Some(20));
- /// ```
- ///
- /// Not finding the pattern:
- ///
- /// ```
- /// let s = "Löwe 老虎 Léopard";
- /// let x: &[_] = &['1', '2'];
- ///
- /// assert_eq!(s.rfind(x), None);
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn rfind<'a, P: Pattern<'a>>(&'a self, pat: P) -> Option<usize>
- where P::Searcher: ReverseSearcher<'a>
- {
- core_str::StrExt::rfind(self, pat)
- }
-
- /// An iterator over substrings of this string slice, separated by
- /// characters matched by a pattern.
- ///
- /// The pattern can be a `&str`, [`char`], or a closure that determines the
- /// split.
- ///
- /// # Iterator behavior
- ///
- /// The returned iterator will be a [`DoubleEndedIterator`] if the pattern
- /// allows a reverse search and forward/reverse search yields the same
- /// elements. This is true for, eg, [`char`] but not for `&str`.
- ///
- /// [`DoubleEndedIterator`]: iter/trait.DoubleEndedIterator.html
- ///
- /// If the pattern allows a reverse search but its results might differ
- /// from a forward search, the [`rsplit`] method can be used.
- ///
- /// [`char`]: primitive.char.html
- /// [`rsplit`]: #method.rsplit
- ///
- /// # Examples
- ///
- /// Simple patterns:
- ///
- /// ```
- /// let v: Vec<&str> = "Mary had a little lamb".split(' ').collect();
- /// assert_eq!(v, ["Mary", "had", "a", "little", "lamb"]);
- ///
- /// let v: Vec<&str> = "".split('X').collect();
- /// assert_eq!(v, [""]);
- ///
- /// let v: Vec<&str> = "lionXXtigerXleopard".split('X').collect();
- /// assert_eq!(v, ["lion", "", "tiger", "leopard"]);
- ///
- /// let v: Vec<&str> = "lion::tiger::leopard".split("::").collect();
- /// assert_eq!(v, ["lion", "tiger", "leopard"]);
- ///
- /// let v: Vec<&str> = "abc1def2ghi".split(char::is_numeric).collect();
- /// assert_eq!(v, ["abc", "def", "ghi"]);
- ///
- /// let v: Vec<&str> = "lionXtigerXleopard".split(char::is_uppercase).collect();
- /// assert_eq!(v, ["lion", "tiger", "leopard"]);
- /// ```
- ///
- /// A more complex pattern, using a closure:
- ///
- /// ```
- /// let v: Vec<&str> = "abc1defXghi".split(|c| c == '1' || c == 'X').collect();
- /// assert_eq!(v, ["abc", "def", "ghi"]);
- /// ```
- ///
- /// If a string contains multiple contiguous separators, you will end up
- /// with empty strings in the output:
- ///
- /// ```
- /// let x = "||||a||b|c".to_string();
- /// let d: Vec<_> = x.split('|').collect();
- ///
- /// assert_eq!(d, &["", "", "", "", "a", "", "b", "c"]);
- /// ```
- ///
- /// Contiguous separators are separated by the empty string.
- ///
- /// ```
- /// let x = "(///)".to_string();
- /// let d: Vec<_> = x.split('/').collect();
- ///
- /// assert_eq!(d, &["(", "", "", ")"]);
- /// ```
- ///
- /// Separators at the start or end of a string are neighbored
- /// by empty strings.
- ///
- /// ```
- /// let d: Vec<_> = "010".split("0").collect();
- /// assert_eq!(d, &["", "1", ""]);
- /// ```
- ///
- /// When the empty string is used as a separator, it separates
- /// every character in the string, along with the beginning
- /// and end of the string.
- ///
- /// ```
- /// let f: Vec<_> = "rust".split("").collect();
- /// assert_eq!(f, &["", "r", "u", "s", "t", ""]);
- /// ```
- ///
- /// Contiguous separators can lead to possibly surprising behavior
- /// when whitespace is used as the separator. This code is correct:
- ///
- /// ```
- /// let x = " a b c".to_string();
- /// let d: Vec<_> = x.split(' ').collect();
- ///
- /// assert_eq!(d, &["", "", "", "", "a", "", "b", "c"]);
- /// ```
- ///
- /// It does _not_ give you:
- ///
- /// ```,ignore
- /// assert_eq!(d, &["a", "b", "c"]);
- /// ```
- ///
- /// Use [`split_whitespace`] for this behavior.
- ///
- /// [`split_whitespace`]: #method.split_whitespace
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn split<'a, P: Pattern<'a>>(&'a self, pat: P) -> Split<'a, P> {
- core_str::StrExt::split(self, pat)
- }
-
- /// An iterator over substrings of the given string slice, separated by
- /// characters matched by a pattern and yielded in reverse order.
- ///
- /// The pattern can be a `&str`, [`char`], or a closure that determines the
- /// split.
- ///
- /// [`char`]: primitive.char.html
- ///
- /// # Iterator behavior
- ///
- /// The returned iterator requires that the pattern supports a reverse
- /// search, and it will be a [`DoubleEndedIterator`] if a forward/reverse
- /// search yields the same elements.
- ///
- /// [`DoubleEndedIterator`]: iter/trait.DoubleEndedIterator.html
- ///
- /// For iterating from the front, the [`split`] method can be used.
- ///
- /// [`split`]: #method.split
- ///
- /// # Examples
- ///
- /// Simple patterns:
- ///
- /// ```
- /// let v: Vec<&str> = "Mary had a little lamb".rsplit(' ').collect();
- /// assert_eq!(v, ["lamb", "little", "a", "had", "Mary"]);
- ///
- /// let v: Vec<&str> = "".rsplit('X').collect();
- /// assert_eq!(v, [""]);
- ///
- /// let v: Vec<&str> = "lionXXtigerXleopard".rsplit('X').collect();
- /// assert_eq!(v, ["leopard", "tiger", "", "lion"]);
- ///
- /// let v: Vec<&str> = "lion::tiger::leopard".rsplit("::").collect();
- /// assert_eq!(v, ["leopard", "tiger", "lion"]);
- /// ```
- ///
- /// A more complex pattern, using a closure:
- ///
- /// ```
- /// let v: Vec<&str> = "abc1defXghi".rsplit(|c| c == '1' || c == 'X').collect();
- /// assert_eq!(v, ["ghi", "def", "abc"]);
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn rsplit<'a, P: Pattern<'a>>(&'a self, pat: P) -> RSplit<'a, P>
- where P::Searcher: ReverseSearcher<'a>
- {
- core_str::StrExt::rsplit(self, pat)
- }
-
- /// An iterator over substrings of the given string slice, separated by
- /// characters matched by a pattern.
- ///
- /// The pattern can be a `&str`, [`char`], or a closure that determines the
- /// split.
- ///
- /// Equivalent to [`split`], except that the trailing substring
- /// is skipped if empty.
- ///
- /// [`split`]: #method.split
- ///
- /// This method can be used for string data that is _terminated_,
- /// rather than _separated_ by a pattern.
- ///
- /// # Iterator behavior
- ///
- /// The returned iterator will be a [`DoubleEndedIterator`] if the pattern
- /// allows a reverse search and forward/reverse search yields the same
- /// elements. This is true for, eg, [`char`] but not for `&str`.
- ///
- /// [`DoubleEndedIterator`]: iter/trait.DoubleEndedIterator.html
- /// [`char`]: primitive.char.html
- ///
- /// If the pattern allows a reverse search but its results might differ
- /// from a forward search, the [`rsplit_terminator`] method can be used.
- ///
- /// [`rsplit_terminator`]: #method.rsplit_terminator
- ///
- /// # Examples
- ///
- /// Basic usage:
- ///
- /// ```
- /// let v: Vec<&str> = "A.B.".split_terminator('.').collect();
- /// assert_eq!(v, ["A", "B"]);
- ///
- /// let v: Vec<&str> = "A..B..".split_terminator(".").collect();
- /// assert_eq!(v, ["A", "", "B", ""]);
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn split_terminator<'a, P: Pattern<'a>>(&'a self, pat: P) -> SplitTerminator<'a, P> {
- core_str::StrExt::split_terminator(self, pat)
- }
-
- /// An iterator over substrings of `self`, separated by characters
- /// matched by a pattern and yielded in reverse order.
- ///
- /// The pattern can be a simple `&str`, [`char`], or a closure that
- /// determines the split.
- /// Additional libraries might provide more complex patterns like
- /// regular expressions.
- ///
- /// [`char`]: primitive.char.html
- ///
- /// Equivalent to [`split`], except that the trailing substring is
- /// skipped if empty.
- ///
- /// [`split`]: #method.split
- ///
- /// This method can be used for string data that is _terminated_,
- /// rather than _separated_ by a pattern.
- ///
- /// # Iterator behavior
- ///
- /// The returned iterator requires that the pattern supports a
- /// reverse search, and it will be double ended if a forward/reverse
- /// search yields the same elements.
- ///
- /// For iterating from the front, the [`split_terminator`] method can be
- /// used.
- ///
- /// [`split_terminator`]: #method.split_terminator
- ///
- /// # Examples
- ///
- /// ```
- /// let v: Vec<&str> = "A.B.".rsplit_terminator('.').collect();
- /// assert_eq!(v, ["B", "A"]);
- ///
- /// let v: Vec<&str> = "A..B..".rsplit_terminator(".").collect();
- /// assert_eq!(v, ["", "B", "", "A"]);
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn rsplit_terminator<'a, P: Pattern<'a>>(&'a self, pat: P) -> RSplitTerminator<'a, P>
- where P::Searcher: ReverseSearcher<'a>
- {
- core_str::StrExt::rsplit_terminator(self, pat)
- }
-
- /// An iterator over substrings of the given string slice, separated by a
- /// pattern, restricted to returning at most `n` items.
- ///
- /// If `n` substrings are returned, the last substring (the `n`th substring)
- /// will contain the remainder of the string.
- ///
- /// The pattern can be a `&str`, [`char`], or a closure that determines the
- /// split.
- ///
- /// [`char`]: primitive.char.html
- ///
- /// # Iterator behavior
- ///
- /// The returned iterator will not be double ended, because it is
- /// not efficient to support.
- ///
- /// If the pattern allows a reverse search, the [`rsplitn`] method can be
- /// used.
- ///
- /// [`rsplitn`]: #method.rsplitn
- ///
- /// # Examples
- ///
- /// Simple patterns:
- ///
- /// ```
- /// let v: Vec<&str> = "Mary had a little lambda".splitn(3, ' ').collect();
- /// assert_eq!(v, ["Mary", "had", "a little lambda"]);
- ///
- /// let v: Vec<&str> = "lionXXtigerXleopard".splitn(3, "X").collect();
- /// assert_eq!(v, ["lion", "", "tigerXleopard"]);
- ///
- /// let v: Vec<&str> = "abcXdef".splitn(1, 'X').collect();
- /// assert_eq!(v, ["abcXdef"]);
- ///
- /// let v: Vec<&str> = "".splitn(1, 'X').collect();
- /// assert_eq!(v, [""]);
- /// ```
- ///
- /// A more complex pattern, using a closure:
- ///
- /// ```
- /// let v: Vec<&str> = "abc1defXghi".splitn(2, |c| c == '1' || c == 'X').collect();
- /// assert_eq!(v, ["abc", "defXghi"]);
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn splitn<'a, P: Pattern<'a>>(&'a self, n: usize, pat: P) -> SplitN<'a, P> {
- core_str::StrExt::splitn(self, n, pat)
- }
-
- /// An iterator over substrings of this string slice, separated by a
- /// pattern, starting from the end of the string, restricted to returning
- /// at most `n` items.
- ///
- /// If `n` substrings are returned, the last substring (the `n`th substring)
- /// will contain the remainder of the string.
- ///
- /// The pattern can be a `&str`, [`char`], or a closure that
- /// determines the split.
- ///
- /// [`char`]: primitive.char.html
- ///
- /// # Iterator behavior
- ///
- /// The returned iterator will not be double ended, because it is not
- /// efficient to support.
- ///
- /// For splitting from the front, the [`splitn`] method can be used.
- ///
- /// [`splitn`]: #method.splitn
- ///
- /// # Examples
- ///
- /// Simple patterns:
- ///
- /// ```
- /// let v: Vec<&str> = "Mary had a little lamb".rsplitn(3, ' ').collect();
- /// assert_eq!(v, ["lamb", "little", "Mary had a"]);
- ///
- /// let v: Vec<&str> = "lionXXtigerXleopard".rsplitn(3, 'X').collect();
- /// assert_eq!(v, ["leopard", "tiger", "lionX"]);
- ///
- /// let v: Vec<&str> = "lion::tiger::leopard".rsplitn(2, "::").collect();
- /// assert_eq!(v, ["leopard", "lion::tiger"]);
- /// ```
- ///
- /// A more complex pattern, using a closure:
- ///
- /// ```
- /// let v: Vec<&str> = "abc1defXghi".rsplitn(2, |c| c == '1' || c == 'X').collect();
- /// assert_eq!(v, ["ghi", "abc1def"]);
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn rsplitn<'a, P: Pattern<'a>>(&'a self, n: usize, pat: P) -> RSplitN<'a, P>
- where P::Searcher: ReverseSearcher<'a>
- {
- core_str::StrExt::rsplitn(self, n, pat)
- }
-
- /// An iterator over the disjoint matches of a pattern within the given string
- /// slice.
- ///
- /// The pattern can be a `&str`, [`char`], or a closure that
- /// determines if a character matches.
- ///
- /// [`char`]: primitive.char.html
- ///
- /// # Iterator behavior
- ///
- /// The returned iterator will be a [`DoubleEndedIterator`] if the pattern
- /// allows a reverse search and forward/reverse search yields the same
- /// elements. This is true for, eg, [`char`] but not for `&str`.
- ///
- /// [`DoubleEndedIterator`]: iter/trait.DoubleEndedIterator.html
- /// [`char`]: primitive.char.html
- ///
- /// If the pattern allows a reverse search but its results might differ
- /// from a forward search, the [`rmatches`] method can be used.
- ///
- /// [`rmatches`]: #method.rmatches
- ///
- /// # Examples
- ///
- /// Basic usage:
- ///
- /// ```
- /// let v: Vec<&str> = "abcXXXabcYYYabc".matches("abc").collect();
- /// assert_eq!(v, ["abc", "abc", "abc"]);
- ///
- /// let v: Vec<&str> = "1abc2abc3".matches(char::is_numeric).collect();
- /// assert_eq!(v, ["1", "2", "3"]);
- /// ```
- #[stable(feature = "str_matches", since = "1.2.0")]
- #[inline]
- pub fn matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> Matches<'a, P> {
- core_str::StrExt::matches(self, pat)
- }
+ let len = self.iter().map(|s| s.borrow().len()).sum();
+ let mut result = String::with_capacity(len);
- /// An iterator over the disjoint matches of a pattern within this string slice,
- /// yielded in reverse order.
- ///
- /// The pattern can be a `&str`, [`char`], or a closure that determines if
- /// a character matches.
- ///
- /// [`char`]: primitive.char.html
- ///
- /// # Iterator behavior
- ///
- /// The returned iterator requires that the pattern supports a reverse
- /// search, and it will be a [`DoubleEndedIterator`] if a forward/reverse
- /// search yields the same elements.
- ///
- /// [`DoubleEndedIterator`]: iter/trait.DoubleEndedIterator.html
- ///
- /// For iterating from the front, the [`matches`] method can be used.
- ///
- /// [`matches`]: #method.matches
- ///
- /// # Examples
- ///
- /// Basic usage:
- ///
- /// ```
- /// let v: Vec<&str> = "abcXXXabcYYYabc".rmatches("abc").collect();
- /// assert_eq!(v, ["abc", "abc", "abc"]);
- ///
- /// let v: Vec<&str> = "1abc2abc3".rmatches(char::is_numeric).collect();
- /// assert_eq!(v, ["3", "2", "1"]);
- /// ```
- #[stable(feature = "str_matches", since = "1.2.0")]
- #[inline]
- pub fn rmatches<'a, P: Pattern<'a>>(&'a self, pat: P) -> RMatches<'a, P>
- where P::Searcher: ReverseSearcher<'a>
- {
- core_str::StrExt::rmatches(self, pat)
- }
+ for s in self {
+ result.push_str(s.borrow())
+ }
- /// An iterator over the disjoint matches of a pattern within this string
- /// slice as well as the index that the match starts at.
- ///
- /// For matches of `pat` within `self` that overlap, only the indices
- /// corresponding to the first match are returned.
- ///
- /// The pattern can be a `&str`, [`char`], or a closure that determines
- /// if a character matches.
- ///
- /// [`char`]: primitive.char.html
- ///
- /// # Iterator behavior
- ///
- /// The returned iterator will be a [`DoubleEndedIterator`] if the pattern
- /// allows a reverse search and forward/reverse search yields the same
- /// elements. This is true for, eg, [`char`] but not for `&str`.
- ///
- /// [`DoubleEndedIterator`]: iter/trait.DoubleEndedIterator.html
- ///
- /// If the pattern allows a reverse search but its results might differ
- /// from a forward search, the [`rmatch_indices`] method can be used.
- ///
- /// [`rmatch_indices`]: #method.rmatch_indices
- ///
- /// # Examples
- ///
- /// Basic usage:
- ///
- /// ```
- /// let v: Vec<_> = "abcXXXabcYYYabc".match_indices("abc").collect();
- /// assert_eq!(v, [(0, "abc"), (6, "abc"), (12, "abc")]);
- ///
- /// let v: Vec<_> = "1abcabc2".match_indices("abc").collect();
- /// assert_eq!(v, [(1, "abc"), (4, "abc")]);
- ///
- /// let v: Vec<_> = "ababa".match_indices("aba").collect();
- /// assert_eq!(v, [(0, "aba")]); // only the first `aba`
- /// ```
- #[stable(feature = "str_match_indices", since = "1.5.0")]
- #[inline]
- pub fn match_indices<'a, P: Pattern<'a>>(&'a self, pat: P) -> MatchIndices<'a, P> {
- core_str::StrExt::match_indices(self, pat)
+ result
}
- /// An iterator over the disjoint matches of a pattern within `self`,
- /// yielded in reverse order along with the index of the match.
- ///
- /// For matches of `pat` within `self` that overlap, only the indices
- /// corresponding to the last match are returned.
- ///
- /// The pattern can be a `&str`, [`char`], or a closure that determines if a
- /// character matches.
- ///
- /// [`char`]: primitive.char.html
- ///
- /// # Iterator behavior
- ///
- /// The returned iterator requires that the pattern supports a reverse
- /// search, and it will be a [`DoubleEndedIterator`] if a forward/reverse
- /// search yields the same elements.
- ///
- /// [`DoubleEndedIterator`]: iter/trait.DoubleEndedIterator.html
- ///
- /// For iterating from the front, the [`match_indices`] method can be used.
- ///
- /// [`match_indices`]: #method.match_indices
- ///
- /// # Examples
- ///
- /// Basic usage:
- ///
- /// ```
- /// let v: Vec<_> = "abcXXXabcYYYabc".rmatch_indices("abc").collect();
- /// assert_eq!(v, [(12, "abc"), (6, "abc"), (0, "abc")]);
- ///
- /// let v: Vec<_> = "1abcabc2".rmatch_indices("abc").collect();
- /// assert_eq!(v, [(4, "abc"), (1, "abc")]);
- ///
- /// let v: Vec<_> = "ababa".rmatch_indices("aba").collect();
- /// assert_eq!(v, [(2, "aba")]); // only the last `aba`
- /// ```
- #[stable(feature = "str_match_indices", since = "1.5.0")]
- #[inline]
- pub fn rmatch_indices<'a, P: Pattern<'a>>(&'a self, pat: P) -> RMatchIndices<'a, P>
- where P::Searcher: ReverseSearcher<'a>
- {
- core_str::StrExt::rmatch_indices(self, pat)
- }
+ fn join(&self, sep: &str) -> String {
+ if self.is_empty() {
+ return String::new();
+ }
- /// Returns a string slice with leading and trailing whitespace removed.
- ///
- /// 'Whitespace' is defined according to the terms of the Unicode Derived
- /// Core Property `White_Space`.
- ///
- /// # Examples
- ///
- /// Basic usage:
- ///
- /// ```
- /// let s = " Hello\tworld\t";
- ///
- /// assert_eq!("Hello\tworld", s.trim());
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- pub fn trim(&self) -> &str {
- StrExt::trim(self)
- }
+ // concat is faster
+ if sep.is_empty() {
+ return self.concat();
+ }
- /// Returns a string slice with leading whitespace removed.
- ///
- /// 'Whitespace' is defined according to the terms of the Unicode Derived
- /// Core Property `White_Space`.
- ///
- /// # Text directionality
- ///
- /// A string is a sequence of bytes. 'Left' in this context means the first
- /// position of that byte string; for a language like Arabic or Hebrew
- /// which are 'right to left' rather than 'left to right', this will be
- /// the _right_ side, not the left.
- ///
- /// # Examples
- ///
- /// Basic usage:
- ///
- /// ```
- /// let s = " Hello\tworld\t";
- ///
- /// assert_eq!("Hello\tworld\t", s.trim_left());
- /// ```
- ///
- /// Directionality:
- ///
- /// ```
- /// let s = " English";
- /// assert!(Some('E') == s.trim_left().chars().next());
- ///
- /// let s = " עברית";
- /// assert!(Some('ע') == s.trim_left().chars().next());
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- pub fn trim_left(&self) -> &str {
- StrExt::trim_left(self)
- }
+ // this is wrong without the guarantee that `self` is non-empty
+ // `len` calculation may overflow but push_str but will check boundaries
+ let len = sep.len() * (self.len() - 1) +
+ self.iter().map(|s| s.borrow().len()).sum::<usize>();
+ let mut result = String::with_capacity(len);
+ let mut first = true;
- /// Returns a string slice with trailing whitespace removed.
- ///
- /// 'Whitespace' is defined according to the terms of the Unicode Derived
- /// Core Property `White_Space`.
- ///
- /// # Text directionality
- ///
- /// A string is a sequence of bytes. 'Right' in this context means the last
- /// position of that byte string; for a language like Arabic or Hebrew
- /// which are 'right to left' rather than 'left to right', this will be
- /// the _left_ side, not the right.
- ///
- /// # Examples
- ///
- /// Basic usage:
- ///
- /// ```
- /// let s = " Hello\tworld\t";
- ///
- /// assert_eq!(" Hello\tworld", s.trim_right());
- /// ```
- ///
- /// Directionality:
- ///
- /// ```
- /// let s = "English ";
- /// assert!(Some('h') == s.trim_right().chars().rev().next());
- ///
- /// let s = "עברית ";
- /// assert!(Some('ת') == s.trim_right().chars().rev().next());
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- pub fn trim_right(&self) -> &str {
- StrExt::trim_right(self)
+ for s in self {
+ if first {
+ first = false;
+ } else {
+ result.push_str(sep);
+ }
+ result.push_str(s.borrow());
+ }
+ result
}
- /// Returns a string slice with all prefixes and suffixes that match a
- /// pattern repeatedly removed.
- ///
- /// The pattern can be a [`char`] or a closure that determines if a
- /// character matches.
- ///
- /// [`char`]: primitive.char.html
- ///
- /// # Examples
- ///
- /// Simple patterns:
- ///
- /// ```
- /// assert_eq!("11foo1bar11".trim_matches('1'), "foo1bar");
- /// assert_eq!("123foo1bar123".trim_matches(char::is_numeric), "foo1bar");
- ///
- /// let x: &[_] = &['1', '2'];
- /// assert_eq!("12foo1bar12".trim_matches(x), "foo1bar");
- /// ```
- ///
- /// A more complex pattern, using a closure:
- ///
- /// ```
- /// assert_eq!("1foo1barXX".trim_matches(|c| c == '1' || c == 'X'), "foo1bar");
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- pub fn trim_matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> &'a str
- where P::Searcher: DoubleEndedSearcher<'a>
- {
- core_str::StrExt::trim_matches(self, pat)
+ fn connect(&self, sep: &str) -> String {
+ self.join(sep)
}
+}
- /// Returns a string slice with all prefixes that match a pattern
- /// repeatedly removed.
- ///
- /// The pattern can be a `&str`, [`char`], or a closure that determines if
- /// a character matches.
- ///
- /// [`char`]: primitive.char.html
- ///
- /// # Text directionality
- ///
- /// A string is a sequence of bytes. 'Left' in this context means the first
- /// position of that byte string; for a language like Arabic or Hebrew
- /// which are 'right to left' rather than 'left to right', this will be
- /// the _right_ side, not the left.
- ///
- /// # Examples
- ///
- /// Basic usage:
- ///
- /// ```
- /// assert_eq!("11foo1bar11".trim_left_matches('1'), "foo1bar11");
- /// assert_eq!("123foo1bar123".trim_left_matches(char::is_numeric), "foo1bar123");
- ///
- /// let x: &[_] = &['1', '2'];
- /// assert_eq!("12foo1bar12".trim_left_matches(x), "foo1bar12");
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- pub fn trim_left_matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> &'a str {
- core_str::StrExt::trim_left_matches(self, pat)
+#[stable(feature = "rust1", since = "1.0.0")]
+impl Borrow<str> for String {
+ #[inline]
+ fn borrow(&self) -> &str {
+ &self[..]
}
+}
- /// Returns a string slice with all suffixes that match a pattern
- /// repeatedly removed.
- ///
- /// The pattern can be a `&str`, [`char`], or a closure that
- /// determines if a character matches.
- ///
- /// [`char`]: primitive.char.html
- ///
- /// # Text directionality
- ///
- /// A string is a sequence of bytes. 'Right' in this context means the last
- /// position of that byte string; for a language like Arabic or Hebrew
- /// which are 'right to left' rather than 'left to right', this will be
- /// the _left_ side, not the right.
- ///
- /// # Examples
- ///
- /// Simple patterns:
- ///
- /// ```
- /// assert_eq!("11foo1bar11".trim_right_matches('1'), "11foo1bar");
- /// assert_eq!("123foo1bar123".trim_right_matches(char::is_numeric), "123foo1bar");
- ///
- /// let x: &[_] = &['1', '2'];
- /// assert_eq!("12foo1bar12".trim_right_matches(x), "12foo1bar");
- /// ```
- ///
- /// A more complex pattern, using a closure:
- ///
- /// ```
- /// assert_eq!("1fooX".trim_right_matches(|c| c == '1' || c == 'X'), "1foo");
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- pub fn trim_right_matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> &'a str
- where P::Searcher: ReverseSearcher<'a>
- {
- core_str::StrExt::trim_right_matches(self, pat)
+#[stable(feature = "rust1", since = "1.0.0")]
+impl ToOwned for str {
+ type Owned = String;
+ fn to_owned(&self) -> String {
+ unsafe { String::from_utf8_unchecked(self.as_bytes().to_owned()) }
}
- /// Parses this string slice into another type.
- ///
- /// Because `parse` is so general, it can cause problems with type
- /// inference. As such, `parse` is one of the few times you'll see
- /// the syntax affectionately known as the 'turbofish': `::<>`. This
- /// helps the inference algorithm understand specifically which type
- /// you're trying to parse into.
- ///
- /// `parse` can parse any type that implements the [`FromStr`] trait.
- ///
- /// [`FromStr`]: str/trait.FromStr.html
- ///
- /// # Errors
- ///
- /// Will return [`Err`] if it's not possible to parse this string slice into
- /// the desired type.
- ///
- /// [`Err`]: str/trait.FromStr.html#associatedtype.Err
- ///
- /// # Examples
- ///
- /// Basic usage
- ///
- /// ```
- /// let four: u32 = "4".parse().unwrap();
- ///
- /// assert_eq!(4, four);
- /// ```
- ///
- /// Using the 'turbofish' instead of annotating `four`:
- ///
- /// ```
- /// let four = "4".parse::<u32>();
- ///
- /// assert_eq!(Ok(4), four);
- /// ```
- ///
- /// Failing to parse:
- ///
- /// ```
- /// let nope = "j".parse::<u32>();
- ///
- /// assert!(nope.is_err());
- /// ```
- #[inline]
- #[stable(feature = "rust1", since = "1.0.0")]
- pub fn parse<F: FromStr>(&self) -> Result<F, F::Err> {
- core_str::StrExt::parse(self)
+ fn clone_into(&self, target: &mut String) {
+ let mut b = mem::replace(target, String::new()).into_bytes();
+ self.as_bytes().clone_into(&mut b);
+ *target = unsafe { String::from_utf8_unchecked(b) }
}
+}
+
+/// Methods for string slices.
+#[cfg_attr(stage0, lang = "str")]
+#[cfg_attr(not(stage0), lang = "str_alloc")]
+#[cfg(not(test))]
+impl str {
+ #[cfg(stage0)]
+ str_core_methods!();
/// Converts a `Box<str>` into a `Box<[u8]>` without copying or allocating.
///
unsafe { String::from_utf8_unchecked(buf) }
}
- /// Checks if all characters in this string are within the ASCII range.
- ///
- /// # Examples
- ///
- /// ```
- /// let ascii = "hello!\n";
- /// let non_ascii = "Grüße, Jürgen ❤";
- ///
- /// assert!(ascii.is_ascii());
- /// assert!(!non_ascii.is_ascii());
- /// ```
- #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
- #[inline]
- pub fn is_ascii(&self) -> bool {
- // We can treat each byte as character here: all multibyte characters
- // start with a byte that is not in the ascii range, so we will stop
- // there already.
- self.bytes().all(|b| b.is_ascii())
- }
-
/// Returns a copy of this string where each character is mapped to its
/// ASCII upper case equivalent.
///
// make_ascii_lowercase() preserves the UTF-8 invariant.
unsafe { String::from_utf8_unchecked(bytes) }
}
-
- /// Checks that two strings are an ASCII case-insensitive match.
- ///
- /// Same as `to_ascii_lowercase(a) == to_ascii_lowercase(b)`,
- /// but without allocating and copying temporaries.
- ///
- /// # Examples
- ///
- /// ```
- /// assert!("Ferris".eq_ignore_ascii_case("FERRIS"));
- /// assert!("Ferrös".eq_ignore_ascii_case("FERRöS"));
- /// assert!(!"Ferrös".eq_ignore_ascii_case("FERRÖS"));
- /// ```
- #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
- #[inline]
- pub fn eq_ignore_ascii_case(&self, other: &str) -> bool {
- self.as_bytes().eq_ignore_ascii_case(other.as_bytes())
- }
-
- /// Converts this string to its ASCII upper case equivalent in-place.
- ///
- /// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
- /// but non-ASCII letters are unchanged.
- ///
- /// To return a new uppercased value without modifying the existing one, use
- /// [`to_ascii_uppercase`].
- ///
- /// [`to_ascii_uppercase`]: #method.to_ascii_uppercase
- #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
- pub fn make_ascii_uppercase(&mut self) {
- let me = unsafe { self.as_bytes_mut() };
- me.make_ascii_uppercase()
- }
-
- /// Converts this string to its ASCII lower case equivalent in-place.
- ///
- /// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
- /// but non-ASCII letters are unchanged.
- ///
- /// To return a new lowercased value without modifying the existing one, use
- /// [`to_ascii_lowercase`].
- ///
- /// [`to_ascii_lowercase`]: #method.to_ascii_lowercase
- #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
- pub fn make_ascii_lowercase(&mut self) {
- let me = unsafe { self.as_bytes_mut() };
- me.make_ascii_lowercase()
- }
}
/// Converts a boxed slice of bytes to a boxed string slice without checking
use core::marker::PhantomData;
use core::mem;
#[cfg(not(test))]
+#[cfg(stage0)]
use core::num::Float;
use core::ops::Bound::{Excluded, Included, Unbounded};
use core::ops::{Index, IndexMut, RangeBounds};
/// requests have positive size. A caller to the `Alloc::alloc`
/// method must either ensure that conditions like this are met, or
/// use specific allocators with looser requirements.)
-#[derive(Clone, Debug, PartialEq, Eq)]
+#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Layout {
// size of the requested block of memory, measured in bytes.
size: usize,
}
}
+#[cfg(stage0)]
+macro_rules! public_in_stage0 {
+ ( { $(#[$attr:meta])* } $($Item: tt)*) => {
+ $(#[$attr])* pub $($Item)*
+ }
+}
+
+#[cfg(not(stage0))]
+macro_rules! public_in_stage0 {
+ ( { $(#[$attr:meta])* } $($Item: tt)*) => {
+ $(#[$attr])* pub(crate) $($Item)*
+ }
+}
#![feature(cfg_target_has_atomic)]
#![feature(concat_idents)]
#![feature(const_fn)]
+#![feature(core_float)]
#![feature(custom_attribute)]
#![feature(doc_cfg)]
#![feature(doc_spotlight)]
#![feature(rustc_attrs)]
#![feature(rustc_const_unstable)]
#![feature(simd_ffi)]
+#![feature(core_slice_ext)]
+#![feature(core_str_ext)]
#![feature(specialization)]
#![feature(staged_api)]
#![feature(stmt_expr_attributes)]
#![feature(unboxed_closures)]
#![feature(untagged_unions)]
#![feature(unwind_attributes)]
+#![feature(doc_alias)]
#![cfg_attr(not(stage0), feature(mmx_target_feature))]
#![cfg_attr(not(stage0), feature(tbm_target_feature))]
#[allow(unused_macros)]
macro_rules! vector_impl { ($([$f:ident, $($args:tt)*]),*) => { $($f!($($args)*);)* } }
#[path = "../stdsimd/coresimd/mod.rs"]
-#[allow(missing_docs, missing_debug_implementations, dead_code)]
+#[allow(missing_docs, missing_debug_implementations, dead_code, unused_imports)]
#[unstable(feature = "stdsimd", issue = "48556")]
#[cfg(not(stage0))] // allow changes to how stdsimd works in stage0
mod coresimd;
/// ```
#[macro_export]
#[stable(feature = "rust1", since = "1.0.0")]
+#[doc(alias = "?")]
macro_rules! try {
($expr:expr) => (match $expr {
$crate::result::Result::Ok(val) => val,
#![stable(feature = "rust1", since = "1.0.0")]
-use intrinsics;
use mem;
use num::Float;
+#[cfg(not(stage0))] use num::FpCategory;
use num::FpCategory as Fp;
/// The radix or base of the internal representation of `f32`.
}
}
- /// Computes the absolute value of `self`. Returns `Float::nan()` if the
- /// number is `Float::nan()`.
- #[inline]
- fn abs(self) -> f32 {
- unsafe { intrinsics::fabsf32(self) }
- }
-
- /// Returns a number that represents the sign of `self`.
- ///
- /// - `1.0` if the number is positive, `+0.0` or `Float::infinity()`
- /// - `-1.0` if the number is negative, `-0.0` or `Float::neg_infinity()`
- /// - `Float::nan()` if the number is `Float::nan()`
- #[inline]
- fn signum(self) -> f32 {
- if self.is_nan() {
- NAN
- } else {
- unsafe { intrinsics::copysignf32(1.0, self) }
- }
- }
-
/// Returns `true` if and only if `self` has a positive sign, including `+0.0`, `NaN`s with
/// positive sign bit and positive infinity.
#[inline]
1.0 / self
}
- #[inline]
- fn powi(self, n: i32) -> f32 {
- unsafe { intrinsics::powif32(self, n) }
- }
-
/// Converts to degrees, assuming the number is in radians.
#[inline]
fn to_degrees(self) -> f32 {
unsafe { mem::transmute(v) }
}
}
+
+// FIXME: remove (inline) this macro and the Float trait
+// when updating to a bootstrap compiler that has the new lang items.
+#[cfg_attr(stage0, macro_export)]
+#[unstable(feature = "core_float", issue = "32110")]
+macro_rules! f32_core_methods { () => {
+ /// Returns `true` if this value is `NaN` and false otherwise.
+ ///
+ /// ```
+ /// use std::f32;
+ ///
+ /// let nan = f32::NAN;
+ /// let f = 7.0_f32;
+ ///
+ /// assert!(nan.is_nan());
+ /// assert!(!f.is_nan());
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn is_nan(self) -> bool { Float::is_nan(self) }
+
+ /// Returns `true` if this value is positive infinity or negative infinity and
+ /// false otherwise.
+ ///
+ /// ```
+ /// use std::f32;
+ ///
+ /// let f = 7.0f32;
+ /// let inf = f32::INFINITY;
+ /// let neg_inf = f32::NEG_INFINITY;
+ /// let nan = f32::NAN;
+ ///
+ /// assert!(!f.is_infinite());
+ /// assert!(!nan.is_infinite());
+ ///
+ /// assert!(inf.is_infinite());
+ /// assert!(neg_inf.is_infinite());
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn is_infinite(self) -> bool { Float::is_infinite(self) }
+
+ /// Returns `true` if this number is neither infinite nor `NaN`.
+ ///
+ /// ```
+ /// use std::f32;
+ ///
+ /// let f = 7.0f32;
+ /// let inf = f32::INFINITY;
+ /// let neg_inf = f32::NEG_INFINITY;
+ /// let nan = f32::NAN;
+ ///
+ /// assert!(f.is_finite());
+ ///
+ /// assert!(!nan.is_finite());
+ /// assert!(!inf.is_finite());
+ /// assert!(!neg_inf.is_finite());
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn is_finite(self) -> bool { Float::is_finite(self) }
+
+ /// Returns `true` if the number is neither zero, infinite,
+ /// [subnormal][subnormal], or `NaN`.
+ ///
+ /// ```
+ /// use std::f32;
+ ///
+ /// let min = f32::MIN_POSITIVE; // 1.17549435e-38f32
+ /// let max = f32::MAX;
+ /// let lower_than_min = 1.0e-40_f32;
+ /// let zero = 0.0_f32;
+ ///
+ /// assert!(min.is_normal());
+ /// assert!(max.is_normal());
+ ///
+ /// assert!(!zero.is_normal());
+ /// assert!(!f32::NAN.is_normal());
+ /// assert!(!f32::INFINITY.is_normal());
+ /// // Values between `0` and `min` are Subnormal.
+ /// assert!(!lower_than_min.is_normal());
+ /// ```
+ /// [subnormal]: https://en.wikipedia.org/wiki/Denormal_number
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn is_normal(self) -> bool { Float::is_normal(self) }
+
+ /// Returns the floating point category of the number. If only one property
+ /// is going to be tested, it is generally faster to use the specific
+ /// predicate instead.
+ ///
+ /// ```
+ /// use std::num::FpCategory;
+ /// use std::f32;
+ ///
+ /// let num = 12.4_f32;
+ /// let inf = f32::INFINITY;
+ ///
+ /// assert_eq!(num.classify(), FpCategory::Normal);
+ /// assert_eq!(inf.classify(), FpCategory::Infinite);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn classify(self) -> FpCategory { Float::classify(self) }
+
+ /// Returns `true` if and only if `self` has a positive sign, including `+0.0`, `NaN`s with
+ /// positive sign bit and positive infinity.
+ ///
+ /// ```
+ /// let f = 7.0_f32;
+ /// let g = -7.0_f32;
+ ///
+ /// assert!(f.is_sign_positive());
+ /// assert!(!g.is_sign_positive());
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn is_sign_positive(self) -> bool { Float::is_sign_positive(self) }
+
+ /// Returns `true` if and only if `self` has a negative sign, including `-0.0`, `NaN`s with
+ /// negative sign bit and negative infinity.
+ ///
+ /// ```
+ /// let f = 7.0f32;
+ /// let g = -7.0f32;
+ ///
+ /// assert!(!f.is_sign_negative());
+ /// assert!(g.is_sign_negative());
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn is_sign_negative(self) -> bool { Float::is_sign_negative(self) }
+
+ /// Takes the reciprocal (inverse) of a number, `1/x`.
+ ///
+ /// ```
+ /// use std::f32;
+ ///
+ /// let x = 2.0_f32;
+ /// let abs_difference = (x.recip() - (1.0/x)).abs();
+ ///
+ /// assert!(abs_difference <= f32::EPSILON);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn recip(self) -> f32 { Float::recip(self) }
+
+ /// Converts radians to degrees.
+ ///
+ /// ```
+ /// use std::f32::{self, consts};
+ ///
+ /// let angle = consts::PI;
+ ///
+ /// let abs_difference = (angle.to_degrees() - 180.0).abs();
+ ///
+ /// assert!(abs_difference <= f32::EPSILON);
+ /// ```
+ #[stable(feature = "f32_deg_rad_conversions", since="1.7.0")]
+ #[inline]
+ pub fn to_degrees(self) -> f32 { Float::to_degrees(self) }
+
+ /// Converts degrees to radians.
+ ///
+ /// ```
+ /// use std::f32::{self, consts};
+ ///
+ /// let angle = 180.0f32;
+ ///
+ /// let abs_difference = (angle.to_radians() - consts::PI).abs();
+ ///
+ /// assert!(abs_difference <= f32::EPSILON);
+ /// ```
+ #[stable(feature = "f32_deg_rad_conversions", since="1.7.0")]
+ #[inline]
+ pub fn to_radians(self) -> f32 { Float::to_radians(self) }
+
+ /// Returns the maximum of the two numbers.
+ ///
+ /// ```
+ /// let x = 1.0f32;
+ /// let y = 2.0f32;
+ ///
+ /// assert_eq!(x.max(y), y);
+ /// ```
+ ///
+ /// If one of the arguments is NaN, then the other argument is returned.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn max(self, other: f32) -> f32 {
+ Float::max(self, other)
+ }
+
+ /// Returns the minimum of the two numbers.
+ ///
+ /// ```
+ /// let x = 1.0f32;
+ /// let y = 2.0f32;
+ ///
+ /// assert_eq!(x.min(y), x);
+ /// ```
+ ///
+ /// If one of the arguments is NaN, then the other argument is returned.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn min(self, other: f32) -> f32 {
+ Float::min(self, other)
+ }
+
+ /// Raw transmutation to `u32`.
+ ///
+ /// This is currently identical to `transmute::<f32, u32>(self)` on all platforms.
+ ///
+ /// See `from_bits` for some discussion of the portability of this operation
+ /// (there are almost no issues).
+ ///
+ /// Note that this function is distinct from `as` casting, which attempts to
+ /// preserve the *numeric* value, and not the bitwise value.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// assert_ne!((1f32).to_bits(), 1f32 as u32); // to_bits() is not casting!
+ /// assert_eq!((12.5f32).to_bits(), 0x41480000);
+ ///
+ /// ```
+ #[stable(feature = "float_bits_conv", since = "1.20.0")]
+ #[inline]
+ pub fn to_bits(self) -> u32 {
+ Float::to_bits(self)
+ }
+
+ /// Raw transmutation from `u32`.
+ ///
+ /// This is currently identical to `transmute::<u32, f32>(v)` on all platforms.
+ /// It turns out this is incredibly portable, for two reasons:
+ ///
+ /// * Floats and Ints have the same endianness on all supported platforms.
+ /// * IEEE-754 very precisely specifies the bit layout of floats.
+ ///
+ /// However there is one caveat: prior to the 2008 version of IEEE-754, how
+ /// to interpret the NaN signaling bit wasn't actually specified. Most platforms
+ /// (notably x86 and ARM) picked the interpretation that was ultimately
+ /// standardized in 2008, but some didn't (notably MIPS). As a result, all
+ /// signaling NaNs on MIPS are quiet NaNs on x86, and vice-versa.
+ ///
+ /// Rather than trying to preserve signaling-ness cross-platform, this
+ /// implementation favours preserving the exact bits. This means that
+ /// any payloads encoded in NaNs will be preserved even if the result of
+ /// this method is sent over the network from an x86 machine to a MIPS one.
+ ///
+ /// If the results of this method are only manipulated by the same
+ /// architecture that produced them, then there is no portability concern.
+ ///
+ /// If the input isn't NaN, then there is no portability concern.
+ ///
+ /// If you don't care about signalingness (very likely), then there is no
+ /// portability concern.
+ ///
+ /// Note that this function is distinct from `as` casting, which attempts to
+ /// preserve the *numeric* value, and not the bitwise value.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::f32;
+ /// let v = f32::from_bits(0x41480000);
+ /// let difference = (v - 12.5).abs();
+ /// assert!(difference <= 1e-5);
+ /// ```
+ #[stable(feature = "float_bits_conv", since = "1.20.0")]
+ #[inline]
+ pub fn from_bits(v: u32) -> Self {
+ Float::from_bits(v)
+ }
+}}
+
+#[lang = "f32"]
+#[cfg(not(test))]
+#[cfg(not(stage0))]
+impl f32 {
+ f32_core_methods!();
+}
#![stable(feature = "rust1", since = "1.0.0")]
-use intrinsics;
use mem;
-use num::FpCategory as Fp;
use num::Float;
+#[cfg(not(stage0))] use num::FpCategory;
+use num::FpCategory as Fp;
/// The radix or base of the internal representation of `f64`.
#[stable(feature = "rust1", since = "1.0.0")]
}
}
- /// Computes the absolute value of `self`. Returns `Float::nan()` if the
- /// number is `Float::nan()`.
- #[inline]
- fn abs(self) -> f64 {
- unsafe { intrinsics::fabsf64(self) }
- }
-
- /// Returns a number that represents the sign of `self`.
- ///
- /// - `1.0` if the number is positive, `+0.0` or `Float::infinity()`
- /// - `-1.0` if the number is negative, `-0.0` or `Float::neg_infinity()`
- /// - `Float::nan()` if the number is `Float::nan()`
- #[inline]
- fn signum(self) -> f64 {
- if self.is_nan() {
- NAN
- } else {
- unsafe { intrinsics::copysignf64(1.0, self) }
- }
- }
-
/// Returns `true` if and only if `self` has a positive sign, including `+0.0`, `NaN`s with
/// positive sign bit and positive infinity.
#[inline]
1.0 / self
}
- #[inline]
- fn powi(self, n: i32) -> f64 {
- unsafe { intrinsics::powif64(self, n) }
- }
-
/// Converts to degrees, assuming the number is in radians.
#[inline]
fn to_degrees(self) -> f64 {
unsafe { mem::transmute(v) }
}
}
+
+// FIXME: remove (inline) this macro and the Float trait
+// when updating to a bootstrap compiler that has the new lang items.
+#[cfg_attr(stage0, macro_export)]
+#[unstable(feature = "core_float", issue = "32110")]
+macro_rules! f64_core_methods { () => {
+ /// Returns `true` if this value is `NaN` and false otherwise.
+ ///
+ /// ```
+ /// use std::f64;
+ ///
+ /// let nan = f64::NAN;
+ /// let f = 7.0_f64;
+ ///
+ /// assert!(nan.is_nan());
+ /// assert!(!f.is_nan());
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn is_nan(self) -> bool { Float::is_nan(self) }
+
+ /// Returns `true` if this value is positive infinity or negative infinity and
+ /// false otherwise.
+ ///
+ /// ```
+ /// use std::f64;
+ ///
+ /// let f = 7.0f64;
+ /// let inf = f64::INFINITY;
+ /// let neg_inf = f64::NEG_INFINITY;
+ /// let nan = f64::NAN;
+ ///
+ /// assert!(!f.is_infinite());
+ /// assert!(!nan.is_infinite());
+ ///
+ /// assert!(inf.is_infinite());
+ /// assert!(neg_inf.is_infinite());
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn is_infinite(self) -> bool { Float::is_infinite(self) }
+
+ /// Returns `true` if this number is neither infinite nor `NaN`.
+ ///
+ /// ```
+ /// use std::f64;
+ ///
+ /// let f = 7.0f64;
+ /// let inf: f64 = f64::INFINITY;
+ /// let neg_inf: f64 = f64::NEG_INFINITY;
+ /// let nan: f64 = f64::NAN;
+ ///
+ /// assert!(f.is_finite());
+ ///
+ /// assert!(!nan.is_finite());
+ /// assert!(!inf.is_finite());
+ /// assert!(!neg_inf.is_finite());
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn is_finite(self) -> bool { Float::is_finite(self) }
+
+ /// Returns `true` if the number is neither zero, infinite,
+ /// [subnormal][subnormal], or `NaN`.
+ ///
+ /// ```
+ /// use std::f64;
+ ///
+ /// let min = f64::MIN_POSITIVE; // 2.2250738585072014e-308f64
+ /// let max = f64::MAX;
+ /// let lower_than_min = 1.0e-308_f64;
+ /// let zero = 0.0f64;
+ ///
+ /// assert!(min.is_normal());
+ /// assert!(max.is_normal());
+ ///
+ /// assert!(!zero.is_normal());
+ /// assert!(!f64::NAN.is_normal());
+ /// assert!(!f64::INFINITY.is_normal());
+ /// // Values between `0` and `min` are Subnormal.
+ /// assert!(!lower_than_min.is_normal());
+ /// ```
+ /// [subnormal]: https://en.wikipedia.org/wiki/Denormal_number
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn is_normal(self) -> bool { Float::is_normal(self) }
+
+ /// Returns the floating point category of the number. If only one property
+ /// is going to be tested, it is generally faster to use the specific
+ /// predicate instead.
+ ///
+ /// ```
+ /// use std::num::FpCategory;
+ /// use std::f64;
+ ///
+ /// let num = 12.4_f64;
+ /// let inf = f64::INFINITY;
+ ///
+ /// assert_eq!(num.classify(), FpCategory::Normal);
+ /// assert_eq!(inf.classify(), FpCategory::Infinite);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn classify(self) -> FpCategory { Float::classify(self) }
+
+ /// Returns `true` if and only if `self` has a positive sign, including `+0.0`, `NaN`s with
+ /// positive sign bit and positive infinity.
+ ///
+ /// ```
+ /// let f = 7.0_f64;
+ /// let g = -7.0_f64;
+ ///
+ /// assert!(f.is_sign_positive());
+ /// assert!(!g.is_sign_positive());
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn is_sign_positive(self) -> bool { Float::is_sign_positive(self) }
+
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[rustc_deprecated(since = "1.0.0", reason = "renamed to is_sign_positive")]
+ #[inline]
+ #[doc(hidden)]
+ pub fn is_positive(self) -> bool { Float::is_sign_positive(self) }
+
+ /// Returns `true` if and only if `self` has a negative sign, including `-0.0`, `NaN`s with
+ /// negative sign bit and negative infinity.
+ ///
+ /// ```
+ /// let f = 7.0_f64;
+ /// let g = -7.0_f64;
+ ///
+ /// assert!(!f.is_sign_negative());
+ /// assert!(g.is_sign_negative());
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn is_sign_negative(self) -> bool { Float::is_sign_negative(self) }
+
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[rustc_deprecated(since = "1.0.0", reason = "renamed to is_sign_negative")]
+ #[inline]
+ #[doc(hidden)]
+ pub fn is_negative(self) -> bool { Float::is_sign_negative(self) }
+
+ /// Takes the reciprocal (inverse) of a number, `1/x`.
+ ///
+ /// ```
+ /// let x = 2.0_f64;
+ /// let abs_difference = (x.recip() - (1.0/x)).abs();
+ ///
+ /// assert!(abs_difference < 1e-10);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn recip(self) -> f64 { Float::recip(self) }
+
+ /// Converts radians to degrees.
+ ///
+ /// ```
+ /// use std::f64::consts;
+ ///
+ /// let angle = consts::PI;
+ ///
+ /// let abs_difference = (angle.to_degrees() - 180.0).abs();
+ ///
+ /// assert!(abs_difference < 1e-10);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn to_degrees(self) -> f64 { Float::to_degrees(self) }
+
+ /// Converts degrees to radians.
+ ///
+ /// ```
+ /// use std::f64::consts;
+ ///
+ /// let angle = 180.0_f64;
+ ///
+ /// let abs_difference = (angle.to_radians() - consts::PI).abs();
+ ///
+ /// assert!(abs_difference < 1e-10);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn to_radians(self) -> f64 { Float::to_radians(self) }
+
+ /// Returns the maximum of the two numbers.
+ ///
+ /// ```
+ /// let x = 1.0_f64;
+ /// let y = 2.0_f64;
+ ///
+ /// assert_eq!(x.max(y), y);
+ /// ```
+ ///
+ /// If one of the arguments is NaN, then the other argument is returned.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn max(self, other: f64) -> f64 {
+ Float::max(self, other)
+ }
+
+ /// Returns the minimum of the two numbers.
+ ///
+ /// ```
+ /// let x = 1.0_f64;
+ /// let y = 2.0_f64;
+ ///
+ /// assert_eq!(x.min(y), x);
+ /// ```
+ ///
+ /// If one of the arguments is NaN, then the other argument is returned.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn min(self, other: f64) -> f64 {
+ Float::min(self, other)
+ }
+
+ /// Raw transmutation to `u64`.
+ ///
+ /// This is currently identical to `transmute::<f64, u64>(self)` on all platforms.
+ ///
+ /// See `from_bits` for some discussion of the portability of this operation
+ /// (there are almost no issues).
+ ///
+ /// Note that this function is distinct from `as` casting, which attempts to
+ /// preserve the *numeric* value, and not the bitwise value.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// assert!((1f64).to_bits() != 1f64 as u64); // to_bits() is not casting!
+ /// assert_eq!((12.5f64).to_bits(), 0x4029000000000000);
+ ///
+ /// ```
+ #[stable(feature = "float_bits_conv", since = "1.20.0")]
+ #[inline]
+ pub fn to_bits(self) -> u64 {
+ Float::to_bits(self)
+ }
+
+ /// Raw transmutation from `u64`.
+ ///
+ /// This is currently identical to `transmute::<u64, f64>(v)` on all platforms.
+ /// It turns out this is incredibly portable, for two reasons:
+ ///
+ /// * Floats and Ints have the same endianness on all supported platforms.
+ /// * IEEE-754 very precisely specifies the bit layout of floats.
+ ///
+ /// However there is one caveat: prior to the 2008 version of IEEE-754, how
+ /// to interpret the NaN signaling bit wasn't actually specified. Most platforms
+ /// (notably x86 and ARM) picked the interpretation that was ultimately
+ /// standardized in 2008, but some didn't (notably MIPS). As a result, all
+ /// signaling NaNs on MIPS are quiet NaNs on x86, and vice-versa.
+ ///
+ /// Rather than trying to preserve signaling-ness cross-platform, this
+ /// implementation favours preserving the exact bits. This means that
+ /// any payloads encoded in NaNs will be preserved even if the result of
+ /// this method is sent over the network from an x86 machine to a MIPS one.
+ ///
+ /// If the results of this method are only manipulated by the same
+ /// architecture that produced them, then there is no portability concern.
+ ///
+ /// If the input isn't NaN, then there is no portability concern.
+ ///
+ /// If you don't care about signalingness (very likely), then there is no
+ /// portability concern.
+ ///
+ /// Note that this function is distinct from `as` casting, which attempts to
+ /// preserve the *numeric* value, and not the bitwise value.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::f64;
+ /// let v = f64::from_bits(0x4029000000000000);
+ /// let difference = (v - 12.5).abs();
+ /// assert!(difference <= 1e-5);
+ /// ```
+ #[stable(feature = "float_bits_conv", since = "1.20.0")]
+ #[inline]
+ pub fn from_bits(v: u64) -> Self {
+ Float::from_bits(v)
+ }
+}}
+
+#[lang = "f64"]
+#[cfg(not(test))]
+#[cfg(not(stage0))]
+impl f64 {
+ f64_core_methods!();
+}
Normal,
}
-/// A built-in floating point number.
+// Technically private and only exposed for coretests:
#[doc(hidden)]
-#[unstable(feature = "core_float",
- reason = "stable interface is via `impl f{32,64}` in later crates",
- issue = "32110")]
+#[unstable(feature = "float_internals",
+ reason = "internal routines only exposed for testing",
+ issue = "0")]
pub trait Float: Sized {
/// Type used by `to_bits` and `from_bits`.
- #[stable(feature = "core_float_bits", since = "1.25.0")]
type Bits;
/// Returns `true` if this value is NaN and false otherwise.
- #[stable(feature = "core", since = "1.6.0")]
fn is_nan(self) -> bool;
+
/// Returns `true` if this value is positive infinity or negative infinity and
/// false otherwise.
- #[stable(feature = "core", since = "1.6.0")]
fn is_infinite(self) -> bool;
+
/// Returns `true` if this number is neither infinite nor NaN.
- #[stable(feature = "core", since = "1.6.0")]
fn is_finite(self) -> bool;
+
/// Returns `true` if this number is neither zero, infinite, denormal, or NaN.
- #[stable(feature = "core", since = "1.6.0")]
fn is_normal(self) -> bool;
+
/// Returns the category that this number falls into.
- #[stable(feature = "core", since = "1.6.0")]
fn classify(self) -> FpCategory;
- /// Computes the absolute value of `self`. Returns `Float::nan()` if the
- /// number is `Float::nan()`.
- #[stable(feature = "core", since = "1.6.0")]
- fn abs(self) -> Self;
- /// Returns a number that represents the sign of `self`.
- ///
- /// - `1.0` if the number is positive, `+0.0` or `Float::infinity()`
- /// - `-1.0` if the number is negative, `-0.0` or `Float::neg_infinity()`
- /// - `Float::nan()` if the number is `Float::nan()`
- #[stable(feature = "core", since = "1.6.0")]
- fn signum(self) -> Self;
-
/// Returns `true` if `self` is positive, including `+0.0` and
/// `Float::infinity()`.
- #[stable(feature = "core", since = "1.6.0")]
fn is_sign_positive(self) -> bool;
+
/// Returns `true` if `self` is negative, including `-0.0` and
/// `Float::neg_infinity()`.
- #[stable(feature = "core", since = "1.6.0")]
fn is_sign_negative(self) -> bool;
/// Take the reciprocal (inverse) of a number, `1/x`.
- #[stable(feature = "core", since = "1.6.0")]
fn recip(self) -> Self;
- /// Raise a number to an integer power.
- ///
- /// Using this function is generally faster than using `powf`
- #[stable(feature = "core", since = "1.6.0")]
- fn powi(self, n: i32) -> Self;
-
/// Convert radians to degrees.
- #[stable(feature = "deg_rad_conversions", since="1.7.0")]
fn to_degrees(self) -> Self;
+
/// Convert degrees to radians.
- #[stable(feature = "deg_rad_conversions", since="1.7.0")]
fn to_radians(self) -> Self;
/// Returns the maximum of the two numbers.
- #[stable(feature = "core_float_min_max", since="1.20.0")]
fn max(self, other: Self) -> Self;
+
/// Returns the minimum of the two numbers.
- #[stable(feature = "core_float_min_max", since="1.20.0")]
fn min(self, other: Self) -> Self;
/// Raw transmutation to integer.
- #[stable(feature = "core_float_bits", since="1.25.0")]
fn to_bits(self) -> Self::Bits;
+
/// Raw transmutation from integer.
- #[stable(feature = "core_float_bits", since="1.25.0")]
fn from_bits(v: Self::Bits) -> Self;
}
message="cannot add `{RHS}` to `{Self}`",
label="no implementation for `{Self} + {RHS}`",
)]
+#[doc(alias = "+")]
pub trait Add<RHS=Self> {
/// The resulting type after applying the `+` operator.
#[stable(feature = "rust1", since = "1.0.0")]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_on_unimplemented(message="cannot subtract `{RHS}` from `{Self}`",
label="no implementation for `{Self} - {RHS}`")]
+#[doc(alias = "-")]
pub trait Sub<RHS=Self> {
/// The resulting type after applying the `-` operator.
#[stable(feature = "rust1", since = "1.0.0")]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_on_unimplemented(message="cannot multiply `{RHS}` to `{Self}`",
label="no implementation for `{Self} * {RHS}`")]
+#[doc(alias = "*")]
pub trait Mul<RHS=Self> {
/// The resulting type after applying the `*` operator.
#[stable(feature = "rust1", since = "1.0.0")]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_on_unimplemented(message="cannot divide `{Self}` by `{RHS}`",
label="no implementation for `{Self} / {RHS}`")]
+#[doc(alias = "/")]
pub trait Div<RHS=Self> {
/// The resulting type after applying the `/` operator.
#[stable(feature = "rust1", since = "1.0.0")]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_on_unimplemented(message="cannot mod `{Self}` by `{RHS}`",
label="no implementation for `{Self} % {RHS}`")]
+#[doc(alias = "%")]
pub trait Rem<RHS=Self> {
/// The resulting type after applying the `%` operator.
#[stable(feature = "rust1", since = "1.0.0")]
/// ```
#[lang = "neg"]
#[stable(feature = "rust1", since = "1.0.0")]
+#[doc(alias = "-")]
pub trait Neg {
/// The resulting type after applying the `-` operator.
#[stable(feature = "rust1", since = "1.0.0")]
#[stable(feature = "op_assign_traits", since = "1.8.0")]
#[rustc_on_unimplemented(message="cannot add-assign `{Rhs}` to `{Self}`",
label="no implementation for `{Self} += {Rhs}`")]
+#[doc(alias = "+")]
+#[doc(alias = "+=")]
pub trait AddAssign<Rhs=Self> {
/// Performs the `+=` operation.
#[stable(feature = "op_assign_traits", since = "1.8.0")]
#[stable(feature = "op_assign_traits", since = "1.8.0")]
#[rustc_on_unimplemented(message="cannot subtract-assign `{Rhs}` from `{Self}`",
label="no implementation for `{Self} -= {Rhs}`")]
+#[doc(alias = "-")]
+#[doc(alias = "-=")]
pub trait SubAssign<Rhs=Self> {
/// Performs the `-=` operation.
#[stable(feature = "op_assign_traits", since = "1.8.0")]
#[stable(feature = "op_assign_traits", since = "1.8.0")]
#[rustc_on_unimplemented(message="cannot multiply-assign `{Rhs}` to `{Self}`",
label="no implementation for `{Self} *= {Rhs}`")]
+#[doc(alias = "*")]
+#[doc(alias = "*=")]
pub trait MulAssign<Rhs=Self> {
/// Performs the `*=` operation.
#[stable(feature = "op_assign_traits", since = "1.8.0")]
#[stable(feature = "op_assign_traits", since = "1.8.0")]
#[rustc_on_unimplemented(message="cannot divide-assign `{Self}` by `{Rhs}`",
label="no implementation for `{Self} /= {Rhs}`")]
+#[doc(alias = "/")]
+#[doc(alias = "/=")]
pub trait DivAssign<Rhs=Self> {
/// Performs the `/=` operation.
#[stable(feature = "op_assign_traits", since = "1.8.0")]
#[stable(feature = "op_assign_traits", since = "1.8.0")]
#[rustc_on_unimplemented(message="cannot mod-assign `{Self}` by `{Rhs}``",
label="no implementation for `{Self} %= {Rhs}`")]
+#[doc(alias = "%")]
+#[doc(alias = "%=")]
pub trait RemAssign<Rhs=Self> {
/// Performs the `%=` operation.
#[stable(feature = "op_assign_traits", since = "1.8.0")]
#[lang = "index"]
#[rustc_on_unimplemented = "the type `{Self}` cannot be indexed by `{Idx}`"]
#[stable(feature = "rust1", since = "1.0.0")]
+#[doc(alias = "]")]
+#[doc(alias = "[")]
+#[doc(alias = "[]")]
pub trait Index<Idx: ?Sized> {
/// The returned type after indexing.
#[stable(feature = "rust1", since = "1.0.0")]
#[lang = "index_mut"]
#[rustc_on_unimplemented = "the type `{Self}` cannot be mutably indexed by `{Idx}`"]
#[stable(feature = "rust1", since = "1.0.0")]
+#[doc(alias = "[")]
+#[doc(alias = "]")]
+#[doc(alias = "[]")]
pub trait IndexMut<Idx: ?Sized>: Index<Idx> {
/// Performs the mutable indexing (`container[index]`) operation.
#[stable(feature = "rust1", since = "1.0.0")]
that implement `{Try}`",
label="the `?` operator cannot be applied to type `{Self}`")
)]
+#[doc(alias = "?")]
pub trait Try {
/// The type of this value when viewed as successful.
#[unstable(feature = "try_trait", issue = "42327")]
// Re-exported extension traits for primitive types
#[stable(feature = "core_prelude", since = "1.4.0")]
#[doc(no_inline)]
+#[cfg(stage0)]
pub use slice::SliceExt;
#[stable(feature = "core_prelude", since = "1.4.0")]
#[doc(no_inline)]
+#[cfg(stage0)]
pub use str::StrExt;
// Extension traits
//
+public_in_stage0! {
+{
/// Extension methods for slices.
#[unstable(feature = "core_slice_ext",
reason = "stable interface provided by `impl [T]` in later crates",
issue = "32110")]
#[allow(missing_docs)] // documented elsewhere
-pub trait SliceExt {
+}
+trait SliceExt {
type Item;
#[stable(feature = "core", since = "1.6.0")]
fn sort_unstable_by_key<B, F>(&mut self, f: F)
where F: FnMut(&Self::Item) -> B,
B: Ord;
-}
+}}
// Use macros to be generic over const/mut
macro_rules! slice_offset {
}
}
+// FIXME: remove (inline) this macro and the SliceExt trait
+// when updating to a bootstrap compiler that has the new lang items.
+#[cfg_attr(stage0, macro_export)]
+#[unstable(feature = "core_slice_ext", issue = "32110")]
+macro_rules! slice_core_methods { () => {
+ /// Returns the number of elements in the slice.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let a = [1, 2, 3];
+ /// assert_eq!(a.len(), 3);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn len(&self) -> usize {
+ SliceExt::len(self)
+ }
+
+ /// Returns `true` if the slice has a length of 0.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let a = [1, 2, 3];
+ /// assert!(!a.is_empty());
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn is_empty(&self) -> bool {
+ SliceExt::is_empty(self)
+ }
+
+ /// Returns the first element of the slice, or `None` if it is empty.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let v = [10, 40, 30];
+ /// assert_eq!(Some(&10), v.first());
+ ///
+ /// let w: &[i32] = &[];
+ /// assert_eq!(None, w.first());
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn first(&self) -> Option<&T> {
+ SliceExt::first(self)
+ }
+
+ /// Returns a mutable pointer to the first element of the slice, or `None` if it is empty.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let x = &mut [0, 1, 2];
+ ///
+ /// if let Some(first) = x.first_mut() {
+ /// *first = 5;
+ /// }
+ /// assert_eq!(x, &[5, 1, 2]);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn first_mut(&mut self) -> Option<&mut T> {
+ SliceExt::first_mut(self)
+ }
+
+ /// Returns the first and all the rest of the elements of the slice, or `None` if it is empty.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let x = &[0, 1, 2];
+ ///
+ /// if let Some((first, elements)) = x.split_first() {
+ /// assert_eq!(first, &0);
+ /// assert_eq!(elements, &[1, 2]);
+ /// }
+ /// ```
+ #[stable(feature = "slice_splits", since = "1.5.0")]
+ #[inline]
+ pub fn split_first(&self) -> Option<(&T, &[T])> {
+ SliceExt::split_first(self)
+ }
+
+ /// Returns the first and all the rest of the elements of the slice, or `None` if it is empty.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let x = &mut [0, 1, 2];
+ ///
+ /// if let Some((first, elements)) = x.split_first_mut() {
+ /// *first = 3;
+ /// elements[0] = 4;
+ /// elements[1] = 5;
+ /// }
+ /// assert_eq!(x, &[3, 4, 5]);
+ /// ```
+ #[stable(feature = "slice_splits", since = "1.5.0")]
+ #[inline]
+ pub fn split_first_mut(&mut self) -> Option<(&mut T, &mut [T])> {
+ SliceExt::split_first_mut(self)
+ }
+
+ /// Returns the last and all the rest of the elements of the slice, or `None` if it is empty.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let x = &[0, 1, 2];
+ ///
+ /// if let Some((last, elements)) = x.split_last() {
+ /// assert_eq!(last, &2);
+ /// assert_eq!(elements, &[0, 1]);
+ /// }
+ /// ```
+ #[stable(feature = "slice_splits", since = "1.5.0")]
+ #[inline]
+ pub fn split_last(&self) -> Option<(&T, &[T])> {
+ SliceExt::split_last(self)
+
+ }
+
+ /// Returns the last and all the rest of the elements of the slice, or `None` if it is empty.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let x = &mut [0, 1, 2];
+ ///
+ /// if let Some((last, elements)) = x.split_last_mut() {
+ /// *last = 3;
+ /// elements[0] = 4;
+ /// elements[1] = 5;
+ /// }
+ /// assert_eq!(x, &[4, 5, 3]);
+ /// ```
+ #[stable(feature = "slice_splits", since = "1.5.0")]
+ #[inline]
+ pub fn split_last_mut(&mut self) -> Option<(&mut T, &mut [T])> {
+ SliceExt::split_last_mut(self)
+ }
+
+ /// Returns the last element of the slice, or `None` if it is empty.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let v = [10, 40, 30];
+ /// assert_eq!(Some(&30), v.last());
+ ///
+ /// let w: &[i32] = &[];
+ /// assert_eq!(None, w.last());
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn last(&self) -> Option<&T> {
+ SliceExt::last(self)
+ }
+
+ /// Returns a mutable pointer to the last item in the slice.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let x = &mut [0, 1, 2];
+ ///
+ /// if let Some(last) = x.last_mut() {
+ /// *last = 10;
+ /// }
+ /// assert_eq!(x, &[0, 1, 10]);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn last_mut(&mut self) -> Option<&mut T> {
+ SliceExt::last_mut(self)
+ }
+
+ /// Returns a reference to an element or subslice depending on the type of
+ /// index.
+ ///
+ /// - If given a position, returns a reference to the element at that
+ /// position or `None` if out of bounds.
+ /// - If given a range, returns the subslice corresponding to that range,
+ /// or `None` if out of bounds.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let v = [10, 40, 30];
+ /// assert_eq!(Some(&40), v.get(1));
+ /// assert_eq!(Some(&[10, 40][..]), v.get(0..2));
+ /// assert_eq!(None, v.get(3));
+ /// assert_eq!(None, v.get(0..4));
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn get<I>(&self, index: I) -> Option<&I::Output>
+ where I: SliceIndex<Self>
+ {
+ SliceExt::get(self, index)
+ }
+
+ /// Returns a mutable reference to an element or subslice depending on the
+ /// type of index (see [`get`]) or `None` if the index is out of bounds.
+ ///
+ /// [`get`]: #method.get
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let x = &mut [0, 1, 2];
+ ///
+ /// if let Some(elem) = x.get_mut(1) {
+ /// *elem = 42;
+ /// }
+ /// assert_eq!(x, &[0, 42, 2]);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn get_mut<I>(&mut self, index: I) -> Option<&mut I::Output>
+ where I: SliceIndex<Self>
+ {
+ SliceExt::get_mut(self, index)
+ }
+
+ /// Returns a reference to an element or subslice, without doing bounds
+ /// checking.
+ ///
+ /// This is generally not recommended, use with caution! For a safe
+ /// alternative see [`get`].
+ ///
+ /// [`get`]: #method.get
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let x = &[1, 2, 4];
+ ///
+ /// unsafe {
+ /// assert_eq!(x.get_unchecked(1), &2);
+ /// }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub unsafe fn get_unchecked<I>(&self, index: I) -> &I::Output
+ where I: SliceIndex<Self>
+ {
+ SliceExt::get_unchecked(self, index)
+ }
+
+ /// Returns a mutable reference to an element or subslice, without doing
+ /// bounds checking.
+ ///
+ /// This is generally not recommended, use with caution! For a safe
+ /// alternative see [`get_mut`].
+ ///
+ /// [`get_mut`]: #method.get_mut
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let x = &mut [1, 2, 4];
+ ///
+ /// unsafe {
+ /// let elem = x.get_unchecked_mut(1);
+ /// *elem = 13;
+ /// }
+ /// assert_eq!(x, &[1, 13, 4]);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub unsafe fn get_unchecked_mut<I>(&mut self, index: I) -> &mut I::Output
+ where I: SliceIndex<Self>
+ {
+ SliceExt::get_unchecked_mut(self, index)
+ }
+
+ /// Returns a raw pointer to the slice's buffer.
+ ///
+ /// The caller must ensure that the slice outlives the pointer this
+ /// function returns, or else it will end up pointing to garbage.
+ ///
+ /// Modifying the container referenced by this slice may cause its buffer
+ /// to be reallocated, which would also make any pointers to it invalid.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let x = &[1, 2, 4];
+ /// let x_ptr = x.as_ptr();
+ ///
+ /// unsafe {
+ /// for i in 0..x.len() {
+ /// assert_eq!(x.get_unchecked(i), &*x_ptr.offset(i as isize));
+ /// }
+ /// }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn as_ptr(&self) -> *const T {
+ SliceExt::as_ptr(self)
+ }
+
+ /// Returns an unsafe mutable pointer to the slice's buffer.
+ ///
+ /// The caller must ensure that the slice outlives the pointer this
+ /// function returns, or else it will end up pointing to garbage.
+ ///
+ /// Modifying the container referenced by this slice may cause its buffer
+ /// to be reallocated, which would also make any pointers to it invalid.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let x = &mut [1, 2, 4];
+ /// let x_ptr = x.as_mut_ptr();
+ ///
+ /// unsafe {
+ /// for i in 0..x.len() {
+ /// *x_ptr.offset(i as isize) += 2;
+ /// }
+ /// }
+ /// assert_eq!(x, &[3, 4, 6]);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn as_mut_ptr(&mut self) -> *mut T {
+ SliceExt::as_mut_ptr(self)
+ }
+
+ /// Swaps two elements in the slice.
+ ///
+ /// # Arguments
+ ///
+ /// * a - The index of the first element
+ /// * b - The index of the second element
+ ///
+ /// # Panics
+ ///
+ /// Panics if `a` or `b` are out of bounds.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let mut v = ["a", "b", "c", "d"];
+ /// v.swap(1, 3);
+ /// assert!(v == ["a", "d", "c", "b"]);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn swap(&mut self, a: usize, b: usize) {
+ SliceExt::swap(self, a, b)
+ }
+
+ /// Reverses the order of elements in the slice, in place.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let mut v = [1, 2, 3];
+ /// v.reverse();
+ /// assert!(v == [3, 2, 1]);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn reverse(&mut self) {
+ SliceExt::reverse(self)
+ }
+
+ /// Returns an iterator over the slice.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let x = &[1, 2, 4];
+ /// let mut iterator = x.iter();
+ ///
+ /// assert_eq!(iterator.next(), Some(&1));
+ /// assert_eq!(iterator.next(), Some(&2));
+ /// assert_eq!(iterator.next(), Some(&4));
+ /// assert_eq!(iterator.next(), None);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn iter(&self) -> Iter<T> {
+ SliceExt::iter(self)
+ }
+
+ /// Returns an iterator that allows modifying each value.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let x = &mut [1, 2, 4];
+ /// for elem in x.iter_mut() {
+ /// *elem += 2;
+ /// }
+ /// assert_eq!(x, &[3, 4, 6]);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn iter_mut(&mut self) -> IterMut<T> {
+ SliceExt::iter_mut(self)
+ }
+
+ /// Returns an iterator over all contiguous windows of length
+ /// `size`. The windows overlap. If the slice is shorter than
+ /// `size`, the iterator returns no values.
+ ///
+ /// # Panics
+ ///
+ /// Panics if `size` is 0.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let slice = ['r', 'u', 's', 't'];
+ /// let mut iter = slice.windows(2);
+ /// assert_eq!(iter.next().unwrap(), &['r', 'u']);
+ /// assert_eq!(iter.next().unwrap(), &['u', 's']);
+ /// assert_eq!(iter.next().unwrap(), &['s', 't']);
+ /// assert!(iter.next().is_none());
+ /// ```
+ ///
+ /// If the slice is shorter than `size`:
+ ///
+ /// ```
+ /// let slice = ['f', 'o', 'o'];
+ /// let mut iter = slice.windows(4);
+ /// assert!(iter.next().is_none());
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn windows(&self, size: usize) -> Windows<T> {
+ SliceExt::windows(self, size)
+ }
+
+ /// Returns an iterator over `chunk_size` elements of the slice at a
+ /// time. The chunks are slices and do not overlap. If `chunk_size` does
+ /// not divide the length of the slice, then the last chunk will
+ /// not have length `chunk_size`.
+ ///
+ /// See [`exact_chunks`] for a variant of this iterator that returns chunks
+ /// of always exactly `chunk_size` elements.
+ ///
+ /// # Panics
+ ///
+ /// Panics if `chunk_size` is 0.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let slice = ['l', 'o', 'r', 'e', 'm'];
+ /// let mut iter = slice.chunks(2);
+ /// assert_eq!(iter.next().unwrap(), &['l', 'o']);
+ /// assert_eq!(iter.next().unwrap(), &['r', 'e']);
+ /// assert_eq!(iter.next().unwrap(), &['m']);
+ /// assert!(iter.next().is_none());
+ /// ```
+ ///
+ /// [`exact_chunks`]: #method.exact_chunks
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn chunks(&self, chunk_size: usize) -> Chunks<T> {
+ SliceExt::chunks(self, chunk_size)
+ }
+
+ /// Returns an iterator over `chunk_size` elements of the slice at a
+ /// time. The chunks are slices and do not overlap. If `chunk_size` does
+ /// not divide the length of the slice, then the last up to `chunk_size-1`
+ /// elements will be omitted.
+ ///
+ /// Due to each chunk having exactly `chunk_size` elements, the compiler
+ /// can often optimize the resulting code better than in the case of
+ /// [`chunks`].
+ ///
+ /// # Panics
+ ///
+ /// Panics if `chunk_size` is 0.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// #![feature(exact_chunks)]
+ ///
+ /// let slice = ['l', 'o', 'r', 'e', 'm'];
+ /// let mut iter = slice.exact_chunks(2);
+ /// assert_eq!(iter.next().unwrap(), &['l', 'o']);
+ /// assert_eq!(iter.next().unwrap(), &['r', 'e']);
+ /// assert!(iter.next().is_none());
+ /// ```
+ ///
+ /// [`chunks`]: #method.chunks
+ #[unstable(feature = "exact_chunks", issue = "47115")]
+ #[inline]
+ pub fn exact_chunks(&self, chunk_size: usize) -> ExactChunks<T> {
+ SliceExt::exact_chunks(self, chunk_size)
+ }
+
+ /// Returns an iterator over `chunk_size` elements of the slice at a time.
+ /// The chunks are mutable slices, and do not overlap. If `chunk_size` does
+ /// not divide the length of the slice, then the last chunk will not
+ /// have length `chunk_size`.
+ ///
+ /// See [`exact_chunks_mut`] for a variant of this iterator that returns chunks
+ /// of always exactly `chunk_size` elements.
+ ///
+ /// # Panics
+ ///
+ /// Panics if `chunk_size` is 0.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let v = &mut [0, 0, 0, 0, 0];
+ /// let mut count = 1;
+ ///
+ /// for chunk in v.chunks_mut(2) {
+ /// for elem in chunk.iter_mut() {
+ /// *elem += count;
+ /// }
+ /// count += 1;
+ /// }
+ /// assert_eq!(v, &[1, 1, 2, 2, 3]);
+ /// ```
+ ///
+ /// [`exact_chunks_mut`]: #method.exact_chunks_mut
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn chunks_mut(&mut self, chunk_size: usize) -> ChunksMut<T> {
+ SliceExt::chunks_mut(self, chunk_size)
+ }
+
+ /// Returns an iterator over `chunk_size` elements of the slice at a time.
+ /// The chunks are mutable slices, and do not overlap. If `chunk_size` does
+ /// not divide the length of the slice, then the last up to `chunk_size-1`
+ /// elements will be omitted.
+ ///
+ ///
+ /// Due to each chunk having exactly `chunk_size` elements, the compiler
+ /// can often optimize the resulting code better than in the case of
+ /// [`chunks_mut`].
+ ///
+ /// # Panics
+ ///
+ /// Panics if `chunk_size` is 0.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// #![feature(exact_chunks)]
+ ///
+ /// let v = &mut [0, 0, 0, 0, 0];
+ /// let mut count = 1;
+ ///
+ /// for chunk in v.exact_chunks_mut(2) {
+ /// for elem in chunk.iter_mut() {
+ /// *elem += count;
+ /// }
+ /// count += 1;
+ /// }
+ /// assert_eq!(v, &[1, 1, 2, 2, 0]);
+ /// ```
+ ///
+ /// [`chunks_mut`]: #method.chunks_mut
+ #[unstable(feature = "exact_chunks", issue = "47115")]
+ #[inline]
+ pub fn exact_chunks_mut(&mut self, chunk_size: usize) -> ExactChunksMut<T> {
+ SliceExt::exact_chunks_mut(self, chunk_size)
+ }
+
+ /// Divides one slice into two at an index.
+ ///
+ /// The first will contain all indices from `[0, mid)` (excluding
+ /// the index `mid` itself) and the second will contain all
+ /// indices from `[mid, len)` (excluding the index `len` itself).
+ ///
+ /// # Panics
+ ///
+ /// Panics if `mid > len`.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let v = [1, 2, 3, 4, 5, 6];
+ ///
+ /// {
+ /// let (left, right) = v.split_at(0);
+ /// assert!(left == []);
+ /// assert!(right == [1, 2, 3, 4, 5, 6]);
+ /// }
+ ///
+ /// {
+ /// let (left, right) = v.split_at(2);
+ /// assert!(left == [1, 2]);
+ /// assert!(right == [3, 4, 5, 6]);
+ /// }
+ ///
+ /// {
+ /// let (left, right) = v.split_at(6);
+ /// assert!(left == [1, 2, 3, 4, 5, 6]);
+ /// assert!(right == []);
+ /// }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn split_at(&self, mid: usize) -> (&[T], &[T]) {
+ SliceExt::split_at(self, mid)
+ }
+
+ /// Divides one mutable slice into two at an index.
+ ///
+ /// The first will contain all indices from `[0, mid)` (excluding
+ /// the index `mid` itself) and the second will contain all
+ /// indices from `[mid, len)` (excluding the index `len` itself).
+ ///
+ /// # Panics
+ ///
+ /// Panics if `mid > len`.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let mut v = [1, 0, 3, 0, 5, 6];
+ /// // scoped to restrict the lifetime of the borrows
+ /// {
+ /// let (left, right) = v.split_at_mut(2);
+ /// assert!(left == [1, 0]);
+ /// assert!(right == [3, 0, 5, 6]);
+ /// left[1] = 2;
+ /// right[1] = 4;
+ /// }
+ /// assert!(v == [1, 2, 3, 4, 5, 6]);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn split_at_mut(&mut self, mid: usize) -> (&mut [T], &mut [T]) {
+ SliceExt::split_at_mut(self, mid)
+ }
+
+ /// Returns an iterator over subslices separated by elements that match
+ /// `pred`. The matched element is not contained in the subslices.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let slice = [10, 40, 33, 20];
+ /// let mut iter = slice.split(|num| num % 3 == 0);
+ ///
+ /// assert_eq!(iter.next().unwrap(), &[10, 40]);
+ /// assert_eq!(iter.next().unwrap(), &[20]);
+ /// assert!(iter.next().is_none());
+ /// ```
+ ///
+ /// If the first element is matched, an empty slice will be the first item
+ /// returned by the iterator. Similarly, if the last element in the slice
+ /// is matched, an empty slice will be the last item returned by the
+ /// iterator:
+ ///
+ /// ```
+ /// let slice = [10, 40, 33];
+ /// let mut iter = slice.split(|num| num % 3 == 0);
+ ///
+ /// assert_eq!(iter.next().unwrap(), &[10, 40]);
+ /// assert_eq!(iter.next().unwrap(), &[]);
+ /// assert!(iter.next().is_none());
+ /// ```
+ ///
+ /// If two matched elements are directly adjacent, an empty slice will be
+ /// present between them:
+ ///
+ /// ```
+ /// let slice = [10, 6, 33, 20];
+ /// let mut iter = slice.split(|num| num % 3 == 0);
+ ///
+ /// assert_eq!(iter.next().unwrap(), &[10]);
+ /// assert_eq!(iter.next().unwrap(), &[]);
+ /// assert_eq!(iter.next().unwrap(), &[20]);
+ /// assert!(iter.next().is_none());
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn split<F>(&self, pred: F) -> Split<T, F>
+ where F: FnMut(&T) -> bool
+ {
+ SliceExt::split(self, pred)
+ }
+
+ /// Returns an iterator over mutable subslices separated by elements that
+ /// match `pred`. The matched element is not contained in the subslices.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let mut v = [10, 40, 30, 20, 60, 50];
+ ///
+ /// for group in v.split_mut(|num| *num % 3 == 0) {
+ /// group[0] = 1;
+ /// }
+ /// assert_eq!(v, [1, 40, 30, 1, 60, 1]);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn split_mut<F>(&mut self, pred: F) -> SplitMut<T, F>
+ where F: FnMut(&T) -> bool
+ {
+ SliceExt::split_mut(self, pred)
+ }
+
+ /// Returns an iterator over subslices separated by elements that match
+ /// `pred`, starting at the end of the slice and working backwards.
+ /// The matched element is not contained in the subslices.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let slice = [11, 22, 33, 0, 44, 55];
+ /// let mut iter = slice.rsplit(|num| *num == 0);
+ ///
+ /// assert_eq!(iter.next().unwrap(), &[44, 55]);
+ /// assert_eq!(iter.next().unwrap(), &[11, 22, 33]);
+ /// assert_eq!(iter.next(), None);
+ /// ```
+ ///
+ /// As with `split()`, if the first or last element is matched, an empty
+ /// slice will be the first (or last) item returned by the iterator.
+ ///
+ /// ```
+ /// let v = &[0, 1, 1, 2, 3, 5, 8];
+ /// let mut it = v.rsplit(|n| *n % 2 == 0);
+ /// assert_eq!(it.next().unwrap(), &[]);
+ /// assert_eq!(it.next().unwrap(), &[3, 5]);
+ /// assert_eq!(it.next().unwrap(), &[1, 1]);
+ /// assert_eq!(it.next().unwrap(), &[]);
+ /// assert_eq!(it.next(), None);
+ /// ```
+ #[stable(feature = "slice_rsplit", since = "1.27.0")]
+ #[inline]
+ pub fn rsplit<F>(&self, pred: F) -> RSplit<T, F>
+ where F: FnMut(&T) -> bool
+ {
+ SliceExt::rsplit(self, pred)
+ }
+
+ /// Returns an iterator over mutable subslices separated by elements that
+ /// match `pred`, starting at the end of the slice and working
+ /// backwards. The matched element is not contained in the subslices.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let mut v = [100, 400, 300, 200, 600, 500];
+ ///
+ /// let mut count = 0;
+ /// for group in v.rsplit_mut(|num| *num % 3 == 0) {
+ /// count += 1;
+ /// group[0] = count;
+ /// }
+ /// assert_eq!(v, [3, 400, 300, 2, 600, 1]);
+ /// ```
+ ///
+ #[stable(feature = "slice_rsplit", since = "1.27.0")]
+ #[inline]
+ pub fn rsplit_mut<F>(&mut self, pred: F) -> RSplitMut<T, F>
+ where F: FnMut(&T) -> bool
+ {
+ SliceExt::rsplit_mut(self, pred)
+ }
+
+ /// Returns an iterator over subslices separated by elements that match
+ /// `pred`, limited to returning at most `n` items. The matched element is
+ /// not contained in the subslices.
+ ///
+ /// The last element returned, if any, will contain the remainder of the
+ /// slice.
+ ///
+ /// # Examples
+ ///
+ /// Print the slice split once by numbers divisible by 3 (i.e. `[10, 40]`,
+ /// `[20, 60, 50]`):
+ ///
+ /// ```
+ /// let v = [10, 40, 30, 20, 60, 50];
+ ///
+ /// for group in v.splitn(2, |num| *num % 3 == 0) {
+ /// println!("{:?}", group);
+ /// }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn splitn<F>(&self, n: usize, pred: F) -> SplitN<T, F>
+ where F: FnMut(&T) -> bool
+ {
+ SliceExt::splitn(self, n, pred)
+ }
+
+ /// Returns an iterator over subslices separated by elements that match
+ /// `pred`, limited to returning at most `n` items. The matched element is
+ /// not contained in the subslices.
+ ///
+ /// The last element returned, if any, will contain the remainder of the
+ /// slice.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let mut v = [10, 40, 30, 20, 60, 50];
+ ///
+ /// for group in v.splitn_mut(2, |num| *num % 3 == 0) {
+ /// group[0] = 1;
+ /// }
+ /// assert_eq!(v, [1, 40, 30, 1, 60, 50]);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn splitn_mut<F>(&mut self, n: usize, pred: F) -> SplitNMut<T, F>
+ where F: FnMut(&T) -> bool
+ {
+ SliceExt::splitn_mut(self, n, pred)
+ }
+
+ /// Returns an iterator over subslices separated by elements that match
+ /// `pred` limited to returning at most `n` items. This starts at the end of
+ /// the slice and works backwards. The matched element is not contained in
+ /// the subslices.
+ ///
+ /// The last element returned, if any, will contain the remainder of the
+ /// slice.
+ ///
+ /// # Examples
+ ///
+ /// Print the slice split once, starting from the end, by numbers divisible
+ /// by 3 (i.e. `[50]`, `[10, 40, 30, 20]`):
+ ///
+ /// ```
+ /// let v = [10, 40, 30, 20, 60, 50];
+ ///
+ /// for group in v.rsplitn(2, |num| *num % 3 == 0) {
+ /// println!("{:?}", group);
+ /// }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn rsplitn<F>(&self, n: usize, pred: F) -> RSplitN<T, F>
+ where F: FnMut(&T) -> bool
+ {
+ SliceExt::rsplitn(self, n, pred)
+ }
+
+ /// Returns an iterator over subslices separated by elements that match
+ /// `pred` limited to returning at most `n` items. This starts at the end of
+ /// the slice and works backwards. The matched element is not contained in
+ /// the subslices.
+ ///
+ /// The last element returned, if any, will contain the remainder of the
+ /// slice.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let mut s = [10, 40, 30, 20, 60, 50];
+ ///
+ /// for group in s.rsplitn_mut(2, |num| *num % 3 == 0) {
+ /// group[0] = 1;
+ /// }
+ /// assert_eq!(s, [1, 40, 30, 20, 60, 1]);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn rsplitn_mut<F>(&mut self, n: usize, pred: F) -> RSplitNMut<T, F>
+ where F: FnMut(&T) -> bool
+ {
+ SliceExt::rsplitn_mut(self, n, pred)
+ }
+
+ /// Returns `true` if the slice contains an element with the given value.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let v = [10, 40, 30];
+ /// assert!(v.contains(&30));
+ /// assert!(!v.contains(&50));
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn contains(&self, x: &T) -> bool
+ where T: PartialEq
+ {
+ SliceExt::contains(self, x)
+ }
+
+ /// Returns `true` if `needle` is a prefix of the slice.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let v = [10, 40, 30];
+ /// assert!(v.starts_with(&[10]));
+ /// assert!(v.starts_with(&[10, 40]));
+ /// assert!(!v.starts_with(&[50]));
+ /// assert!(!v.starts_with(&[10, 50]));
+ /// ```
+ ///
+ /// Always returns `true` if `needle` is an empty slice:
+ ///
+ /// ```
+ /// let v = &[10, 40, 30];
+ /// assert!(v.starts_with(&[]));
+ /// let v: &[u8] = &[];
+ /// assert!(v.starts_with(&[]));
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn starts_with(&self, needle: &[T]) -> bool
+ where T: PartialEq
+ {
+ SliceExt::starts_with(self, needle)
+ }
+
+ /// Returns `true` if `needle` is a suffix of the slice.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let v = [10, 40, 30];
+ /// assert!(v.ends_with(&[30]));
+ /// assert!(v.ends_with(&[40, 30]));
+ /// assert!(!v.ends_with(&[50]));
+ /// assert!(!v.ends_with(&[50, 30]));
+ /// ```
+ ///
+ /// Always returns `true` if `needle` is an empty slice:
+ ///
+ /// ```
+ /// let v = &[10, 40, 30];
+ /// assert!(v.ends_with(&[]));
+ /// let v: &[u8] = &[];
+ /// assert!(v.ends_with(&[]));
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn ends_with(&self, needle: &[T]) -> bool
+ where T: PartialEq
+ {
+ SliceExt::ends_with(self, needle)
+ }
+
+ /// Binary searches this sorted slice for a given element.
+ ///
+ /// If the value is found then `Ok` is returned, containing the
+ /// index of the matching element; if the value is not found then
+ /// `Err` is returned, containing the index where a matching
+ /// element could be inserted while maintaining sorted order.
+ ///
+ /// # Examples
+ ///
+ /// Looks up a series of four elements. The first is found, with a
+ /// uniquely determined position; the second and third are not
+ /// found; the fourth could match any position in `[1, 4]`.
+ ///
+ /// ```
+ /// let s = [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55];
+ ///
+ /// assert_eq!(s.binary_search(&13), Ok(9));
+ /// assert_eq!(s.binary_search(&4), Err(7));
+ /// assert_eq!(s.binary_search(&100), Err(13));
+ /// let r = s.binary_search(&1);
+ /// assert!(match r { Ok(1...4) => true, _ => false, });
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn binary_search(&self, x: &T) -> Result<usize, usize>
+ where T: Ord
+ {
+ SliceExt::binary_search(self, x)
+ }
+
+ /// Binary searches this sorted slice with a comparator function.
+ ///
+ /// The comparator function should implement an order consistent
+ /// with the sort order of the underlying slice, returning an
+ /// order code that indicates whether its argument is `Less`,
+ /// `Equal` or `Greater` the desired target.
+ ///
+ /// If a matching value is found then returns `Ok`, containing
+ /// the index for the matched element; if no match is found then
+ /// `Err` is returned, containing the index where a matching
+ /// element could be inserted while maintaining sorted order.
+ ///
+ /// # Examples
+ ///
+ /// Looks up a series of four elements. The first is found, with a
+ /// uniquely determined position; the second and third are not
+ /// found; the fourth could match any position in `[1, 4]`.
+ ///
+ /// ```
+ /// let s = [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55];
+ ///
+ /// let seek = 13;
+ /// assert_eq!(s.binary_search_by(|probe| probe.cmp(&seek)), Ok(9));
+ /// let seek = 4;
+ /// assert_eq!(s.binary_search_by(|probe| probe.cmp(&seek)), Err(7));
+ /// let seek = 100;
+ /// assert_eq!(s.binary_search_by(|probe| probe.cmp(&seek)), Err(13));
+ /// let seek = 1;
+ /// let r = s.binary_search_by(|probe| probe.cmp(&seek));
+ /// assert!(match r { Ok(1...4) => true, _ => false, });
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn binary_search_by<'a, F>(&'a self, f: F) -> Result<usize, usize>
+ where F: FnMut(&'a T) -> Ordering
+ {
+ SliceExt::binary_search_by(self, f)
+ }
+
+ /// Binary searches this sorted slice with a key extraction function.
+ ///
+ /// Assumes that the slice is sorted by the key, for instance with
+ /// [`sort_by_key`] using the same key extraction function.
+ ///
+ /// If a matching value is found then returns `Ok`, containing the
+ /// index for the matched element; if no match is found then `Err`
+ /// is returned, containing the index where a matching element could
+ /// be inserted while maintaining sorted order.
+ ///
+ /// [`sort_by_key`]: #method.sort_by_key
+ ///
+ /// # Examples
+ ///
+ /// Looks up a series of four elements in a slice of pairs sorted by
+ /// their second elements. The first is found, with a uniquely
+ /// determined position; the second and third are not found; the
+ /// fourth could match any position in `[1, 4]`.
+ ///
+ /// ```
+ /// let s = [(0, 0), (2, 1), (4, 1), (5, 1), (3, 1),
+ /// (1, 2), (2, 3), (4, 5), (5, 8), (3, 13),
+ /// (1, 21), (2, 34), (4, 55)];
+ ///
+ /// assert_eq!(s.binary_search_by_key(&13, |&(a,b)| b), Ok(9));
+ /// assert_eq!(s.binary_search_by_key(&4, |&(a,b)| b), Err(7));
+ /// assert_eq!(s.binary_search_by_key(&100, |&(a,b)| b), Err(13));
+ /// let r = s.binary_search_by_key(&1, |&(a,b)| b);
+ /// assert!(match r { Ok(1...4) => true, _ => false, });
+ /// ```
+ #[stable(feature = "slice_binary_search_by_key", since = "1.10.0")]
+ #[inline]
+ pub fn binary_search_by_key<'a, B, F>(&'a self, b: &B, f: F) -> Result<usize, usize>
+ where F: FnMut(&'a T) -> B,
+ B: Ord
+ {
+ SliceExt::binary_search_by_key(self, b, f)
+ }
+
+ /// Sorts the slice, but may not preserve the order of equal elements.
+ ///
+ /// This sort is unstable (i.e. may reorder equal elements), in-place (i.e. does not allocate),
+ /// and `O(n log n)` worst-case.
+ ///
+ /// # Current implementation
+ ///
+ /// The current algorithm is based on [pattern-defeating quicksort][pdqsort] by Orson Peters,
+ /// which combines the fast average case of randomized quicksort with the fast worst case of
+ /// heapsort, while achieving linear time on slices with certain patterns. It uses some
+ /// randomization to avoid degenerate cases, but with a fixed seed to always provide
+ /// deterministic behavior.
+ ///
+ /// It is typically faster than stable sorting, except in a few special cases, e.g. when the
+ /// slice consists of several concatenated sorted sequences.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let mut v = [-5, 4, 1, -3, 2];
+ ///
+ /// v.sort_unstable();
+ /// assert!(v == [-5, -3, 1, 2, 4]);
+ /// ```
+ ///
+ /// [pdqsort]: https://github.com/orlp/pdqsort
+ #[stable(feature = "sort_unstable", since = "1.20.0")]
+ #[inline]
+ pub fn sort_unstable(&mut self)
+ where T: Ord
+ {
+ SliceExt::sort_unstable(self);
+ }
+
+ /// Sorts the slice with a comparator function, but may not preserve the order of equal
+ /// elements.
+ ///
+ /// This sort is unstable (i.e. may reorder equal elements), in-place (i.e. does not allocate),
+ /// and `O(n log n)` worst-case.
+ ///
+ /// # Current implementation
+ ///
+ /// The current algorithm is based on [pattern-defeating quicksort][pdqsort] by Orson Peters,
+ /// which combines the fast average case of randomized quicksort with the fast worst case of
+ /// heapsort, while achieving linear time on slices with certain patterns. It uses some
+ /// randomization to avoid degenerate cases, but with a fixed seed to always provide
+ /// deterministic behavior.
+ ///
+ /// It is typically faster than stable sorting, except in a few special cases, e.g. when the
+ /// slice consists of several concatenated sorted sequences.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let mut v = [5, 4, 1, 3, 2];
+ /// v.sort_unstable_by(|a, b| a.cmp(b));
+ /// assert!(v == [1, 2, 3, 4, 5]);
+ ///
+ /// // reverse sorting
+ /// v.sort_unstable_by(|a, b| b.cmp(a));
+ /// assert!(v == [5, 4, 3, 2, 1]);
+ /// ```
+ ///
+ /// [pdqsort]: https://github.com/orlp/pdqsort
+ #[stable(feature = "sort_unstable", since = "1.20.0")]
+ #[inline]
+ pub fn sort_unstable_by<F>(&mut self, compare: F)
+ where F: FnMut(&T, &T) -> Ordering
+ {
+ SliceExt::sort_unstable_by(self, compare);
+ }
+
+ /// Sorts the slice with a key extraction function, but may not preserve the order of equal
+ /// elements.
+ ///
+ /// This sort is unstable (i.e. may reorder equal elements), in-place (i.e. does not allocate),
+ /// and `O(m n log(m n))` worst-case, where the key function is `O(m)`.
+ ///
+ /// # Current implementation
+ ///
+ /// The current algorithm is based on [pattern-defeating quicksort][pdqsort] by Orson Peters,
+ /// which combines the fast average case of randomized quicksort with the fast worst case of
+ /// heapsort, while achieving linear time on slices with certain patterns. It uses some
+ /// randomization to avoid degenerate cases, but with a fixed seed to always provide
+ /// deterministic behavior.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let mut v = [-5i32, 4, 1, -3, 2];
+ ///
+ /// v.sort_unstable_by_key(|k| k.abs());
+ /// assert!(v == [1, 2, -3, 4, -5]);
+ /// ```
+ ///
+ /// [pdqsort]: https://github.com/orlp/pdqsort
+ #[stable(feature = "sort_unstable", since = "1.20.0")]
+ #[inline]
+ pub fn sort_unstable_by_key<K, F>(&mut self, f: F)
+ where F: FnMut(&T) -> K, K: Ord
+ {
+ SliceExt::sort_unstable_by_key(self, f);
+ }
+
+ /// Rotates the slice in-place such that the first `mid` elements of the
+ /// slice move to the end while the last `self.len() - mid` elements move to
+ /// the front. After calling `rotate_left`, the element previously at index
+ /// `mid` will become the first element in the slice.
+ ///
+ /// # Panics
+ ///
+ /// This function will panic if `mid` is greater than the length of the
+ /// slice. Note that `mid == self.len()` does _not_ panic and is a no-op
+ /// rotation.
+ ///
+ /// # Complexity
+ ///
+ /// Takes linear (in `self.len()`) time.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let mut a = ['a', 'b', 'c', 'd', 'e', 'f'];
+ /// a.rotate_left(2);
+ /// assert_eq!(a, ['c', 'd', 'e', 'f', 'a', 'b']);
+ /// ```
+ ///
+ /// Rotating a subslice:
+ ///
+ /// ```
+ /// let mut a = ['a', 'b', 'c', 'd', 'e', 'f'];
+ /// a[1..5].rotate_left(1);
+ /// assert_eq!(a, ['a', 'c', 'd', 'e', 'b', 'f']);
+ /// ```
+ #[stable(feature = "slice_rotate", since = "1.26.0")]
+ pub fn rotate_left(&mut self, mid: usize) {
+ SliceExt::rotate_left(self, mid);
+ }
+
+ /// Rotates the slice in-place such that the first `self.len() - k`
+ /// elements of the slice move to the end while the last `k` elements move
+ /// to the front. After calling `rotate_right`, the element previously at
+ /// index `self.len() - k` will become the first element in the slice.
+ ///
+ /// # Panics
+ ///
+ /// This function will panic if `k` is greater than the length of the
+ /// slice. Note that `k == self.len()` does _not_ panic and is a no-op
+ /// rotation.
+ ///
+ /// # Complexity
+ ///
+ /// Takes linear (in `self.len()`) time.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let mut a = ['a', 'b', 'c', 'd', 'e', 'f'];
+ /// a.rotate_right(2);
+ /// assert_eq!(a, ['e', 'f', 'a', 'b', 'c', 'd']);
+ /// ```
+ ///
+ /// Rotate a subslice:
+ ///
+ /// ```
+ /// let mut a = ['a', 'b', 'c', 'd', 'e', 'f'];
+ /// a[1..5].rotate_right(1);
+ /// assert_eq!(a, ['a', 'e', 'b', 'c', 'd', 'f']);
+ /// ```
+ #[stable(feature = "slice_rotate", since = "1.26.0")]
+ pub fn rotate_right(&mut self, k: usize) {
+ SliceExt::rotate_right(self, k);
+ }
+
+ /// Copies the elements from `src` into `self`.
+ ///
+ /// The length of `src` must be the same as `self`.
+ ///
+ /// If `src` implements `Copy`, it can be more performant to use
+ /// [`copy_from_slice`].
+ ///
+ /// # Panics
+ ///
+ /// This function will panic if the two slices have different lengths.
+ ///
+ /// # Examples
+ ///
+ /// Cloning two elements from a slice into another:
+ ///
+ /// ```
+ /// let src = [1, 2, 3, 4];
+ /// let mut dst = [0, 0];
+ ///
+ /// dst.clone_from_slice(&src[2..]);
+ ///
+ /// assert_eq!(src, [1, 2, 3, 4]);
+ /// assert_eq!(dst, [3, 4]);
+ /// ```
+ ///
+ /// Rust enforces that there can only be one mutable reference with no
+ /// immutable references to a particular piece of data in a particular
+ /// scope. Because of this, attempting to use `clone_from_slice` on a
+ /// single slice will result in a compile failure:
+ ///
+ /// ```compile_fail
+ /// let mut slice = [1, 2, 3, 4, 5];
+ ///
+ /// slice[..2].clone_from_slice(&slice[3..]); // compile fail!
+ /// ```
+ ///
+ /// To work around this, we can use [`split_at_mut`] to create two distinct
+ /// sub-slices from a slice:
+ ///
+ /// ```
+ /// let mut slice = [1, 2, 3, 4, 5];
+ ///
+ /// {
+ /// let (left, right) = slice.split_at_mut(2);
+ /// left.clone_from_slice(&right[1..]);
+ /// }
+ ///
+ /// assert_eq!(slice, [4, 5, 3, 4, 5]);
+ /// ```
+ ///
+ /// [`copy_from_slice`]: #method.copy_from_slice
+ /// [`split_at_mut`]: #method.split_at_mut
+ #[stable(feature = "clone_from_slice", since = "1.7.0")]
+ pub fn clone_from_slice(&mut self, src: &[T]) where T: Clone {
+ SliceExt::clone_from_slice(self, src)
+ }
+
+ /// Copies all elements from `src` into `self`, using a memcpy.
+ ///
+ /// The length of `src` must be the same as `self`.
+ ///
+ /// If `src` does not implement `Copy`, use [`clone_from_slice`].
+ ///
+ /// # Panics
+ ///
+ /// This function will panic if the two slices have different lengths.
+ ///
+ /// # Examples
+ ///
+ /// Copying two elements from a slice into another:
+ ///
+ /// ```
+ /// let src = [1, 2, 3, 4];
+ /// let mut dst = [0, 0];
+ ///
+ /// dst.copy_from_slice(&src[2..]);
+ ///
+ /// assert_eq!(src, [1, 2, 3, 4]);
+ /// assert_eq!(dst, [3, 4]);
+ /// ```
+ ///
+ /// Rust enforces that there can only be one mutable reference with no
+ /// immutable references to a particular piece of data in a particular
+ /// scope. Because of this, attempting to use `copy_from_slice` on a
+ /// single slice will result in a compile failure:
+ ///
+ /// ```compile_fail
+ /// let mut slice = [1, 2, 3, 4, 5];
+ ///
+ /// slice[..2].copy_from_slice(&slice[3..]); // compile fail!
+ /// ```
+ ///
+ /// To work around this, we can use [`split_at_mut`] to create two distinct
+ /// sub-slices from a slice:
+ ///
+ /// ```
+ /// let mut slice = [1, 2, 3, 4, 5];
+ ///
+ /// {
+ /// let (left, right) = slice.split_at_mut(2);
+ /// left.copy_from_slice(&right[1..]);
+ /// }
+ ///
+ /// assert_eq!(slice, [4, 5, 3, 4, 5]);
+ /// ```
+ ///
+ /// [`clone_from_slice`]: #method.clone_from_slice
+ /// [`split_at_mut`]: #method.split_at_mut
+ #[stable(feature = "copy_from_slice", since = "1.9.0")]
+ pub fn copy_from_slice(&mut self, src: &[T]) where T: Copy {
+ SliceExt::copy_from_slice(self, src)
+ }
+
+ /// Swaps all elements in `self` with those in `other`.
+ ///
+ /// The length of `other` must be the same as `self`.
+ ///
+ /// # Panics
+ ///
+ /// This function will panic if the two slices have different lengths.
+ ///
+ /// # Example
+ ///
+ /// Swapping two elements across slices:
+ ///
+ /// ```
+ /// let mut slice1 = [0, 0];
+ /// let mut slice2 = [1, 2, 3, 4];
+ ///
+ /// slice1.swap_with_slice(&mut slice2[2..]);
+ ///
+ /// assert_eq!(slice1, [3, 4]);
+ /// assert_eq!(slice2, [1, 2, 0, 0]);
+ /// ```
+ ///
+ /// Rust enforces that there can only be one mutable reference to a
+ /// particular piece of data in a particular scope. Because of this,
+ /// attempting to use `swap_with_slice` on a single slice will result in
+ /// a compile failure:
+ ///
+ /// ```compile_fail
+ /// let mut slice = [1, 2, 3, 4, 5];
+ /// slice[..2].swap_with_slice(&mut slice[3..]); // compile fail!
+ /// ```
+ ///
+ /// To work around this, we can use [`split_at_mut`] to create two distinct
+ /// mutable sub-slices from a slice:
+ ///
+ /// ```
+ /// let mut slice = [1, 2, 3, 4, 5];
+ ///
+ /// {
+ /// let (left, right) = slice.split_at_mut(2);
+ /// left.swap_with_slice(&mut right[1..]);
+ /// }
+ ///
+ /// assert_eq!(slice, [4, 5, 3, 1, 2]);
+ /// ```
+ ///
+ /// [`split_at_mut`]: #method.split_at_mut
+ #[stable(feature = "swap_with_slice", since = "1.27.0")]
+ pub fn swap_with_slice(&mut self, other: &mut [T]) {
+ SliceExt::swap_with_slice(self, other)
+ }
+}}
+
+#[lang = "slice"]
+#[cfg(not(test))]
+#[cfg(not(stage0))]
+impl<T> [T] {
+ slice_core_methods!();
+}
+
+// FIXME: remove (inline) this macro
+// when updating to a bootstrap compiler that has the new lang items.
+#[cfg_attr(stage0, macro_export)]
+#[unstable(feature = "core_slice_ext", issue = "32110")]
+macro_rules! slice_u8_core_methods { () => {
+ /// Checks if all bytes in this slice are within the ASCII range.
+ #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
+ #[inline]
+ pub fn is_ascii(&self) -> bool {
+ self.iter().all(|b| b.is_ascii())
+ }
+
+ /// Checks that two slices are an ASCII case-insensitive match.
+ ///
+ /// Same as `to_ascii_lowercase(a) == to_ascii_lowercase(b)`,
+ /// but without allocating and copying temporaries.
+ #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
+ #[inline]
+ pub fn eq_ignore_ascii_case(&self, other: &[u8]) -> bool {
+ self.len() == other.len() &&
+ self.iter().zip(other).all(|(a, b)| {
+ a.eq_ignore_ascii_case(b)
+ })
+ }
+
+ /// Converts this slice to its ASCII upper case equivalent in-place.
+ ///
+ /// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
+ /// but non-ASCII letters are unchanged.
+ ///
+ /// To return a new uppercased value without modifying the existing one, use
+ /// [`to_ascii_uppercase`].
+ ///
+ /// [`to_ascii_uppercase`]: #method.to_ascii_uppercase
+ #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
+ #[inline]
+ pub fn make_ascii_uppercase(&mut self) {
+ for byte in self {
+ byte.make_ascii_uppercase();
+ }
+ }
+
+ /// Converts this slice to its ASCII lower case equivalent in-place.
+ ///
+ /// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
+ /// but non-ASCII letters are unchanged.
+ ///
+ /// To return a new lowercased value without modifying the existing one, use
+ /// [`to_ascii_lowercase`].
+ ///
+ /// [`to_ascii_lowercase`]: #method.to_ascii_lowercase
+ #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
+ #[inline]
+ pub fn make_ascii_lowercase(&mut self) {
+ for byte in self {
+ byte.make_ascii_lowercase();
+ }
+ }
+}}
+
+#[lang = "slice_u8"]
+#[cfg(not(test))]
+#[cfg(not(stage0))]
+impl [u8] {
+ slice_u8_core_methods!();
+}
+
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_on_unimplemented = "slice indices are of type `usize` or ranges of `usize`"]
impl<T, I> ops::Index<I> for [T]
}
-
+public_in_stage0! {
+{
/// Methods for string slices
#[allow(missing_docs)]
#[doc(hidden)]
#[unstable(feature = "core_str_ext",
reason = "stable interface provided by `impl str` in later crates",
issue = "32110")]
-pub trait StrExt {
+}
+trait StrExt {
// NB there are no docs here are they're all located on the StrExt trait in
// liballoc, not here.
fn parse<T: FromStr>(&self) -> Result<T, T::Err>;
#[stable(feature = "split_whitespace", since = "1.1.0")]
fn split_whitespace<'a>(&'a self) -> SplitWhitespace<'a>;
- #[stable(feature = "unicode_methods_on_intrinsics", since = "1.27.0")]
- fn is_whitespace(&self) -> bool;
- #[stable(feature = "unicode_methods_on_intrinsics", since = "1.27.0")]
- fn is_alphanumeric(&self) -> bool;
#[stable(feature = "rust1", since = "1.0.0")]
fn trim(&self) -> &str;
#[stable(feature = "rust1", since = "1.0.0")]
fn trim_left(&self) -> &str;
#[stable(feature = "rust1", since = "1.0.0")]
fn trim_right(&self) -> &str;
-}
+}}
// truncate `&str` to length at most equal to `max`
// return `true` if it were truncated, and the new str.
SplitWhitespace { inner: self.split(IsWhitespace).filter(IsNotEmpty) }
}
- #[inline]
- fn is_whitespace(&self) -> bool {
- self.chars().all(|c| c.is_whitespace())
- }
-
- #[inline]
- fn is_alphanumeric(&self) -> bool {
- self.chars().all(|c| c.is_alphanumeric())
- }
-
#[inline]
fn trim(&self) -> &str {
self.trim_matches(|c: char| c.is_whitespace())
}
}
-#[stable(feature = "rust1", since = "1.0.0")]
-impl AsRef<[u8]> for str {
+// FIXME: remove (inline) this macro and the SliceExt trait
+// when updating to a bootstrap compiler that has the new lang items.
+#[cfg_attr(stage0, macro_export)]
+#[unstable(feature = "core_str_ext", issue = "32110")]
+macro_rules! str_core_methods { () => {
+ /// Returns the length of `self`.
+ ///
+ /// This length is in bytes, not [`char`]s or graphemes. In other words,
+ /// it may not be what a human considers the length of the string.
+ ///
+ /// [`char`]: primitive.char.html
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// let len = "foo".len();
+ /// assert_eq!(3, len);
+ ///
+ /// let len = "ƒoo".len(); // fancy f!
+ /// assert_eq!(4, len);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
#[inline]
- fn as_ref(&self) -> &[u8] {
- self.as_bytes()
+ pub fn len(&self) -> usize {
+ StrExt::len(self)
}
-}
-#[stable(feature = "rust1", since = "1.0.0")]
-impl<'a> Default for &'a str {
- /// Creates an empty str
- fn default() -> &'a str { "" }
-}
+ /// Returns `true` if `self` has a length of zero bytes.
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// let s = "";
+ /// assert!(s.is_empty());
+ ///
+ /// let s = "not empty";
+ /// assert!(!s.is_empty());
+ /// ```
+ #[inline]
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn is_empty(&self) -> bool {
+ StrExt::is_empty(self)
+ }
-/// An iterator over the non-whitespace substrings of a string,
-/// separated by any amount of whitespace.
-///
-/// This struct is created by the [`split_whitespace`] method on [`str`].
-/// See its documentation for more.
-///
-/// [`split_whitespace`]: ../../std/primitive.str.html#method.split_whitespace
-/// [`str`]: ../../std/primitive.str.html
-#[stable(feature = "split_whitespace", since = "1.1.0")]
-#[derive(Clone, Debug)]
-pub struct SplitWhitespace<'a> {
- inner: Filter<Split<'a, IsWhitespace>, IsNotEmpty>,
-}
+ /// Checks that `index`-th byte lies at the start and/or end of a
+ /// UTF-8 code point sequence.
+ ///
+ /// The start and end of the string (when `index == self.len()`) are
+ /// considered to be
+ /// boundaries.
+ ///
+ /// Returns `false` if `index` is greater than `self.len()`.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let s = "Löwe 老虎 Léopard";
+ /// assert!(s.is_char_boundary(0));
+ /// // start of `老`
+ /// assert!(s.is_char_boundary(6));
+ /// assert!(s.is_char_boundary(s.len()));
+ ///
+ /// // second byte of `ö`
+ /// assert!(!s.is_char_boundary(2));
+ ///
+ /// // third byte of `老`
+ /// assert!(!s.is_char_boundary(8));
+ /// ```
+ #[stable(feature = "is_char_boundary", since = "1.9.0")]
+ #[inline]
+ pub fn is_char_boundary(&self, index: usize) -> bool {
+ StrExt::is_char_boundary(self, index)
+ }
-#[derive(Clone)]
-struct IsWhitespace;
+ /// Converts a string slice to a byte slice. To convert the byte slice back
+ /// into a string slice, use the [`str::from_utf8`] function.
+ ///
+ /// [`str::from_utf8`]: ./str/fn.from_utf8.html
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// let bytes = "bors".as_bytes();
+ /// assert_eq!(b"bors", bytes);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline(always)]
+ pub fn as_bytes(&self) -> &[u8] {
+ StrExt::as_bytes(self)
+ }
-impl FnOnce<(char, )> for IsWhitespace {
- type Output = bool;
+ /// Converts a mutable string slice to a mutable byte slice. To convert the
+ /// mutable byte slice back into a mutable string slice, use the
+ /// [`str::from_utf8_mut`] function.
+ ///
+ /// [`str::from_utf8_mut`]: ./str/fn.from_utf8_mut.html
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// let mut s = String::from("Hello");
+ /// let bytes = unsafe { s.as_bytes_mut() };
+ ///
+ /// assert_eq!(b"Hello", bytes);
+ /// ```
+ ///
+ /// Mutability:
+ ///
+ /// ```
+ /// let mut s = String::from("🗻∈🌏");
+ ///
+ /// unsafe {
+ /// let bytes = s.as_bytes_mut();
+ ///
+ /// bytes[0] = 0xF0;
+ /// bytes[1] = 0x9F;
+ /// bytes[2] = 0x8D;
+ /// bytes[3] = 0x94;
+ /// }
+ ///
+ /// assert_eq!("🍔∈🌏", s);
+ /// ```
+ #[stable(feature = "str_mut_extras", since = "1.20.0")]
+ #[inline(always)]
+ pub unsafe fn as_bytes_mut(&mut self) -> &mut [u8] {
+ StrExt::as_bytes_mut(self)
+ }
+ /// Converts a string slice to a raw pointer.
+ ///
+ /// As string slices are a slice of bytes, the raw pointer points to a
+ /// [`u8`]. This pointer will be pointing to the first byte of the string
+ /// slice.
+ ///
+ /// [`u8`]: primitive.u8.html
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// let s = "Hello";
+ /// let ptr = s.as_ptr();
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
#[inline]
- extern "rust-call" fn call_once(mut self, arg: (char, )) -> bool {
- self.call_mut(arg)
+ pub fn as_ptr(&self) -> *const u8 {
+ StrExt::as_ptr(self)
}
-}
-impl FnMut<(char, )> for IsWhitespace {
+ /// Returns a subslice of `str`.
+ ///
+ /// This is the non-panicking alternative to indexing the `str`. Returns
+ /// [`None`] whenever equivalent indexing operation would panic.
+ ///
+ /// [`None`]: option/enum.Option.html#variant.None
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let v = String::from("🗻∈🌏");
+ ///
+ /// assert_eq!(Some("🗻"), v.get(0..4));
+ ///
+ /// // indices not on UTF-8 sequence boundaries
+ /// assert!(v.get(1..).is_none());
+ /// assert!(v.get(..8).is_none());
+ ///
+ /// // out of bounds
+ /// assert!(v.get(..42).is_none());
+ /// ```
+ #[stable(feature = "str_checked_slicing", since = "1.20.0")]
#[inline]
- extern "rust-call" fn call_mut(&mut self, arg: (char, )) -> bool {
- arg.0.is_whitespace()
+ pub fn get<I: SliceIndex<str>>(&self, i: I) -> Option<&I::Output> {
+ StrExt::get(self, i)
}
-}
-#[derive(Clone)]
-struct IsNotEmpty;
-
-impl<'a, 'b> FnOnce<(&'a &'b str, )> for IsNotEmpty {
- type Output = bool;
+ /// Returns a mutable subslice of `str`.
+ ///
+ /// This is the non-panicking alternative to indexing the `str`. Returns
+ /// [`None`] whenever equivalent indexing operation would panic.
+ ///
+ /// [`None`]: option/enum.Option.html#variant.None
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let mut v = String::from("hello");
+ /// // correct length
+ /// assert!(v.get_mut(0..5).is_some());
+ /// // out of bounds
+ /// assert!(v.get_mut(..42).is_none());
+ /// assert_eq!(Some("he"), v.get_mut(0..2).map(|v| &*v));
+ ///
+ /// assert_eq!("hello", v);
+ /// {
+ /// let s = v.get_mut(0..2);
+ /// let s = s.map(|s| {
+ /// s.make_ascii_uppercase();
+ /// &*s
+ /// });
+ /// assert_eq!(Some("HE"), s);
+ /// }
+ /// assert_eq!("HEllo", v);
+ /// ```
+ #[stable(feature = "str_checked_slicing", since = "1.20.0")]
+ #[inline]
+ pub fn get_mut<I: SliceIndex<str>>(&mut self, i: I) -> Option<&mut I::Output> {
+ StrExt::get_mut(self, i)
+ }
+ /// Returns a unchecked subslice of `str`.
+ ///
+ /// This is the unchecked alternative to indexing the `str`.
+ ///
+ /// # Safety
+ ///
+ /// Callers of this function are responsible that these preconditions are
+ /// satisfied:
+ ///
+ /// * The starting index must come before the ending index;
+ /// * Indexes must be within bounds of the original slice;
+ /// * Indexes must lie on UTF-8 sequence boundaries.
+ ///
+ /// Failing that, the returned string slice may reference invalid memory or
+ /// violate the invariants communicated by the `str` type.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let v = "🗻∈🌏";
+ /// unsafe {
+ /// assert_eq!("🗻", v.get_unchecked(0..4));
+ /// assert_eq!("∈", v.get_unchecked(4..7));
+ /// assert_eq!("🌏", v.get_unchecked(7..11));
+ /// }
+ /// ```
+ #[stable(feature = "str_checked_slicing", since = "1.20.0")]
#[inline]
- extern "rust-call" fn call_once(mut self, arg: (&&str, )) -> bool {
- self.call_mut(arg)
+ pub unsafe fn get_unchecked<I: SliceIndex<str>>(&self, i: I) -> &I::Output {
+ StrExt::get_unchecked(self, i)
}
-}
-impl<'a, 'b> FnMut<(&'a &'b str, )> for IsNotEmpty {
+ /// Returns a mutable, unchecked subslice of `str`.
+ ///
+ /// This is the unchecked alternative to indexing the `str`.
+ ///
+ /// # Safety
+ ///
+ /// Callers of this function are responsible that these preconditions are
+ /// satisfied:
+ ///
+ /// * The starting index must come before the ending index;
+ /// * Indexes must be within bounds of the original slice;
+ /// * Indexes must lie on UTF-8 sequence boundaries.
+ ///
+ /// Failing that, the returned string slice may reference invalid memory or
+ /// violate the invariants communicated by the `str` type.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let mut v = String::from("🗻∈🌏");
+ /// unsafe {
+ /// assert_eq!("🗻", v.get_unchecked_mut(0..4));
+ /// assert_eq!("∈", v.get_unchecked_mut(4..7));
+ /// assert_eq!("🌏", v.get_unchecked_mut(7..11));
+ /// }
+ /// ```
+ #[stable(feature = "str_checked_slicing", since = "1.20.0")]
#[inline]
- extern "rust-call" fn call_mut(&mut self, arg: (&&str, )) -> bool {
- !arg.0.is_empty()
+ pub unsafe fn get_unchecked_mut<I: SliceIndex<str>>(&mut self, i: I) -> &mut I::Output {
+ StrExt::get_unchecked_mut(self, i)
}
-}
+ /// Creates a string slice from another string slice, bypassing safety
+ /// checks.
+ ///
+ /// This is generally not recommended, use with caution! For a safe
+ /// alternative see [`str`] and [`Index`].
+ ///
+ /// [`str`]: primitive.str.html
+ /// [`Index`]: ops/trait.Index.html
+ ///
+ /// This new slice goes from `begin` to `end`, including `begin` but
+ /// excluding `end`.
+ ///
+ /// To get a mutable string slice instead, see the
+ /// [`slice_mut_unchecked`] method.
+ ///
+ /// [`slice_mut_unchecked`]: #method.slice_mut_unchecked
+ ///
+ /// # Safety
+ ///
+ /// Callers of this function are responsible that three preconditions are
+ /// satisfied:
+ ///
+ /// * `begin` must come before `end`.
+ /// * `begin` and `end` must be byte positions within the string slice.
+ /// * `begin` and `end` must lie on UTF-8 sequence boundaries.
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// let s = "Löwe 老虎 Léopard";
+ ///
+ /// unsafe {
+ /// assert_eq!("Löwe 老虎 Léopard", s.slice_unchecked(0, 21));
+ /// }
+ ///
+ /// let s = "Hello, world!";
+ ///
+ /// unsafe {
+ /// assert_eq!("world", s.slice_unchecked(7, 12));
+ /// }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub unsafe fn slice_unchecked(&self, begin: usize, end: usize) -> &str {
+ StrExt::slice_unchecked(self, begin, end)
+ }
-#[stable(feature = "split_whitespace", since = "1.1.0")]
-impl<'a> Iterator for SplitWhitespace<'a> {
- type Item = &'a str;
+ /// Creates a string slice from another string slice, bypassing safety
+ /// checks.
+ /// This is generally not recommended, use with caution! For a safe
+ /// alternative see [`str`] and [`IndexMut`].
+ ///
+ /// [`str`]: primitive.str.html
+ /// [`IndexMut`]: ops/trait.IndexMut.html
+ ///
+ /// This new slice goes from `begin` to `end`, including `begin` but
+ /// excluding `end`.
+ ///
+ /// To get an immutable string slice instead, see the
+ /// [`slice_unchecked`] method.
+ ///
+ /// [`slice_unchecked`]: #method.slice_unchecked
+ ///
+ /// # Safety
+ ///
+ /// Callers of this function are responsible that three preconditions are
+ /// satisfied:
+ ///
+ /// * `begin` must come before `end`.
+ /// * `begin` and `end` must be byte positions within the string slice.
+ /// * `begin` and `end` must lie on UTF-8 sequence boundaries.
+ #[stable(feature = "str_slice_mut", since = "1.5.0")]
+ #[inline]
+ pub unsafe fn slice_mut_unchecked(&mut self, begin: usize, end: usize) -> &mut str {
+ StrExt::slice_mut_unchecked(self, begin, end)
+ }
- fn next(&mut self) -> Option<&'a str> {
- self.inner.next()
+ /// Divide one string slice into two at an index.
+ ///
+ /// The argument, `mid`, should be a byte offset from the start of the
+ /// string. It must also be on the boundary of a UTF-8 code point.
+ ///
+ /// The two slices returned go from the start of the string slice to `mid`,
+ /// and from `mid` to the end of the string slice.
+ ///
+ /// To get mutable string slices instead, see the [`split_at_mut`]
+ /// method.
+ ///
+ /// [`split_at_mut`]: #method.split_at_mut
+ ///
+ /// # Panics
+ ///
+ /// Panics if `mid` is not on a UTF-8 code point boundary, or if it is
+ /// beyond the last code point of the string slice.
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// let s = "Per Martin-Löf";
+ ///
+ /// let (first, last) = s.split_at(3);
+ ///
+ /// assert_eq!("Per", first);
+ /// assert_eq!(" Martin-Löf", last);
+ /// ```
+ #[inline]
+ #[stable(feature = "str_split_at", since = "1.4.0")]
+ pub fn split_at(&self, mid: usize) -> (&str, &str) {
+ StrExt::split_at(self, mid)
}
-}
-#[stable(feature = "split_whitespace", since = "1.1.0")]
-impl<'a> DoubleEndedIterator for SplitWhitespace<'a> {
- fn next_back(&mut self) -> Option<&'a str> {
- self.inner.next_back()
+ /// Divide one mutable string slice into two at an index.
+ ///
+ /// The argument, `mid`, should be a byte offset from the start of the
+ /// string. It must also be on the boundary of a UTF-8 code point.
+ ///
+ /// The two slices returned go from the start of the string slice to `mid`,
+ /// and from `mid` to the end of the string slice.
+ ///
+ /// To get immutable string slices instead, see the [`split_at`] method.
+ ///
+ /// [`split_at`]: #method.split_at
+ ///
+ /// # Panics
+ ///
+ /// Panics if `mid` is not on a UTF-8 code point boundary, or if it is
+ /// beyond the last code point of the string slice.
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// let mut s = "Per Martin-Löf".to_string();
+ /// {
+ /// let (first, last) = s.split_at_mut(3);
+ /// first.make_ascii_uppercase();
+ /// assert_eq!("PER", first);
+ /// assert_eq!(" Martin-Löf", last);
+ /// }
+ /// assert_eq!("PER Martin-Löf", s);
+ /// ```
+ #[inline]
+ #[stable(feature = "str_split_at", since = "1.4.0")]
+ pub fn split_at_mut(&mut self, mid: usize) -> (&mut str, &mut str) {
+ StrExt::split_at_mut(self, mid)
}
-}
-#[stable(feature = "fused", since = "1.26.0")]
-impl<'a> FusedIterator for SplitWhitespace<'a> {}
+ /// Returns an iterator over the [`char`]s of a string slice.
+ ///
+ /// As a string slice consists of valid UTF-8, we can iterate through a
+ /// string slice by [`char`]. This method returns such an iterator.
+ ///
+ /// It's important to remember that [`char`] represents a Unicode Scalar
+ /// Value, and may not match your idea of what a 'character' is. Iteration
+ /// over grapheme clusters may be what you actually want.
+ ///
+ /// [`char`]: primitive.char.html
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// let word = "goodbye";
+ ///
+ /// let count = word.chars().count();
+ /// assert_eq!(7, count);
+ ///
+ /// let mut chars = word.chars();
+ ///
+ /// assert_eq!(Some('g'), chars.next());
+ /// assert_eq!(Some('o'), chars.next());
+ /// assert_eq!(Some('o'), chars.next());
+ /// assert_eq!(Some('d'), chars.next());
+ /// assert_eq!(Some('b'), chars.next());
+ /// assert_eq!(Some('y'), chars.next());
+ /// assert_eq!(Some('e'), chars.next());
+ ///
+ /// assert_eq!(None, chars.next());
+ /// ```
+ ///
+ /// Remember, [`char`]s may not match your human intuition about characters:
+ ///
+ /// ```
+ /// let y = "y̆";
+ ///
+ /// let mut chars = y.chars();
+ ///
+ /// assert_eq!(Some('y'), chars.next()); // not 'y̆'
+ /// assert_eq!(Some('\u{0306}'), chars.next());
+ ///
+ /// assert_eq!(None, chars.next());
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn chars(&self) -> Chars {
+ StrExt::chars(self)
+ }
+ /// Returns an iterator over the [`char`]s of a string slice, and their
+ /// positions.
+ ///
+ /// As a string slice consists of valid UTF-8, we can iterate through a
+ /// string slice by [`char`]. This method returns an iterator of both
+ /// these [`char`]s, as well as their byte positions.
+ ///
+ /// The iterator yields tuples. The position is first, the [`char`] is
+ /// second.
+ ///
+ /// [`char`]: primitive.char.html
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// let word = "goodbye";
+ ///
+ /// let count = word.char_indices().count();
+ /// assert_eq!(7, count);
+ ///
+ /// let mut char_indices = word.char_indices();
+ ///
+ /// assert_eq!(Some((0, 'g')), char_indices.next());
+ /// assert_eq!(Some((1, 'o')), char_indices.next());
+ /// assert_eq!(Some((2, 'o')), char_indices.next());
+ /// assert_eq!(Some((3, 'd')), char_indices.next());
+ /// assert_eq!(Some((4, 'b')), char_indices.next());
+ /// assert_eq!(Some((5, 'y')), char_indices.next());
+ /// assert_eq!(Some((6, 'e')), char_indices.next());
+ ///
+ /// assert_eq!(None, char_indices.next());
+ /// ```
+ ///
+ /// Remember, [`char`]s may not match your human intuition about characters:
+ ///
+ /// ```
+ /// let yes = "y̆es";
+ ///
+ /// let mut char_indices = yes.char_indices();
+ ///
+ /// assert_eq!(Some((0, 'y')), char_indices.next()); // not (0, 'y̆')
+ /// assert_eq!(Some((1, '\u{0306}')), char_indices.next());
+ ///
+ /// // note the 3 here - the last character took up two bytes
+ /// assert_eq!(Some((3, 'e')), char_indices.next());
+ /// assert_eq!(Some((4, 's')), char_indices.next());
+ ///
+ /// assert_eq!(None, char_indices.next());
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn char_indices(&self) -> CharIndices {
+ StrExt::char_indices(self)
+ }
+
+ /// An iterator over the bytes of a string slice.
+ ///
+ /// As a string slice consists of a sequence of bytes, we can iterate
+ /// through a string slice by byte. This method returns such an iterator.
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// let mut bytes = "bors".bytes();
+ ///
+ /// assert_eq!(Some(b'b'), bytes.next());
+ /// assert_eq!(Some(b'o'), bytes.next());
+ /// assert_eq!(Some(b'r'), bytes.next());
+ /// assert_eq!(Some(b's'), bytes.next());
+ ///
+ /// assert_eq!(None, bytes.next());
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn bytes(&self) -> Bytes {
+ StrExt::bytes(self)
+ }
+
+ /// Split a string slice by whitespace.
+ ///
+ /// The iterator returned will return string slices that are sub-slices of
+ /// the original string slice, separated by any amount of whitespace.
+ ///
+ /// 'Whitespace' is defined according to the terms of the Unicode Derived
+ /// Core Property `White_Space`.
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// let mut iter = "A few words".split_whitespace();
+ ///
+ /// assert_eq!(Some("A"), iter.next());
+ /// assert_eq!(Some("few"), iter.next());
+ /// assert_eq!(Some("words"), iter.next());
+ ///
+ /// assert_eq!(None, iter.next());
+ /// ```
+ ///
+ /// All kinds of whitespace are considered:
+ ///
+ /// ```
+ /// let mut iter = " Mary had\ta\u{2009}little \n\t lamb".split_whitespace();
+ /// assert_eq!(Some("Mary"), iter.next());
+ /// assert_eq!(Some("had"), iter.next());
+ /// assert_eq!(Some("a"), iter.next());
+ /// assert_eq!(Some("little"), iter.next());
+ /// assert_eq!(Some("lamb"), iter.next());
+ ///
+ /// assert_eq!(None, iter.next());
+ /// ```
+ #[stable(feature = "split_whitespace", since = "1.1.0")]
+ #[inline]
+ pub fn split_whitespace(&self) -> SplitWhitespace {
+ StrExt::split_whitespace(self)
+ }
+
+ /// An iterator over the lines of a string, as string slices.
+ ///
+ /// Lines are ended with either a newline (`\n`) or a carriage return with
+ /// a line feed (`\r\n`).
+ ///
+ /// The final line ending is optional.
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// let text = "foo\r\nbar\n\nbaz\n";
+ /// let mut lines = text.lines();
+ ///
+ /// assert_eq!(Some("foo"), lines.next());
+ /// assert_eq!(Some("bar"), lines.next());
+ /// assert_eq!(Some(""), lines.next());
+ /// assert_eq!(Some("baz"), lines.next());
+ ///
+ /// assert_eq!(None, lines.next());
+ /// ```
+ ///
+ /// The final line ending isn't required:
+ ///
+ /// ```
+ /// let text = "foo\nbar\n\r\nbaz";
+ /// let mut lines = text.lines();
+ ///
+ /// assert_eq!(Some("foo"), lines.next());
+ /// assert_eq!(Some("bar"), lines.next());
+ /// assert_eq!(Some(""), lines.next());
+ /// assert_eq!(Some("baz"), lines.next());
+ ///
+ /// assert_eq!(None, lines.next());
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn lines(&self) -> Lines {
+ StrExt::lines(self)
+ }
+
+ /// An iterator over the lines of a string.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[rustc_deprecated(since = "1.4.0", reason = "use lines() instead now")]
+ #[inline]
+ #[allow(deprecated)]
+ pub fn lines_any(&self) -> LinesAny {
+ StrExt::lines_any(self)
+ }
+
+ /// Returns an iterator of `u16` over the string encoded as UTF-16.
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// let text = "Zażółć gęślą jaźń";
+ ///
+ /// let utf8_len = text.len();
+ /// let utf16_len = text.encode_utf16().count();
+ ///
+ /// assert!(utf16_len <= utf8_len);
+ /// ```
+ #[stable(feature = "encode_utf16", since = "1.8.0")]
+ pub fn encode_utf16(&self) -> EncodeUtf16 {
+ EncodeUtf16::new(self)
+ }
+
+ /// Returns `true` if the given pattern matches a sub-slice of
+ /// this string slice.
+ ///
+ /// Returns `false` if it does not.
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// let bananas = "bananas";
+ ///
+ /// assert!(bananas.contains("nana"));
+ /// assert!(!bananas.contains("apples"));
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn contains<'a, P: Pattern<'a>>(&'a self, pat: P) -> bool {
+ StrExt::contains(self, pat)
+ }
+
+ /// Returns `true` if the given pattern matches a prefix of this
+ /// string slice.
+ ///
+ /// Returns `false` if it does not.
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// let bananas = "bananas";
+ ///
+ /// assert!(bananas.starts_with("bana"));
+ /// assert!(!bananas.starts_with("nana"));
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn starts_with<'a, P: Pattern<'a>>(&'a self, pat: P) -> bool {
+ StrExt::starts_with(self, pat)
+ }
+
+ /// Returns `true` if the given pattern matches a suffix of this
+ /// string slice.
+ ///
+ /// Returns `false` if it does not.
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// let bananas = "bananas";
+ ///
+ /// assert!(bananas.ends_with("anas"));
+ /// assert!(!bananas.ends_with("nana"));
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn ends_with<'a, P: Pattern<'a>>(&'a self, pat: P) -> bool
+ where P::Searcher: ReverseSearcher<'a>
+ {
+ StrExt::ends_with(self, pat)
+ }
+
+ /// Returns the byte index of the first character of this string slice that
+ /// matches the pattern.
+ ///
+ /// Returns [`None`] if the pattern doesn't match.
+ ///
+ /// The pattern can be a `&str`, [`char`], or a closure that determines if
+ /// a character matches.
+ ///
+ /// [`char`]: primitive.char.html
+ /// [`None`]: option/enum.Option.html#variant.None
+ ///
+ /// # Examples
+ ///
+ /// Simple patterns:
+ ///
+ /// ```
+ /// let s = "Löwe 老虎 Léopard";
+ ///
+ /// assert_eq!(s.find('L'), Some(0));
+ /// assert_eq!(s.find('é'), Some(14));
+ /// assert_eq!(s.find("Léopard"), Some(13));
+ /// ```
+ ///
+ /// More complex patterns using point-free style and closures:
+ ///
+ /// ```
+ /// let s = "Löwe 老虎 Léopard";
+ ///
+ /// assert_eq!(s.find(char::is_whitespace), Some(5));
+ /// assert_eq!(s.find(char::is_lowercase), Some(1));
+ /// assert_eq!(s.find(|c: char| c.is_whitespace() || c.is_lowercase()), Some(1));
+ /// assert_eq!(s.find(|c: char| (c < 'o') && (c > 'a')), Some(4));
+ /// ```
+ ///
+ /// Not finding the pattern:
+ ///
+ /// ```
+ /// let s = "Löwe 老虎 Léopard";
+ /// let x: &[_] = &['1', '2'];
+ ///
+ /// assert_eq!(s.find(x), None);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn find<'a, P: Pattern<'a>>(&'a self, pat: P) -> Option<usize> {
+ StrExt::find(self, pat)
+ }
+
+ /// Returns the byte index of the last character of this string slice that
+ /// matches the pattern.
+ ///
+ /// Returns [`None`] if the pattern doesn't match.
+ ///
+ /// The pattern can be a `&str`, [`char`], or a closure that determines if
+ /// a character matches.
+ ///
+ /// [`char`]: primitive.char.html
+ /// [`None`]: option/enum.Option.html#variant.None
+ ///
+ /// # Examples
+ ///
+ /// Simple patterns:
+ ///
+ /// ```
+ /// let s = "Löwe 老虎 Léopard";
+ ///
+ /// assert_eq!(s.rfind('L'), Some(13));
+ /// assert_eq!(s.rfind('é'), Some(14));
+ /// ```
+ ///
+ /// More complex patterns with closures:
+ ///
+ /// ```
+ /// let s = "Löwe 老虎 Léopard";
+ ///
+ /// assert_eq!(s.rfind(char::is_whitespace), Some(12));
+ /// assert_eq!(s.rfind(char::is_lowercase), Some(20));
+ /// ```
+ ///
+ /// Not finding the pattern:
+ ///
+ /// ```
+ /// let s = "Löwe 老虎 Léopard";
+ /// let x: &[_] = &['1', '2'];
+ ///
+ /// assert_eq!(s.rfind(x), None);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn rfind<'a, P: Pattern<'a>>(&'a self, pat: P) -> Option<usize>
+ where P::Searcher: ReverseSearcher<'a>
+ {
+ StrExt::rfind(self, pat)
+ }
+
+ /// An iterator over substrings of this string slice, separated by
+ /// characters matched by a pattern.
+ ///
+ /// The pattern can be a `&str`, [`char`], or a closure that determines the
+ /// split.
+ ///
+ /// # Iterator behavior
+ ///
+ /// The returned iterator will be a [`DoubleEndedIterator`] if the pattern
+ /// allows a reverse search and forward/reverse search yields the same
+ /// elements. This is true for, eg, [`char`] but not for `&str`.
+ ///
+ /// [`DoubleEndedIterator`]: iter/trait.DoubleEndedIterator.html
+ ///
+ /// If the pattern allows a reverse search but its results might differ
+ /// from a forward search, the [`rsplit`] method can be used.
+ ///
+ /// [`char`]: primitive.char.html
+ /// [`rsplit`]: #method.rsplit
+ ///
+ /// # Examples
+ ///
+ /// Simple patterns:
+ ///
+ /// ```
+ /// let v: Vec<&str> = "Mary had a little lamb".split(' ').collect();
+ /// assert_eq!(v, ["Mary", "had", "a", "little", "lamb"]);
+ ///
+ /// let v: Vec<&str> = "".split('X').collect();
+ /// assert_eq!(v, [""]);
+ ///
+ /// let v: Vec<&str> = "lionXXtigerXleopard".split('X').collect();
+ /// assert_eq!(v, ["lion", "", "tiger", "leopard"]);
+ ///
+ /// let v: Vec<&str> = "lion::tiger::leopard".split("::").collect();
+ /// assert_eq!(v, ["lion", "tiger", "leopard"]);
+ ///
+ /// let v: Vec<&str> = "abc1def2ghi".split(char::is_numeric).collect();
+ /// assert_eq!(v, ["abc", "def", "ghi"]);
+ ///
+ /// let v: Vec<&str> = "lionXtigerXleopard".split(char::is_uppercase).collect();
+ /// assert_eq!(v, ["lion", "tiger", "leopard"]);
+ /// ```
+ ///
+ /// A more complex pattern, using a closure:
+ ///
+ /// ```
+ /// let v: Vec<&str> = "abc1defXghi".split(|c| c == '1' || c == 'X').collect();
+ /// assert_eq!(v, ["abc", "def", "ghi"]);
+ /// ```
+ ///
+ /// If a string contains multiple contiguous separators, you will end up
+ /// with empty strings in the output:
+ ///
+ /// ```
+ /// let x = "||||a||b|c".to_string();
+ /// let d: Vec<_> = x.split('|').collect();
+ ///
+ /// assert_eq!(d, &["", "", "", "", "a", "", "b", "c"]);
+ /// ```
+ ///
+ /// Contiguous separators are separated by the empty string.
+ ///
+ /// ```
+ /// let x = "(///)".to_string();
+ /// let d: Vec<_> = x.split('/').collect();
+ ///
+ /// assert_eq!(d, &["(", "", "", ")"]);
+ /// ```
+ ///
+ /// Separators at the start or end of a string are neighbored
+ /// by empty strings.
+ ///
+ /// ```
+ /// let d: Vec<_> = "010".split("0").collect();
+ /// assert_eq!(d, &["", "1", ""]);
+ /// ```
+ ///
+ /// When the empty string is used as a separator, it separates
+ /// every character in the string, along with the beginning
+ /// and end of the string.
+ ///
+ /// ```
+ /// let f: Vec<_> = "rust".split("").collect();
+ /// assert_eq!(f, &["", "r", "u", "s", "t", ""]);
+ /// ```
+ ///
+ /// Contiguous separators can lead to possibly surprising behavior
+ /// when whitespace is used as the separator. This code is correct:
+ ///
+ /// ```
+ /// let x = " a b c".to_string();
+ /// let d: Vec<_> = x.split(' ').collect();
+ ///
+ /// assert_eq!(d, &["", "", "", "", "a", "", "b", "c"]);
+ /// ```
+ ///
+ /// It does _not_ give you:
+ ///
+ /// ```,ignore
+ /// assert_eq!(d, &["a", "b", "c"]);
+ /// ```
+ ///
+ /// Use [`split_whitespace`] for this behavior.
+ ///
+ /// [`split_whitespace`]: #method.split_whitespace
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn split<'a, P: Pattern<'a>>(&'a self, pat: P) -> Split<'a, P> {
+ StrExt::split(self, pat)
+ }
+
+ /// An iterator over substrings of the given string slice, separated by
+ /// characters matched by a pattern and yielded in reverse order.
+ ///
+ /// The pattern can be a `&str`, [`char`], or a closure that determines the
+ /// split.
+ ///
+ /// [`char`]: primitive.char.html
+ ///
+ /// # Iterator behavior
+ ///
+ /// The returned iterator requires that the pattern supports a reverse
+ /// search, and it will be a [`DoubleEndedIterator`] if a forward/reverse
+ /// search yields the same elements.
+ ///
+ /// [`DoubleEndedIterator`]: iter/trait.DoubleEndedIterator.html
+ ///
+ /// For iterating from the front, the [`split`] method can be used.
+ ///
+ /// [`split`]: #method.split
+ ///
+ /// # Examples
+ ///
+ /// Simple patterns:
+ ///
+ /// ```
+ /// let v: Vec<&str> = "Mary had a little lamb".rsplit(' ').collect();
+ /// assert_eq!(v, ["lamb", "little", "a", "had", "Mary"]);
+ ///
+ /// let v: Vec<&str> = "".rsplit('X').collect();
+ /// assert_eq!(v, [""]);
+ ///
+ /// let v: Vec<&str> = "lionXXtigerXleopard".rsplit('X').collect();
+ /// assert_eq!(v, ["leopard", "tiger", "", "lion"]);
+ ///
+ /// let v: Vec<&str> = "lion::tiger::leopard".rsplit("::").collect();
+ /// assert_eq!(v, ["leopard", "tiger", "lion"]);
+ /// ```
+ ///
+ /// A more complex pattern, using a closure:
+ ///
+ /// ```
+ /// let v: Vec<&str> = "abc1defXghi".rsplit(|c| c == '1' || c == 'X').collect();
+ /// assert_eq!(v, ["ghi", "def", "abc"]);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn rsplit<'a, P: Pattern<'a>>(&'a self, pat: P) -> RSplit<'a, P>
+ where P::Searcher: ReverseSearcher<'a>
+ {
+ StrExt::rsplit(self, pat)
+ }
+
+ /// An iterator over substrings of the given string slice, separated by
+ /// characters matched by a pattern.
+ ///
+ /// The pattern can be a `&str`, [`char`], or a closure that determines the
+ /// split.
+ ///
+ /// Equivalent to [`split`], except that the trailing substring
+ /// is skipped if empty.
+ ///
+ /// [`split`]: #method.split
+ ///
+ /// This method can be used for string data that is _terminated_,
+ /// rather than _separated_ by a pattern.
+ ///
+ /// # Iterator behavior
+ ///
+ /// The returned iterator will be a [`DoubleEndedIterator`] if the pattern
+ /// allows a reverse search and forward/reverse search yields the same
+ /// elements. This is true for, eg, [`char`] but not for `&str`.
+ ///
+ /// [`DoubleEndedIterator`]: iter/trait.DoubleEndedIterator.html
+ /// [`char`]: primitive.char.html
+ ///
+ /// If the pattern allows a reverse search but its results might differ
+ /// from a forward search, the [`rsplit_terminator`] method can be used.
+ ///
+ /// [`rsplit_terminator`]: #method.rsplit_terminator
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// let v: Vec<&str> = "A.B.".split_terminator('.').collect();
+ /// assert_eq!(v, ["A", "B"]);
+ ///
+ /// let v: Vec<&str> = "A..B..".split_terminator(".").collect();
+ /// assert_eq!(v, ["A", "", "B", ""]);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn split_terminator<'a, P: Pattern<'a>>(&'a self, pat: P) -> SplitTerminator<'a, P> {
+ StrExt::split_terminator(self, pat)
+ }
+
+ /// An iterator over substrings of `self`, separated by characters
+ /// matched by a pattern and yielded in reverse order.
+ ///
+ /// The pattern can be a simple `&str`, [`char`], or a closure that
+ /// determines the split.
+ /// Additional libraries might provide more complex patterns like
+ /// regular expressions.
+ ///
+ /// [`char`]: primitive.char.html
+ ///
+ /// Equivalent to [`split`], except that the trailing substring is
+ /// skipped if empty.
+ ///
+ /// [`split`]: #method.split
+ ///
+ /// This method can be used for string data that is _terminated_,
+ /// rather than _separated_ by a pattern.
+ ///
+ /// # Iterator behavior
+ ///
+ /// The returned iterator requires that the pattern supports a
+ /// reverse search, and it will be double ended if a forward/reverse
+ /// search yields the same elements.
+ ///
+ /// For iterating from the front, the [`split_terminator`] method can be
+ /// used.
+ ///
+ /// [`split_terminator`]: #method.split_terminator
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let v: Vec<&str> = "A.B.".rsplit_terminator('.').collect();
+ /// assert_eq!(v, ["B", "A"]);
+ ///
+ /// let v: Vec<&str> = "A..B..".rsplit_terminator(".").collect();
+ /// assert_eq!(v, ["", "B", "", "A"]);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn rsplit_terminator<'a, P: Pattern<'a>>(&'a self, pat: P) -> RSplitTerminator<'a, P>
+ where P::Searcher: ReverseSearcher<'a>
+ {
+ StrExt::rsplit_terminator(self, pat)
+ }
+
+ /// An iterator over substrings of the given string slice, separated by a
+ /// pattern, restricted to returning at most `n` items.
+ ///
+ /// If `n` substrings are returned, the last substring (the `n`th substring)
+ /// will contain the remainder of the string.
+ ///
+ /// The pattern can be a `&str`, [`char`], or a closure that determines the
+ /// split.
+ ///
+ /// [`char`]: primitive.char.html
+ ///
+ /// # Iterator behavior
+ ///
+ /// The returned iterator will not be double ended, because it is
+ /// not efficient to support.
+ ///
+ /// If the pattern allows a reverse search, the [`rsplitn`] method can be
+ /// used.
+ ///
+ /// [`rsplitn`]: #method.rsplitn
+ ///
+ /// # Examples
+ ///
+ /// Simple patterns:
+ ///
+ /// ```
+ /// let v: Vec<&str> = "Mary had a little lambda".splitn(3, ' ').collect();
+ /// assert_eq!(v, ["Mary", "had", "a little lambda"]);
+ ///
+ /// let v: Vec<&str> = "lionXXtigerXleopard".splitn(3, "X").collect();
+ /// assert_eq!(v, ["lion", "", "tigerXleopard"]);
+ ///
+ /// let v: Vec<&str> = "abcXdef".splitn(1, 'X').collect();
+ /// assert_eq!(v, ["abcXdef"]);
+ ///
+ /// let v: Vec<&str> = "".splitn(1, 'X').collect();
+ /// assert_eq!(v, [""]);
+ /// ```
+ ///
+ /// A more complex pattern, using a closure:
+ ///
+ /// ```
+ /// let v: Vec<&str> = "abc1defXghi".splitn(2, |c| c == '1' || c == 'X').collect();
+ /// assert_eq!(v, ["abc", "defXghi"]);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn splitn<'a, P: Pattern<'a>>(&'a self, n: usize, pat: P) -> SplitN<'a, P> {
+ StrExt::splitn(self, n, pat)
+ }
+
+ /// An iterator over substrings of this string slice, separated by a
+ /// pattern, starting from the end of the string, restricted to returning
+ /// at most `n` items.
+ ///
+ /// If `n` substrings are returned, the last substring (the `n`th substring)
+ /// will contain the remainder of the string.
+ ///
+ /// The pattern can be a `&str`, [`char`], or a closure that
+ /// determines the split.
+ ///
+ /// [`char`]: primitive.char.html
+ ///
+ /// # Iterator behavior
+ ///
+ /// The returned iterator will not be double ended, because it is not
+ /// efficient to support.
+ ///
+ /// For splitting from the front, the [`splitn`] method can be used.
+ ///
+ /// [`splitn`]: #method.splitn
+ ///
+ /// # Examples
+ ///
+ /// Simple patterns:
+ ///
+ /// ```
+ /// let v: Vec<&str> = "Mary had a little lamb".rsplitn(3, ' ').collect();
+ /// assert_eq!(v, ["lamb", "little", "Mary had a"]);
+ ///
+ /// let v: Vec<&str> = "lionXXtigerXleopard".rsplitn(3, 'X').collect();
+ /// assert_eq!(v, ["leopard", "tiger", "lionX"]);
+ ///
+ /// let v: Vec<&str> = "lion::tiger::leopard".rsplitn(2, "::").collect();
+ /// assert_eq!(v, ["leopard", "lion::tiger"]);
+ /// ```
+ ///
+ /// A more complex pattern, using a closure:
+ ///
+ /// ```
+ /// let v: Vec<&str> = "abc1defXghi".rsplitn(2, |c| c == '1' || c == 'X').collect();
+ /// assert_eq!(v, ["ghi", "abc1def"]);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn rsplitn<'a, P: Pattern<'a>>(&'a self, n: usize, pat: P) -> RSplitN<'a, P>
+ where P::Searcher: ReverseSearcher<'a>
+ {
+ StrExt::rsplitn(self, n, pat)
+ }
+
+ /// An iterator over the disjoint matches of a pattern within the given string
+ /// slice.
+ ///
+ /// The pattern can be a `&str`, [`char`], or a closure that
+ /// determines if a character matches.
+ ///
+ /// [`char`]: primitive.char.html
+ ///
+ /// # Iterator behavior
+ ///
+ /// The returned iterator will be a [`DoubleEndedIterator`] if the pattern
+ /// allows a reverse search and forward/reverse search yields the same
+ /// elements. This is true for, eg, [`char`] but not for `&str`.
+ ///
+ /// [`DoubleEndedIterator`]: iter/trait.DoubleEndedIterator.html
+ /// [`char`]: primitive.char.html
+ ///
+ /// If the pattern allows a reverse search but its results might differ
+ /// from a forward search, the [`rmatches`] method can be used.
+ ///
+ /// [`rmatches`]: #method.rmatches
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// let v: Vec<&str> = "abcXXXabcYYYabc".matches("abc").collect();
+ /// assert_eq!(v, ["abc", "abc", "abc"]);
+ ///
+ /// let v: Vec<&str> = "1abc2abc3".matches(char::is_numeric).collect();
+ /// assert_eq!(v, ["1", "2", "3"]);
+ /// ```
+ #[stable(feature = "str_matches", since = "1.2.0")]
+ #[inline]
+ pub fn matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> Matches<'a, P> {
+ StrExt::matches(self, pat)
+ }
+
+ /// An iterator over the disjoint matches of a pattern within this string slice,
+ /// yielded in reverse order.
+ ///
+ /// The pattern can be a `&str`, [`char`], or a closure that determines if
+ /// a character matches.
+ ///
+ /// [`char`]: primitive.char.html
+ ///
+ /// # Iterator behavior
+ ///
+ /// The returned iterator requires that the pattern supports a reverse
+ /// search, and it will be a [`DoubleEndedIterator`] if a forward/reverse
+ /// search yields the same elements.
+ ///
+ /// [`DoubleEndedIterator`]: iter/trait.DoubleEndedIterator.html
+ ///
+ /// For iterating from the front, the [`matches`] method can be used.
+ ///
+ /// [`matches`]: #method.matches
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// let v: Vec<&str> = "abcXXXabcYYYabc".rmatches("abc").collect();
+ /// assert_eq!(v, ["abc", "abc", "abc"]);
+ ///
+ /// let v: Vec<&str> = "1abc2abc3".rmatches(char::is_numeric).collect();
+ /// assert_eq!(v, ["3", "2", "1"]);
+ /// ```
+ #[stable(feature = "str_matches", since = "1.2.0")]
+ #[inline]
+ pub fn rmatches<'a, P: Pattern<'a>>(&'a self, pat: P) -> RMatches<'a, P>
+ where P::Searcher: ReverseSearcher<'a>
+ {
+ StrExt::rmatches(self, pat)
+ }
+
+ /// An iterator over the disjoint matches of a pattern within this string
+ /// slice as well as the index that the match starts at.
+ ///
+ /// For matches of `pat` within `self` that overlap, only the indices
+ /// corresponding to the first match are returned.
+ ///
+ /// The pattern can be a `&str`, [`char`], or a closure that determines
+ /// if a character matches.
+ ///
+ /// [`char`]: primitive.char.html
+ ///
+ /// # Iterator behavior
+ ///
+ /// The returned iterator will be a [`DoubleEndedIterator`] if the pattern
+ /// allows a reverse search and forward/reverse search yields the same
+ /// elements. This is true for, eg, [`char`] but not for `&str`.
+ ///
+ /// [`DoubleEndedIterator`]: iter/trait.DoubleEndedIterator.html
+ ///
+ /// If the pattern allows a reverse search but its results might differ
+ /// from a forward search, the [`rmatch_indices`] method can be used.
+ ///
+ /// [`rmatch_indices`]: #method.rmatch_indices
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// let v: Vec<_> = "abcXXXabcYYYabc".match_indices("abc").collect();
+ /// assert_eq!(v, [(0, "abc"), (6, "abc"), (12, "abc")]);
+ ///
+ /// let v: Vec<_> = "1abcabc2".match_indices("abc").collect();
+ /// assert_eq!(v, [(1, "abc"), (4, "abc")]);
+ ///
+ /// let v: Vec<_> = "ababa".match_indices("aba").collect();
+ /// assert_eq!(v, [(0, "aba")]); // only the first `aba`
+ /// ```
+ #[stable(feature = "str_match_indices", since = "1.5.0")]
+ #[inline]
+ pub fn match_indices<'a, P: Pattern<'a>>(&'a self, pat: P) -> MatchIndices<'a, P> {
+ StrExt::match_indices(self, pat)
+ }
+
+ /// An iterator over the disjoint matches of a pattern within `self`,
+ /// yielded in reverse order along with the index of the match.
+ ///
+ /// For matches of `pat` within `self` that overlap, only the indices
+ /// corresponding to the last match are returned.
+ ///
+ /// The pattern can be a `&str`, [`char`], or a closure that determines if a
+ /// character matches.
+ ///
+ /// [`char`]: primitive.char.html
+ ///
+ /// # Iterator behavior
+ ///
+ /// The returned iterator requires that the pattern supports a reverse
+ /// search, and it will be a [`DoubleEndedIterator`] if a forward/reverse
+ /// search yields the same elements.
+ ///
+ /// [`DoubleEndedIterator`]: iter/trait.DoubleEndedIterator.html
+ ///
+ /// For iterating from the front, the [`match_indices`] method can be used.
+ ///
+ /// [`match_indices`]: #method.match_indices
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// let v: Vec<_> = "abcXXXabcYYYabc".rmatch_indices("abc").collect();
+ /// assert_eq!(v, [(12, "abc"), (6, "abc"), (0, "abc")]);
+ ///
+ /// let v: Vec<_> = "1abcabc2".rmatch_indices("abc").collect();
+ /// assert_eq!(v, [(4, "abc"), (1, "abc")]);
+ ///
+ /// let v: Vec<_> = "ababa".rmatch_indices("aba").collect();
+ /// assert_eq!(v, [(2, "aba")]); // only the last `aba`
+ /// ```
+ #[stable(feature = "str_match_indices", since = "1.5.0")]
+ #[inline]
+ pub fn rmatch_indices<'a, P: Pattern<'a>>(&'a self, pat: P) -> RMatchIndices<'a, P>
+ where P::Searcher: ReverseSearcher<'a>
+ {
+ StrExt::rmatch_indices(self, pat)
+ }
+
+ /// Returns a string slice with leading and trailing whitespace removed.
+ ///
+ /// 'Whitespace' is defined according to the terms of the Unicode Derived
+ /// Core Property `White_Space`.
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// let s = " Hello\tworld\t";
+ ///
+ /// assert_eq!("Hello\tworld", s.trim());
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn trim(&self) -> &str {
+ StrExt::trim(self)
+ }
+
+ /// Returns a string slice with leading whitespace removed.
+ ///
+ /// 'Whitespace' is defined according to the terms of the Unicode Derived
+ /// Core Property `White_Space`.
+ ///
+ /// # Text directionality
+ ///
+ /// A string is a sequence of bytes. 'Left' in this context means the first
+ /// position of that byte string; for a language like Arabic or Hebrew
+ /// which are 'right to left' rather than 'left to right', this will be
+ /// the _right_ side, not the left.
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// let s = " Hello\tworld\t";
+ ///
+ /// assert_eq!("Hello\tworld\t", s.trim_left());
+ /// ```
+ ///
+ /// Directionality:
+ ///
+ /// ```
+ /// let s = " English";
+ /// assert!(Some('E') == s.trim_left().chars().next());
+ ///
+ /// let s = " עברית";
+ /// assert!(Some('ע') == s.trim_left().chars().next());
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn trim_left(&self) -> &str {
+ StrExt::trim_left(self)
+ }
+
+ /// Returns a string slice with trailing whitespace removed.
+ ///
+ /// 'Whitespace' is defined according to the terms of the Unicode Derived
+ /// Core Property `White_Space`.
+ ///
+ /// # Text directionality
+ ///
+ /// A string is a sequence of bytes. 'Right' in this context means the last
+ /// position of that byte string; for a language like Arabic or Hebrew
+ /// which are 'right to left' rather than 'left to right', this will be
+ /// the _left_ side, not the right.
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// let s = " Hello\tworld\t";
+ ///
+ /// assert_eq!(" Hello\tworld", s.trim_right());
+ /// ```
+ ///
+ /// Directionality:
+ ///
+ /// ```
+ /// let s = "English ";
+ /// assert!(Some('h') == s.trim_right().chars().rev().next());
+ ///
+ /// let s = "עברית ";
+ /// assert!(Some('ת') == s.trim_right().chars().rev().next());
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn trim_right(&self) -> &str {
+ StrExt::trim_right(self)
+ }
+
+ /// Returns a string slice with all prefixes and suffixes that match a
+ /// pattern repeatedly removed.
+ ///
+ /// The pattern can be a [`char`] or a closure that determines if a
+ /// character matches.
+ ///
+ /// [`char`]: primitive.char.html
+ ///
+ /// # Examples
+ ///
+ /// Simple patterns:
+ ///
+ /// ```
+ /// assert_eq!("11foo1bar11".trim_matches('1'), "foo1bar");
+ /// assert_eq!("123foo1bar123".trim_matches(char::is_numeric), "foo1bar");
+ ///
+ /// let x: &[_] = &['1', '2'];
+ /// assert_eq!("12foo1bar12".trim_matches(x), "foo1bar");
+ /// ```
+ ///
+ /// A more complex pattern, using a closure:
+ ///
+ /// ```
+ /// assert_eq!("1foo1barXX".trim_matches(|c| c == '1' || c == 'X'), "foo1bar");
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn trim_matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> &'a str
+ where P::Searcher: DoubleEndedSearcher<'a>
+ {
+ StrExt::trim_matches(self, pat)
+ }
+
+ /// Returns a string slice with all prefixes that match a pattern
+ /// repeatedly removed.
+ ///
+ /// The pattern can be a `&str`, [`char`], or a closure that determines if
+ /// a character matches.
+ ///
+ /// [`char`]: primitive.char.html
+ ///
+ /// # Text directionality
+ ///
+ /// A string is a sequence of bytes. 'Left' in this context means the first
+ /// position of that byte string; for a language like Arabic or Hebrew
+ /// which are 'right to left' rather than 'left to right', this will be
+ /// the _right_ side, not the left.
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// assert_eq!("11foo1bar11".trim_left_matches('1'), "foo1bar11");
+ /// assert_eq!("123foo1bar123".trim_left_matches(char::is_numeric), "foo1bar123");
+ ///
+ /// let x: &[_] = &['1', '2'];
+ /// assert_eq!("12foo1bar12".trim_left_matches(x), "foo1bar12");
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn trim_left_matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> &'a str {
+ StrExt::trim_left_matches(self, pat)
+ }
+
+ /// Returns a string slice with all suffixes that match a pattern
+ /// repeatedly removed.
+ ///
+ /// The pattern can be a `&str`, [`char`], or a closure that
+ /// determines if a character matches.
+ ///
+ /// [`char`]: primitive.char.html
+ ///
+ /// # Text directionality
+ ///
+ /// A string is a sequence of bytes. 'Right' in this context means the last
+ /// position of that byte string; for a language like Arabic or Hebrew
+ /// which are 'right to left' rather than 'left to right', this will be
+ /// the _left_ side, not the right.
+ ///
+ /// # Examples
+ ///
+ /// Simple patterns:
+ ///
+ /// ```
+ /// assert_eq!("11foo1bar11".trim_right_matches('1'), "11foo1bar");
+ /// assert_eq!("123foo1bar123".trim_right_matches(char::is_numeric), "123foo1bar");
+ ///
+ /// let x: &[_] = &['1', '2'];
+ /// assert_eq!("12foo1bar12".trim_right_matches(x), "12foo1bar");
+ /// ```
+ ///
+ /// A more complex pattern, using a closure:
+ ///
+ /// ```
+ /// assert_eq!("1fooX".trim_right_matches(|c| c == '1' || c == 'X'), "1foo");
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn trim_right_matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> &'a str
+ where P::Searcher: ReverseSearcher<'a>
+ {
+ StrExt::trim_right_matches(self, pat)
+ }
+
+ /// Parses this string slice into another type.
+ ///
+ /// Because `parse` is so general, it can cause problems with type
+ /// inference. As such, `parse` is one of the few times you'll see
+ /// the syntax affectionately known as the 'turbofish': `::<>`. This
+ /// helps the inference algorithm understand specifically which type
+ /// you're trying to parse into.
+ ///
+ /// `parse` can parse any type that implements the [`FromStr`] trait.
+ ///
+ /// [`FromStr`]: str/trait.FromStr.html
+ ///
+ /// # Errors
+ ///
+ /// Will return [`Err`] if it's not possible to parse this string slice into
+ /// the desired type.
+ ///
+ /// [`Err`]: str/trait.FromStr.html#associatedtype.Err
+ ///
+ /// # Examples
+ ///
+ /// Basic usage
+ ///
+ /// ```
+ /// let four: u32 = "4".parse().unwrap();
+ ///
+ /// assert_eq!(4, four);
+ /// ```
+ ///
+ /// Using the 'turbofish' instead of annotating `four`:
+ ///
+ /// ```
+ /// let four = "4".parse::<u32>();
+ ///
+ /// assert_eq!(Ok(4), four);
+ /// ```
+ ///
+ /// Failing to parse:
+ ///
+ /// ```
+ /// let nope = "j".parse::<u32>();
+ ///
+ /// assert!(nope.is_err());
+ /// ```
+ #[inline]
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn parse<F: FromStr>(&self) -> Result<F, F::Err> {
+ StrExt::parse(self)
+ }
+
+ /// Checks if all characters in this string are within the ASCII range.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let ascii = "hello!\n";
+ /// let non_ascii = "Grüße, Jürgen ❤";
+ ///
+ /// assert!(ascii.is_ascii());
+ /// assert!(!non_ascii.is_ascii());
+ /// ```
+ #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
+ #[inline]
+ pub fn is_ascii(&self) -> bool {
+ // We can treat each byte as character here: all multibyte characters
+ // start with a byte that is not in the ascii range, so we will stop
+ // there already.
+ self.bytes().all(|b| b.is_ascii())
+ }
+
+ /// Checks that two strings are an ASCII case-insensitive match.
+ ///
+ /// Same as `to_ascii_lowercase(a) == to_ascii_lowercase(b)`,
+ /// but without allocating and copying temporaries.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// assert!("Ferris".eq_ignore_ascii_case("FERRIS"));
+ /// assert!("Ferrös".eq_ignore_ascii_case("FERRöS"));
+ /// assert!(!"Ferrös".eq_ignore_ascii_case("FERRÖS"));
+ /// ```
+ #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
+ #[inline]
+ pub fn eq_ignore_ascii_case(&self, other: &str) -> bool {
+ self.as_bytes().eq_ignore_ascii_case(other.as_bytes())
+ }
+
+ /// Converts this string to its ASCII upper case equivalent in-place.
+ ///
+ /// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
+ /// but non-ASCII letters are unchanged.
+ ///
+ /// To return a new uppercased value without modifying the existing one, use
+ /// [`to_ascii_uppercase`].
+ ///
+ /// [`to_ascii_uppercase`]: #method.to_ascii_uppercase
+ #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
+ pub fn make_ascii_uppercase(&mut self) {
+ let me = unsafe { self.as_bytes_mut() };
+ me.make_ascii_uppercase()
+ }
+
+ /// Converts this string to its ASCII lower case equivalent in-place.
+ ///
+ /// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
+ /// but non-ASCII letters are unchanged.
+ ///
+ /// To return a new lowercased value without modifying the existing one, use
+ /// [`to_ascii_lowercase`].
+ ///
+ /// [`to_ascii_lowercase`]: #method.to_ascii_lowercase
+ #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
+ pub fn make_ascii_lowercase(&mut self) {
+ let me = unsafe { self.as_bytes_mut() };
+ me.make_ascii_lowercase()
+ }
+}}
+
+#[lang = "str"]
+#[cfg(not(test))]
+#[cfg(not(stage0))]
+impl str {
+ str_core_methods!();
+}
+
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl AsRef<[u8]> for str {
+ #[inline]
+ fn as_ref(&self) -> &[u8] {
+ self.as_bytes()
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<'a> Default for &'a str {
+ /// Creates an empty str
+ fn default() -> &'a str { "" }
+}
+
+/// An iterator over the non-whitespace substrings of a string,
+/// separated by any amount of whitespace.
+///
+/// This struct is created by the [`split_whitespace`] method on [`str`].
+/// See its documentation for more.
+///
+/// [`split_whitespace`]: ../../std/primitive.str.html#method.split_whitespace
+/// [`str`]: ../../std/primitive.str.html
+#[stable(feature = "split_whitespace", since = "1.1.0")]
+#[derive(Clone, Debug)]
+pub struct SplitWhitespace<'a> {
+ inner: Filter<Split<'a, IsWhitespace>, IsNotEmpty>,
+}
+
+#[derive(Clone)]
+struct IsWhitespace;
+
+impl FnOnce<(char, )> for IsWhitespace {
+ type Output = bool;
+
+ #[inline]
+ extern "rust-call" fn call_once(mut self, arg: (char, )) -> bool {
+ self.call_mut(arg)
+ }
+}
+
+impl FnMut<(char, )> for IsWhitespace {
+ #[inline]
+ extern "rust-call" fn call_mut(&mut self, arg: (char, )) -> bool {
+ arg.0.is_whitespace()
+ }
+}
+
+#[derive(Clone)]
+struct IsNotEmpty;
+
+impl<'a, 'b> FnOnce<(&'a &'b str, )> for IsNotEmpty {
+ type Output = bool;
+
+ #[inline]
+ extern "rust-call" fn call_once(mut self, arg: (&&str, )) -> bool {
+ self.call_mut(arg)
+ }
+}
+
+impl<'a, 'b> FnMut<(&'a &'b str, )> for IsNotEmpty {
+ #[inline]
+ extern "rust-call" fn call_mut(&mut self, arg: (&&str, )) -> bool {
+ !arg.0.is_empty()
+ }
+}
+
+
+#[stable(feature = "split_whitespace", since = "1.1.0")]
+impl<'a> Iterator for SplitWhitespace<'a> {
+ type Item = &'a str;
+
+ fn next(&mut self) -> Option<&'a str> {
+ self.inner.next()
+ }
+}
+
+#[stable(feature = "split_whitespace", since = "1.1.0")]
+impl<'a> DoubleEndedIterator for SplitWhitespace<'a> {
+ fn next_back(&mut self) -> Option<&'a str> {
+ self.inner.next_back()
+ }
+}
+
+#[stable(feature = "fused", since = "1.26.0")]
+impl<'a> FusedIterator for SplitWhitespace<'a> {}
+
+/// An iterator of [`u16`] over the string encoded as UTF-16.
+///
+/// [`u16`]: ../../std/primitive.u16.html
+///
+/// This struct is created by the [`encode_utf16`] method on [`str`].
+/// See its documentation for more.
+///
+/// [`encode_utf16`]: ../../std/primitive.str.html#method.encode_utf16
+/// [`str`]: ../../std/primitive.str.html
+#[derive(Clone)]
+#[stable(feature = "encode_utf16", since = "1.8.0")]
+pub struct EncodeUtf16<'a> {
+ chars: Chars<'a>,
+ extra: u16,
+}
+
+// FIXME: remove (inline) this method
+// when updating to a bootstrap compiler that has the new lang items.
+// For grepping purpose: #[cfg(stage0)]
+impl<'a> EncodeUtf16<'a> {
+ #[unstable(feature = "core_str_ext", issue = "32110")]
+ #[doc(hidden)]
+ pub fn new(s: &'a str) -> Self {
+ EncodeUtf16 { chars: s.chars(), extra: 0 }
+ }
+}
+
+#[stable(feature = "collection_debug", since = "1.17.0")]
+impl<'a> fmt::Debug for EncodeUtf16<'a> {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ f.pad("EncodeUtf16 { .. }")
+ }
+}
+
+#[stable(feature = "encode_utf16", since = "1.8.0")]
+impl<'a> Iterator for EncodeUtf16<'a> {
+ type Item = u16;
+
+ #[inline]
+ fn next(&mut self) -> Option<u16> {
+ if self.extra != 0 {
+ let tmp = self.extra;
+ self.extra = 0;
+ return Some(tmp);
+ }
+
+ let mut buf = [0; 2];
+ self.chars.next().map(|ch| {
+ let n = ch.encode_utf16(&mut buf).len();
+ if n == 2 {
+ self.extra = buf[1];
+ }
+ buf[0]
+ })
+ }
+
+ #[inline]
+ fn size_hint(&self) -> (usize, Option<usize>) {
+ let (low, high) = self.chars.size_hint();
+ // every char gets either one u16 or two u16,
+ // so this iterator is between 1 or 2 times as
+ // long as the underlying iterator.
+ (low, high.and_then(|n| n.checked_mul(2)))
+ }
+}
+
+#[stable(feature = "fused", since = "1.26.0")]
+impl<'a> FusedIterator for EncodeUtf16<'a> {}
#![feature(decode_utf8)]
#![feature(exact_size_is_empty)]
#![feature(fixed_size_array)]
+#![feature(float_internals)]
#![feature(flt2dec)]
#![feature(fmt_internals)]
#![feature(hashmap_internals)]
StrImplItem, "str", str_impl;
SliceImplItem, "slice", slice_impl;
SliceU8ImplItem, "slice_u8", slice_u8_impl;
+ StrAllocImplItem, "str_alloc", str_alloc_impl;
+ SliceAllocImplItem, "slice_alloc", slice_alloc_impl;
+ SliceU8AllocImplItem, "slice_u8_alloc", slice_u8_alloc_impl;
ConstPtrImplItem, "const_ptr", const_ptr_impl;
MutPtrImplItem, "mut_ptr", mut_ptr_impl;
I8ImplItem, "i8", i8_impl;
UsizeImplItem, "usize", usize_impl;
F32ImplItem, "f32", f32_impl;
F64ImplItem, "f64", f64_impl;
+ F32RuntimeImplItem, "f32_runtime", f32_runtime_impl;
+ F64RuntimeImplItem, "f64_runtime", f64_runtime_impl;
SizedTraitLangItem, "sized", sized_trait;
UnsizeTraitLangItem, "unsize", unsize_trait;
ty::TyStr => {
let lang_def_id = lang_items.str_impl();
self.assemble_inherent_impl_for_primitive(lang_def_id);
+
+ let lang_def_id = lang_items.str_alloc_impl();
+ self.assemble_inherent_impl_for_primitive(lang_def_id);
}
ty::TySlice(_) => {
let lang_def_id = lang_items.slice_impl();
let lang_def_id = lang_items.slice_u8_impl();
self.assemble_inherent_impl_for_primitive(lang_def_id);
+
+ let lang_def_id = lang_items.slice_alloc_impl();
+ self.assemble_inherent_impl_for_primitive(lang_def_id);
+
+ let lang_def_id = lang_items.slice_u8_alloc_impl();
+ self.assemble_inherent_impl_for_primitive(lang_def_id);
}
ty::TyRawPtr(ty::TypeAndMut { ty: _, mutbl: hir::MutImmutable }) => {
let lang_def_id = lang_items.const_ptr_impl();
ty::TyFloat(ast::FloatTy::F32) => {
let lang_def_id = lang_items.f32_impl();
self.assemble_inherent_impl_for_primitive(lang_def_id);
+
+ let lang_def_id = lang_items.f32_runtime_impl();
+ self.assemble_inherent_impl_for_primitive(lang_def_id);
}
ty::TyFloat(ast::FloatTy::F64) => {
let lang_def_id = lang_items.f64_impl();
self.assemble_inherent_impl_for_primitive(lang_def_id);
+
+ let lang_def_id = lang_items.f64_runtime_impl();
+ self.assemble_inherent_impl_for_primitive(lang_def_id);
}
_ => {}
}
ty::TyChar => {
self.check_primitive_impl(def_id,
lang_items.char_impl(),
+ None,
"char",
"char",
item.span);
ty::TyStr => {
self.check_primitive_impl(def_id,
lang_items.str_impl(),
+ lang_items.str_alloc_impl(),
"str",
"str",
item.span);
ty::TySlice(slice_item) if slice_item == self.tcx.types.u8 => {
self.check_primitive_impl(def_id,
lang_items.slice_u8_impl(),
+ lang_items.slice_u8_alloc_impl(),
"slice_u8",
"[u8]",
item.span);
ty::TySlice(_) => {
self.check_primitive_impl(def_id,
lang_items.slice_impl(),
+ lang_items.slice_alloc_impl(),
"slice",
"[T]",
item.span);
ty::TyRawPtr(ty::TypeAndMut { ty: _, mutbl: hir::MutImmutable }) => {
self.check_primitive_impl(def_id,
lang_items.const_ptr_impl(),
+ None,
"const_ptr",
"*const T",
item.span);
ty::TyRawPtr(ty::TypeAndMut { ty: _, mutbl: hir::MutMutable }) => {
self.check_primitive_impl(def_id,
lang_items.mut_ptr_impl(),
+ None,
"mut_ptr",
"*mut T",
item.span);
ty::TyInt(ast::IntTy::I8) => {
self.check_primitive_impl(def_id,
lang_items.i8_impl(),
+ None,
"i8",
"i8",
item.span);
ty::TyInt(ast::IntTy::I16) => {
self.check_primitive_impl(def_id,
lang_items.i16_impl(),
+ None,
"i16",
"i16",
item.span);
ty::TyInt(ast::IntTy::I32) => {
self.check_primitive_impl(def_id,
lang_items.i32_impl(),
+ None,
"i32",
"i32",
item.span);
ty::TyInt(ast::IntTy::I64) => {
self.check_primitive_impl(def_id,
lang_items.i64_impl(),
+ None,
"i64",
"i64",
item.span);
ty::TyInt(ast::IntTy::I128) => {
self.check_primitive_impl(def_id,
lang_items.i128_impl(),
+ None,
"i128",
"i128",
item.span);
ty::TyInt(ast::IntTy::Isize) => {
self.check_primitive_impl(def_id,
lang_items.isize_impl(),
+ None,
"isize",
"isize",
item.span);
ty::TyUint(ast::UintTy::U8) => {
self.check_primitive_impl(def_id,
lang_items.u8_impl(),
+ None,
"u8",
"u8",
item.span);
ty::TyUint(ast::UintTy::U16) => {
self.check_primitive_impl(def_id,
lang_items.u16_impl(),
+ None,
"u16",
"u16",
item.span);
ty::TyUint(ast::UintTy::U32) => {
self.check_primitive_impl(def_id,
lang_items.u32_impl(),
+ None,
"u32",
"u32",
item.span);
ty::TyUint(ast::UintTy::U64) => {
self.check_primitive_impl(def_id,
lang_items.u64_impl(),
+ None,
"u64",
"u64",
item.span);
ty::TyUint(ast::UintTy::U128) => {
self.check_primitive_impl(def_id,
lang_items.u128_impl(),
+ None,
"u128",
"u128",
item.span);
ty::TyUint(ast::UintTy::Usize) => {
self.check_primitive_impl(def_id,
lang_items.usize_impl(),
+ None,
"usize",
"usize",
item.span);
ty::TyFloat(ast::FloatTy::F32) => {
self.check_primitive_impl(def_id,
lang_items.f32_impl(),
+ lang_items.f32_runtime_impl(),
"f32",
"f32",
item.span);
ty::TyFloat(ast::FloatTy::F64) => {
self.check_primitive_impl(def_id,
lang_items.f64_impl(),
+ lang_items.f64_runtime_impl(),
"f64",
"f64",
item.span);
fn check_primitive_impl(&self,
impl_def_id: DefId,
lang_def_id: Option<DefId>,
+ lang_def_id2: Option<DefId>,
lang: &str,
ty: &str,
span: Span) {
- match lang_def_id {
- Some(lang_def_id) if lang_def_id == impl_def_id => {
+ match (lang_def_id, lang_def_id2) {
+ (Some(lang_def_id), _) if lang_def_id == impl_def_id => {
+ // OK
+ }
+ (_, Some(lang_def_id)) if lang_def_id == impl_def_id => {
// OK
}
_ => {
The error happens on numeric literals:
```compile_fail,E0689
-2.0.powi(2);
+2.0.recip();
```
and on numeric bindings without an identified concrete type:
```compile_fail,E0689
let x = 2.0;
-x.powi(2); // same error as above
+x.recip(); // same error as above
```
Because of this, you must give the numeric literal or binding a type:
```
-let _ = 2.0_f32.powi(2);
+let _ = 2.0_f32.recip();
let x: f32 = 2.0;
-let _ = x.powi(2);
-let _ = (2.0 as f32).powi(2);
+let _ = x.recip();
+let _ = (2.0 as f32).recip();
```
"##,
lang_items.u128_impl(),
lang_items.f32_impl(),
lang_items.f64_impl(),
+ lang_items.f32_runtime_impl(),
+ lang_items.f64_runtime_impl(),
lang_items.char_impl(),
lang_items.str_impl(),
lang_items.slice_impl(),
lang_items.slice_u8_impl(),
+ lang_items.str_alloc_impl(),
+ lang_items.slice_alloc_impl(),
+ lang_items.slice_u8_alloc_impl(),
lang_items.const_ptr_impl(),
lang_items.mut_ptr_impl(),
];
/// discriminants. JavaScript then is used to decode them into the original value.
/// Consequently, every change to this type should be synchronized to
/// the `itemTypes` mapping table in `static/main.js`.
-#[derive(Copy, PartialEq, Clone)]
+#[derive(Copy, PartialEq, Clone, Debug)]
pub enum ItemType {
Module = 0,
ExternCrate = 1,
autocomplete=\"off\" \
placeholder=\"Click or press ‘S’ to search, ‘?’ for more options…\" \
type=\"search\">\
+ <a id=\"settings-menu\" href=\"{root_path}settings.html\">\
+ <img src=\"{root_path}wheel{suffix}.svg\" width=\"18\" alt=\"Change settings\">\
+ </a>\
</div>\
</form>\
</nav>\
</script>\
<script src=\"{root_path}main{suffix}.js\"></script>\
<script defer src=\"{root_path}search-index.js\"></script>\
+ <script defer src=\"{root_path}aliases.js\"></script>\
</body>\
</html>",
css_extension = if css_file_extension {
themes = themes.iter()
.filter_map(|t| t.file_stem())
.filter_map(|t| t.to_str())
- .map(|t| format!(r#"<link rel="stylesheet" type="text/css" href="{}{}">"#,
+ .map(|t| format!(r#"<link rel="stylesheet" type="text/css" href="{}{}{}.css">"#,
page.root_path,
- t.replace(".css", &format!("{}.css", page.resource_suffix))))
+ t,
+ page.resource_suffix))
.collect::<String>(),
suffix=page.resource_suffix,
)
// yet when its implementation methods are being indexed. Caches such methods
// and their parent id here and indexes them at the end of crate parsing.
orphan_impl_items: Vec<(DefId, clean::Item)>,
+
+ /// Aliases added through `#[doc(alias = "...")]`. Since a few items can have the same alias,
+ /// we need the alias element to have an array of items.
+ aliases: FxHashMap<String, Vec<IndexItem>>,
}
/// Temporary storage for data obtained during `RustdocVisitor::clean()`.
/// Struct representing one entry in the JS search index. These are all emitted
/// by hand to a large JS file at the end of cache-creation.
+#[derive(Debug)]
struct IndexItem {
ty: ItemType,
name: String,
}
/// A type used for the search index.
+#[derive(Debug)]
struct Type {
name: Option<String>,
generics: Option<Vec<String>>,
}
/// Full type of functions/methods in the search index.
+#[derive(Debug)]
struct IndexItemFunctionType {
inputs: Vec<Type>,
- output: Option<Type>
+ output: Option<Type>,
}
impl ToJson for IndexItemFunctionType {
owned_box_did,
masked_crates: mem::replace(&mut krate.masked_crates, FxHashSet()),
typarams: external_typarams,
+ aliases: FxHashMap(),
};
// Cache where all our extern crates are located
write(cx.dst.join(&format!("rustdoc{}.css", cx.shared.resource_suffix)),
include_bytes!("static/rustdoc.css"))?;
+ write(cx.dst.join(&format!("settings{}.css", cx.shared.resource_suffix)),
+ include_bytes!("static/settings.css"))?;
// To avoid "light.css" to be overwritten, we'll first run over the received themes and only
// then we'll run over the "official" styles.
write(cx.dst.join(&format!("brush{}.svg", cx.shared.resource_suffix)),
include_bytes!("static/brush.svg"))?;
+ write(cx.dst.join(&format!("wheel{}.svg", cx.shared.resource_suffix)),
+ include_bytes!("static/wheel.svg"))?;
write(cx.dst.join(&format!("light{}.css", cx.shared.resource_suffix)),
include_bytes!("static/themes/light.css"))?;
themes.insert("light".to_owned());
switchTheme(currentTheme, mainTheme, item);
}};
themes.appendChild(but);
-}});
-"#,
+}});"#,
themes.iter()
.map(|s| format!("\"{}\"", s))
.collect::<Vec<String>>()
write(cx.dst.join(&format!("main{}.js", cx.shared.resource_suffix)),
include_bytes!("static/main.js"))?;
+ write(cx.dst.join(&format!("settings{}.js", cx.shared.resource_suffix)),
+ include_bytes!("static/settings.js"))?;
{
let mut data = format!("var resourcesSuffix = \"{}\";\n",
write(cx.dst.join("COPYRIGHT.txt"),
include_bytes!("static/COPYRIGHT.txt"))?;
- fn collect(path: &Path, krate: &str,
- key: &str) -> io::Result<Vec<String>> {
+ fn collect(path: &Path, krate: &str, key: &str) -> io::Result<Vec<String>> {
let mut ret = Vec::new();
if path.exists() {
for line in BufReader::new(File::open(path)?).lines() {
Ok(ret)
}
+ fn show_item(item: &IndexItem, krate: &str) -> String {
+ format!("{{'crate':'{}','ty':{},'name':'{}','path':'{}'{}}}",
+ krate, item.ty as usize, item.name, item.path,
+ if let Some(p) = item.parent_idx {
+ format!(",'parent':{}", p)
+ } else {
+ String::new()
+ })
+ }
+
+ let dst = cx.dst.join("aliases.js");
+ {
+ let mut all_aliases = try_err!(collect(&dst, &krate.name, "ALIASES"), &dst);
+ let mut w = try_err!(File::create(&dst), &dst);
+ let mut output = String::with_capacity(100);
+ for (alias, items) in &cache.aliases {
+ if items.is_empty() {
+ continue
+ }
+ output.push_str(&format!("\"{}\":[{}],",
+ alias,
+ items.iter()
+ .map(|v| show_item(v, &krate.name))
+ .collect::<Vec<_>>()
+ .join(",")));
+ }
+ all_aliases.push(format!("ALIASES['{}'] = {{{}}};", krate.name, output));
+ all_aliases.sort();
+ try_err!(writeln!(&mut w, "var ALIASES = {{}};"), &dst);
+ for aliases in &all_aliases {
+ try_err!(writeln!(&mut w, "{}", aliases), &dst);
+ }
+ }
+
// Update the search index
let dst = cx.dst.join("search-index.js");
let mut all_indexes = try_err!(collect(&dst, &krate.name, "searchIndex"), &dst);
// `public_items` map, so we can skip inserting into the
// paths map if there was already an entry present and we're
// not a public item.
- if
- !self.paths.contains_key(&item.def_id) ||
- self.access_levels.is_public(item.def_id)
+ if !self.paths.contains_key(&item.def_id) ||
+ self.access_levels.is_public(item.def_id)
{
self.paths.insert(item.def_id,
(self.stack.clone(), item.type_()));
}
+ self.add_aliases(&item);
}
// Link variants to their parent enum because pages aren't emitted
// for each variant.
}
clean::PrimitiveItem(..) if item.visibility.is_some() => {
+ self.add_aliases(&item);
self.paths.insert(item.def_id, (self.stack.clone(),
item.type_()));
}
}
}
}
+
+ fn add_aliases(&mut self, item: &clean::Item) {
+ if item.def_id.index == CRATE_DEF_INDEX {
+ return
+ }
+ if let Some(ref item_name) = item.name {
+ let path = self.paths.get(&item.def_id)
+ .map(|p| p.0.join("::").to_string())
+ .unwrap_or("std".to_owned());
+ for alias in item.attrs.lists("doc")
+ .filter(|a| a.check_name("alias"))
+ .filter_map(|a| a.value_str()
+ .map(|s| s.to_string().replace("\"", "")))
+ .filter(|v| !v.is_empty())
+ .collect::<FxHashSet<_>>()
+ .into_iter() {
+ self.aliases.entry(alias)
+ .or_insert(Vec::with_capacity(1))
+ .push(IndexItem {
+ ty: item.type_(),
+ name: item_name.to_string(),
+ path: path.clone(),
+ desc: String::new(),
+ parent: None,
+ parent_idx: None,
+ search_type: get_index_search_type(&item),
+ });
+ }
+ }
+ }
}
#[derive(Debug, Eq, PartialEq, Hash)]
}
}
+#[derive(Debug)]
+struct Settings<'a> {
+ // (id, explanation, default value)
+ settings: Vec<(&'static str, &'static str, bool)>,
+ root_path: &'a str,
+ suffix: &'a str,
+}
+
+impl<'a> Settings<'a> {
+ pub fn new(root_path: &'a str, suffix: &'a str) -> Settings<'a> {
+ Settings {
+ settings: vec![
+ ("item-declarations", "Auto-hide item declarations.", true),
+ ("item-attributes", "Auto-hide item attributes.", true),
+ ],
+ root_path,
+ suffix,
+ }
+ }
+}
+
+impl<'a> fmt::Display for Settings<'a> {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ write!(f,
+"<h1 class='fqn'>\
+ <span class='in-band'>Rustdoc settings</span>\
+</h1>\
+<div class='settings'>{}</div>\
+<script src='{}settings{}.js'></script>",
+ self.settings.iter()
+ .map(|(id, text, enabled)| {
+ format!("<div class='setting-line'>\
+ <label class='toggle'>\
+ <input type='checkbox' id='{}' {}>\
+ <span class='slider'></span>\
+ </label>\
+ <div>{}</div>\
+ </div>", id, if *enabled { " checked" } else { "" }, text)
+ })
+ .collect::<String>(),
+ self.root_path,
+ self.suffix)
+ }
+}
+
impl Context {
/// String representation of how to get back to the root path of the 'doc/'
/// folder in terms of a relative URL.
};
let final_file = self.dst.join(&krate.name)
.join("all.html");
+ let settings_file = self.dst.join("settings.html");
+
let crate_name = krate.name.clone();
item.name = Some(krate.name);
if !root_path.ends_with('/') {
root_path.push('/');
}
- let page = layout::Page {
+ let mut page = layout::Page {
title: "List of all items in this crate",
css_class: "mod",
root_path: "../",
self.shared.css_file_extension.is_some(),
&self.shared.themes),
&final_file);
+
+ // Generating settings page.
+ let settings = Settings::new("./", &self.shared.resource_suffix);
+ page.title = "Rustdoc settings";
+ page.description = "Settings of Rustdoc";
+ page.root_path = "./";
+
+ let mut w = BufWriter::new(try_err!(File::create(&settings_file), &settings_file));
+ let mut themes = self.shared.themes.clone();
+ let sidebar = "<p class='location'>Settings</p><div class='sidebar-elems'></div>";
+ themes.push(PathBuf::from("settings.css"));
+ let mut layout = self.shared.layout.clone();
+ layout.krate = String::new();
+ layout.logo = String::new();
+ layout.favicon = String::new();
+ try_err!(layout::render(&mut w, &layout,
+ &page, &sidebar, &settings,
+ self.shared.css_file_extension.is_some(),
+ &themes),
+ &settings_file);
+
Ok(())
}
return false;
}
var end = start + className.length;
- if (end < elemClass.length && elemClass[end] !== ' ') {
- return false;
- }
- return true;
+ return !(end < elemClass.length && elemClass[end] !== ' ');
}
if (start > 0 && elemClass[start - 1] !== ' ') {
return false;
}
var end = start + className.length;
- if (end < elemClass.length && elemClass[end] !== ' ') {
- return false;
- }
- return true;
+ return !(end < elemClass.length && elemClass[end] !== ' ');
}
return false;
}
} else if (ev.target.tagName === 'SPAN' && hasClass(ev.target.parentNode, 'line-numbers')) {
var prev_id = 0;
- var set_fragment = function (name) {
+ var set_fragment = function(name) {
if (browserSupportsHistoryApi()) {
history.replaceState(null, null, '#' + name);
window.hashchange();
query.search = val;
// searching by type
} else if (val.search("->") > -1) {
- var trimmer = function (s) { return s.trim(); };
+ var trimmer = function(s) { return s.trim(); };
var parts = val.split("->").map(trimmer);
var input = parts[0];
// sort inputs so that order does not matter
}
}
- return {
+ var ret = {
'in_args': sortResults(results_in_args, true),
'returned': sortResults(results_returned, true),
'others': sortResults(results),
};
+ if (ALIASES[window.currentCrate][query.raw]) {
+ var aliases = ALIASES[window.currentCrate][query.raw];
+ for (var i = 0; i < aliases.length; ++i) {
+ ret['others'].unshift(aliases[i]);
+ if (ret['others'].length > MAX_RESULTS) {
+ ret['others'].pop();
+ }
+ }
+ }
+ return ret;
}
/**
array.forEach(function(item) {
var name, type, href, displayPath;
- if (shown.indexOf(item) !== -1) {
+ var id_ty = item.ty + item.path + item.name;
+ if (shown.indexOf(id_ty) !== -1) {
return;
}
- shown.push(item);
+ console.log(item);
+ shown.push(id_ty);
name = item.name;
type = itemTypes[item.ty];
function search(e) {
var params = getQueryStringParams();
- var query = getQuery(document.getElementsByClassName('search-input')[0].value);
+ var query = getQuery(document.getElementsByClassName('search-input')[0].value.trim());
if (e) {
e.preventDefault();
startSearch();
// Draw a convenient sidebar of known crates if we have a listing
- if (rootPath === '../') {
+ if (rootPath === '../' || rootPath === "./") {
var sidebar = document.getElementsByClassName('sidebar-elems')[0];
if (sidebar) {
var div = document.createElement('div');
crates.sort();
for (var i = 0; i < crates.length; ++i) {
var klass = 'crate';
- if (crates[i] === window.currentCrate) {
+ if (rootPath !== "./" && crates[i] === window.currentCrate) {
klass += ' current';
}
var link = document.createElement('a');
- link.href = '../' + crates[i] + '/index.html';
+ link.href = rootPath + crates[i] + '/index.html';
link.title = rawSearchIndex[crates[i]].doc;
link.className = klass;
link.textContent = crates[i];
otherMessage = ' Show type declaration';
}
e.parentNode.insertBefore(createToggle(otherMessage), e);
- if (otherMessage) {
+ if (otherMessage && getCurrentValue('rustdoc-item-declarations') !== "false") {
collapseDocs(e.previousSibling.childNodes[0], "toggle");
}
}
onEach(document.getElementById('main').getElementsByTagName('pre'), function(e) {
onEach(e.getElementsByClassName('attributes'), function(i_e) {
i_e.parentNode.insertBefore(createToggleWrapper(), i_e);
- collapseDocs(i_e.previousSibling.childNodes[0], "toggle");
+ if (getCurrentValue("rustdoc-item-attributes") !== "false") {
+ collapseDocs(i_e.previousSibling.childNodes[0], "toggle");
+ }
});
});
.block a.current.crate { font-weight: 500; }
+.search-container {
+ position: relative;
+}
+.search-container > .top-button {
+ position: absolute;
+ right: 0;
+ top: 10px;
+}
.search-input {
- width: 100%;
+ width: calc(100% - 34px);
/* Override Normalize.css: we have margins and do
not want to overflow - the `moz` attribute is necessary
until Firefox 29, too early to drop at this point */
outline: none;
}
-#theme-picker {
+#settings-menu {
+ position: absolute;
+ right: 0;
+ top: 10px;
+ outline: none;
+}
+
+#theme-picker, #settings-menu {
padding: 4px;
width: 27px;
height: 29px;
--- /dev/null
+/**
+ * Copyright 2018 The Rust Project Developers. See the COPYRIGHT
+ * file at the top-level directory of this distribution and at
+ * http://rust-lang.org/COPYRIGHT.
+ *
+ * Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+ * http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+ * <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+ * option. This file may not be copied, modified, or distributed
+ * except according to those terms.
+ */
+
+.setting-line {
+ padding: 5px;
+}
+
+.setting-line > div {
+ max-width: calc(100% - 74px);
+ display: inline-block;
+ vertical-align: top;
+ font-size: 17px;
+ padding-top: 2px;
+}
+
+.toggle {
+ position: relative;
+ display: inline-block;
+ width: 45px;
+ height: 27px;
+ margin-right: 20px;
+}
+
+.toggle input {
+ display: none;
+}
+
+.slider {
+ position: absolute;
+ cursor: pointer;
+ top: 0;
+ left: 0;
+ right: 0;
+ bottom: 0;
+ background-color: #ccc;
+ -webkit-transition: .3s;
+ transition: .3s;
+}
+
+.slider:before {
+ position: absolute;
+ content: "";
+ height: 19px;
+ width: 19px;
+ left: 4px;
+ bottom: 4px;
+ background-color: white;
+ -webkit-transition: .3s;
+ transition: .3s;
+}
+
+input:checked + .slider {
+ background-color: #2196F3;
+}
+
+input:focus + .slider {
+ box-shadow: 0 0 1px #2196F3;
+}
+
+input:checked + .slider:before {
+ -webkit-transform: translateX(19px);
+ -ms-transform: translateX(19px);
+ transform: translateX(19px);
+}
\ No newline at end of file
--- /dev/null
+/*!
+ * Copyright 2018 The Rust Project Developers. See the COPYRIGHT
+ * file at the top-level directory of this distribution and at
+ * http://rust-lang.org/COPYRIGHT.
+ *
+ * Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+ * http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+ * <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+ * option. This file may not be copied, modified, or distributed
+ * except according to those terms.
+ */
+
+(function () {
+ function changeSetting(settingName, isEnabled) {
+ updateLocalStorage('rustdoc-' + settingName, isEnabled);
+ }
+
+ function getSettingValue(settingName) {
+ return getCurrentValue('rustdoc-' + settingName);
+ }
+
+ function setEvents() {
+ var elems = document.getElementsByClassName("slider");
+ if (!elems || elems.length === 0) {
+ return;
+ }
+ for (var i = 0; i < elems.length; ++i) {
+ var toggle = elems[i].previousElementSibling;
+ var settingId = toggle.id;
+ var settingValue = getSettingValue(settingId);
+ if (settingValue !== null) {
+ toggle.checked = settingValue === "true";
+ }
+ toggle.onchange = function() {
+ changeSetting(this.id, this.checked);
+ };
+ }
+ }
+
+ setEvents();
+})();
box-shadow-color: #c6cbd1;
}
-#theme-picker {
+#theme-picker, #settings-menu {
border-color: #e0e0e0;
background: #f0f0f0;
}
-#theme-picker:hover, #theme-picker:focus {
+#theme-picker:hover, #theme-picker:focus,
+#settings-menu:hover, #settings-menu:focus {
border-color: #ffb900;
}
box-shadow-color: #c6cbd1;
}
-#theme-picker {
+#theme-picker, #settings-menu {
border-color: #e0e0e0;
background-color: #fff;
}
-#theme-picker:hover, #theme-picker:focus {
+#theme-picker:hover, #theme-picker:focus,
+#settings-menu:hover, #settings-menu:focus {
border-color: #717171;
}
--- /dev/null
+<?xml version="1.0" ?><!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.1//EN' 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'><svg enable-background="new 0 0 27.434 29.5" height="29.5px" id="Capa_1" version="1.1" viewBox="0 0 27.434 29.5" width="27.434px" xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><g><path d="M27.315,18.389c-0.165-0.604-0.509-1.113-0.981-1.459c-0.042-0.144-0.083-0.429-0.015-0.761l0.037-0.177v-0.182V14.8 c0-1.247-0.006-1.277-0.048-1.472c-0.076-0.354-0.035-0.653,0.007-0.803c0.477-0.346,0.828-0.861,0.996-1.476 c0.261-0.956,0.076-2.091-0.508-3.114l-0.591-1.032c-0.746-1.307-1.965-2.119-3.182-2.119c-0.378,0-0.75,0.081-1.085,0.235 c-0.198-0.025-0.554-0.15-0.855-0.389l-0.103-0.082l-0.114-0.065l-1.857-1.067L18.92,3.36l-0.105-0.044 c-0.376-0.154-0.658-0.41-0.768-0.556C17.918,1.172,16.349,0,14.296,0H13.14c-2.043,0-3.608,1.154-3.749,2.721 C9.277,2.862,8.999,3.104,8.633,3.25l-0.1,0.039L8.439,3.341L6.495,4.406L6.363,4.479L6.245,4.573 C5.936,4.82,5.596,4.944,5.416,4.977c-0.314-0.139-0.66-0.21-1.011-0.21c-1.198,0-2.411,0.819-3.165,2.139L0.65,7.938 c-0.412,0.72-0.642,1.521-0.644,2.258c-0.003,0.952,0.362,1.756,1.013,2.256c0.034,0.155,0.061,0.448-0.016,0.786 c-0.038,0.168-0.062,0.28-0.062,1.563c0,1.148,0,1.148,0.015,1.262l0.009,0.073l0.017,0.073c0.073,0.346,0.045,0.643,0.011,0.802 C0.348,17.512-0.01,18.314,0,19.268c0.008,0.729,0.238,1.523,0.648,2.242l0.589,1.031c0.761,1.331,1.967,2.159,3.15,2.159 c0.324,0,0.645-0.064,0.938-0.187c0.167,0.038,0.492,0.156,0.813,0.416l0.11,0.088l0.124,0.07l2.045,1.156l0.102,0.057l0.107,0.043 c0.364,0.147,0.646,0.381,0.766,0.521c0.164,1.52,1.719,2.634,3.745,2.634h1.155c2.037,0,3.598-1.134,3.747-2.675 c0.117-0.145,0.401-0.393,0.774-0.549l0.111-0.047l0.105-0.062l1.96-1.159l0.105-0.062l0.097-0.075 c0.309-0.246,0.651-0.371,0.832-0.402c0.313,0.138,0.662,0.212,1.016,0.212c1.199,0,2.412-0.82,3.166-2.139l0.59-1.032 C27.387,20.48,27.575,19.342,27.315,18.389z M25.274,20.635l-0.59,1.032c-0.438,0.765-1.104,1.251-1.639,1.251 c-0.133,0-0.258-0.029-0.369-0.094c-0.15-0.086-0.346-0.127-0.566-0.127c-0.596,0-1.383,0.295-2.01,0.796l-1.96,1.157 c-1.016,0.425-1.846,1.291-1.846,1.929s-0.898,1.159-1.998,1.159H13.14c-1.1,0-1.998-0.514-1.998-1.141s-0.834-1.477-1.854-1.888 l-2.046-1.157c-0.636-0.511-1.425-0.814-2.006-0.814c-0.202,0-0.379,0.037-0.516,0.115c-0.101,0.057-0.214,0.084-0.333,0.084 c-0.518,0-1.179-0.498-1.62-1.271l-0.591-1.032c-0.545-0.954-0.556-1.983-0.024-2.286c0.532-0.305,0.78-1.432,0.551-2.506 c0,0,0-0.003,0-1.042c0-1.088,0.021-1.18,0.021-1.18c0.238-1.072-0.01-2.203-0.552-2.513C1.631,10.8,1.634,9.765,2.18,8.812 L2.769,7.78c0.438-0.766,1.103-1.251,1.636-1.251c0.131,0,0.255,0.029,0.365,0.092C4.92,6.707,5.114,6.747,5.334,6.747 c0.596,0,1.38-0.296,2.007-0.795l1.944-1.065c1.021-0.407,1.856-1.277,1.856-1.933c0-0.656,0.898-1.192,1.998-1.192h1.156V1.761 c1.1,0,1.998,0.545,1.998,1.211c0,0.667,0.832,1.554,1.849,1.973L20,6.013c0.618,0.489,1.401,0.775,2.012,0.775 c0.24,0,0.454-0.045,0.62-0.139c0.122-0.069,0.259-0.102,0.403-0.102c0.551,0,1.221,0.476,1.653,1.231l0.59,1.032 c0.544,0.953,0.518,2.004-0.062,2.334c-0.577,0.331-0.859,1.48-0.627,2.554c0,0,0.01,0.042,0.01,1.103c0,1.012,0,1.012,0,1.012 c-0.218,1.049,0.068,2.174,0.636,2.498C25.802,18.635,25.819,19.68,25.274,20.635z"/><path d="M13.61,7.611c-3.913,0-7.084,3.173-7.084,7.085c0,3.914,3.171,7.085,7.084,7.085s7.085-3.172,7.085-7.085 C20.695,10.784,17.523,7.611,13.61,7.611z M13.61,20.02c-2.936,0-5.323-2.388-5.323-5.323c0-2.935,2.388-5.323,5.323-5.323 s5.324,2.388,5.324,5.323C18.934,17.632,16.546,20.02,13.61,20.02z"/><path d="M13.682,9.908c-2.602,0-4.718,2.116-4.718,4.718c0,2.601,2.116,4.716,4.718,4.716c2.601,0,4.717-2.115,4.717-4.716 C18.399,12.024,16.283,9.908,13.682,9.908z M13.682,17.581c-1.633,0-2.956-1.323-2.956-2.955s1.323-2.956,2.956-2.956 c1.632,0,2.956,1.324,2.956,2.956S15.314,17.581,13.682,17.581z"/></g></svg>
\ No newline at end of file
#![allow(missing_docs)]
#[cfg(not(test))]
-use core::num;
+#[cfg(stage0)]
+use core::num::Float;
#[cfg(not(test))]
use intrinsics;
#[cfg(not(test))]
+#[cfg(stage0)]
use num::FpCategory;
#[cfg(not(test))]
use sys::cmath;
pub use core::f32::consts;
#[cfg(not(test))]
-#[lang = "f32"]
+#[cfg_attr(stage0, lang = "f32")]
+#[cfg_attr(not(stage0), lang = "f32_runtime")]
impl f32 {
- /// Returns `true` if this value is `NaN` and false otherwise.
- ///
- /// ```
- /// use std::f32;
- ///
- /// let nan = f32::NAN;
- /// let f = 7.0_f32;
- ///
- /// assert!(nan.is_nan());
- /// assert!(!f.is_nan());
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn is_nan(self) -> bool { num::Float::is_nan(self) }
-
- /// Returns `true` if this value is positive infinity or negative infinity and
- /// false otherwise.
- ///
- /// ```
- /// use std::f32;
- ///
- /// let f = 7.0f32;
- /// let inf = f32::INFINITY;
- /// let neg_inf = f32::NEG_INFINITY;
- /// let nan = f32::NAN;
- ///
- /// assert!(!f.is_infinite());
- /// assert!(!nan.is_infinite());
- ///
- /// assert!(inf.is_infinite());
- /// assert!(neg_inf.is_infinite());
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn is_infinite(self) -> bool { num::Float::is_infinite(self) }
-
- /// Returns `true` if this number is neither infinite nor `NaN`.
- ///
- /// ```
- /// use std::f32;
- ///
- /// let f = 7.0f32;
- /// let inf = f32::INFINITY;
- /// let neg_inf = f32::NEG_INFINITY;
- /// let nan = f32::NAN;
- ///
- /// assert!(f.is_finite());
- ///
- /// assert!(!nan.is_finite());
- /// assert!(!inf.is_finite());
- /// assert!(!neg_inf.is_finite());
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn is_finite(self) -> bool { num::Float::is_finite(self) }
-
- /// Returns `true` if the number is neither zero, infinite,
- /// [subnormal][subnormal], or `NaN`.
- ///
- /// ```
- /// use std::f32;
- ///
- /// let min = f32::MIN_POSITIVE; // 1.17549435e-38f32
- /// let max = f32::MAX;
- /// let lower_than_min = 1.0e-40_f32;
- /// let zero = 0.0_f32;
- ///
- /// assert!(min.is_normal());
- /// assert!(max.is_normal());
- ///
- /// assert!(!zero.is_normal());
- /// assert!(!f32::NAN.is_normal());
- /// assert!(!f32::INFINITY.is_normal());
- /// // Values between `0` and `min` are Subnormal.
- /// assert!(!lower_than_min.is_normal());
- /// ```
- /// [subnormal]: https://en.wikipedia.org/wiki/Denormal_number
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn is_normal(self) -> bool { num::Float::is_normal(self) }
-
- /// Returns the floating point category of the number. If only one property
- /// is going to be tested, it is generally faster to use the specific
- /// predicate instead.
- ///
- /// ```
- /// use std::num::FpCategory;
- /// use std::f32;
- ///
- /// let num = 12.4_f32;
- /// let inf = f32::INFINITY;
- ///
- /// assert_eq!(num.classify(), FpCategory::Normal);
- /// assert_eq!(inf.classify(), FpCategory::Infinite);
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn classify(self) -> FpCategory { num::Float::classify(self) }
+ #[cfg(stage0)]
+ f32_core_methods!();
/// Returns the largest integer less than or equal to a number.
///
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
- pub fn abs(self) -> f32 { num::Float::abs(self) }
+ pub fn abs(self) -> f32 {
+ unsafe { intrinsics::fabsf32(self) }
+ }
/// Returns a number that represents the sign of `self`.
///
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
- pub fn signum(self) -> f32 { num::Float::signum(self) }
-
- /// Returns `true` if and only if `self` has a positive sign, including `+0.0`, `NaN`s with
- /// positive sign bit and positive infinity.
- ///
- /// ```
- /// let f = 7.0_f32;
- /// let g = -7.0_f32;
- ///
- /// assert!(f.is_sign_positive());
- /// assert!(!g.is_sign_positive());
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn is_sign_positive(self) -> bool { num::Float::is_sign_positive(self) }
-
- /// Returns `true` if and only if `self` has a negative sign, including `-0.0`, `NaN`s with
- /// negative sign bit and negative infinity.
- ///
- /// ```
- /// let f = 7.0f32;
- /// let g = -7.0f32;
- ///
- /// assert!(!f.is_sign_negative());
- /// assert!(g.is_sign_negative());
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn is_sign_negative(self) -> bool { num::Float::is_sign_negative(self) }
+ pub fn signum(self) -> f32 {
+ if self.is_nan() {
+ NAN
+ } else {
+ unsafe { intrinsics::copysignf32(1.0, self) }
+ }
+ }
/// Fused multiply-add. Computes `(self * a) + b` with only one rounding
/// error. This produces a more accurate result with better performance than
}
- /// Takes the reciprocal (inverse) of a number, `1/x`.
- ///
- /// ```
- /// use std::f32;
- ///
- /// let x = 2.0_f32;
- /// let abs_difference = (x.recip() - (1.0/x)).abs();
- ///
- /// assert!(abs_difference <= f32::EPSILON);
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn recip(self) -> f32 { num::Float::recip(self) }
-
/// Raises a number to an integer power.
///
/// Using this function is generally faster than using `powf`
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
- pub fn powi(self, n: i32) -> f32 { num::Float::powi(self, n) }
+ pub fn powi(self, n: i32) -> f32 {
+ unsafe { intrinsics::powif32(self, n) }
+ }
/// Raises a number to a floating point power.
///
return unsafe { intrinsics::log10f32(self) };
}
- /// Converts radians to degrees.
- ///
- /// ```
- /// use std::f32::{self, consts};
- ///
- /// let angle = consts::PI;
- ///
- /// let abs_difference = (angle.to_degrees() - 180.0).abs();
- ///
- /// assert!(abs_difference <= f32::EPSILON);
- /// ```
- #[stable(feature = "f32_deg_rad_conversions", since="1.7.0")]
- #[inline]
- pub fn to_degrees(self) -> f32 { num::Float::to_degrees(self) }
-
- /// Converts degrees to radians.
- ///
- /// ```
- /// use std::f32::{self, consts};
- ///
- /// let angle = 180.0f32;
- ///
- /// let abs_difference = (angle.to_radians() - consts::PI).abs();
- ///
- /// assert!(abs_difference <= f32::EPSILON);
- /// ```
- #[stable(feature = "f32_deg_rad_conversions", since="1.7.0")]
- #[inline]
- pub fn to_radians(self) -> f32 { num::Float::to_radians(self) }
-
- /// Returns the maximum of the two numbers.
- ///
- /// ```
- /// let x = 1.0f32;
- /// let y = 2.0f32;
- ///
- /// assert_eq!(x.max(y), y);
- /// ```
- ///
- /// If one of the arguments is NaN, then the other argument is returned.
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn max(self, other: f32) -> f32 {
- num::Float::max(self, other)
- }
-
- /// Returns the minimum of the two numbers.
- ///
- /// ```
- /// let x = 1.0f32;
- /// let y = 2.0f32;
- ///
- /// assert_eq!(x.min(y), x);
- /// ```
- ///
- /// If one of the arguments is NaN, then the other argument is returned.
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn min(self, other: f32) -> f32 {
- num::Float::min(self, other)
- }
-
/// The positive difference of two numbers.
///
/// * If `self <= other`: `0:0`
pub fn atanh(self) -> f32 {
0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
}
-
- /// Raw transmutation to `u32`.
- ///
- /// This is currently identical to `transmute::<f32, u32>(self)` on all platforms.
- ///
- /// See `from_bits` for some discussion of the portability of this operation
- /// (there are almost no issues).
- ///
- /// Note that this function is distinct from `as` casting, which attempts to
- /// preserve the *numeric* value, and not the bitwise value.
- ///
- /// # Examples
- ///
- /// ```
- /// assert_ne!((1f32).to_bits(), 1f32 as u32); // to_bits() is not casting!
- /// assert_eq!((12.5f32).to_bits(), 0x41480000);
- ///
- /// ```
- #[stable(feature = "float_bits_conv", since = "1.20.0")]
- #[inline]
- pub fn to_bits(self) -> u32 {
- num::Float::to_bits(self)
- }
-
- /// Raw transmutation from `u32`.
- ///
- /// This is currently identical to `transmute::<u32, f32>(v)` on all platforms.
- /// It turns out this is incredibly portable, for two reasons:
- ///
- /// * Floats and Ints have the same endianness on all supported platforms.
- /// * IEEE-754 very precisely specifies the bit layout of floats.
- ///
- /// However there is one caveat: prior to the 2008 version of IEEE-754, how
- /// to interpret the NaN signaling bit wasn't actually specified. Most platforms
- /// (notably x86 and ARM) picked the interpretation that was ultimately
- /// standardized in 2008, but some didn't (notably MIPS). As a result, all
- /// signaling NaNs on MIPS are quiet NaNs on x86, and vice-versa.
- ///
- /// Rather than trying to preserve signaling-ness cross-platform, this
- /// implementation favours preserving the exact bits. This means that
- /// any payloads encoded in NaNs will be preserved even if the result of
- /// this method is sent over the network from an x86 machine to a MIPS one.
- ///
- /// If the results of this method are only manipulated by the same
- /// architecture that produced them, then there is no portability concern.
- ///
- /// If the input isn't NaN, then there is no portability concern.
- ///
- /// If you don't care about signalingness (very likely), then there is no
- /// portability concern.
- ///
- /// Note that this function is distinct from `as` casting, which attempts to
- /// preserve the *numeric* value, and not the bitwise value.
- ///
- /// # Examples
- ///
- /// ```
- /// use std::f32;
- /// let v = f32::from_bits(0x41480000);
- /// let difference = (v - 12.5).abs();
- /// assert!(difference <= 1e-5);
- /// ```
- #[stable(feature = "float_bits_conv", since = "1.20.0")]
- #[inline]
- pub fn from_bits(v: u32) -> Self {
- num::Float::from_bits(v)
- }
}
#[cfg(test)]
#![allow(missing_docs)]
#[cfg(not(test))]
-use core::num;
+#[cfg(stage0)]
+use core::num::Float;
#[cfg(not(test))]
use intrinsics;
#[cfg(not(test))]
+#[cfg(stage0)]
use num::FpCategory;
#[cfg(not(test))]
use sys::cmath;
pub use core::f64::consts;
#[cfg(not(test))]
-#[lang = "f64"]
+#[cfg_attr(stage0, lang = "f64")]
+#[cfg_attr(not(stage0), lang = "f64_runtime")]
impl f64 {
- /// Returns `true` if this value is `NaN` and false otherwise.
- ///
- /// ```
- /// use std::f64;
- ///
- /// let nan = f64::NAN;
- /// let f = 7.0_f64;
- ///
- /// assert!(nan.is_nan());
- /// assert!(!f.is_nan());
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn is_nan(self) -> bool { num::Float::is_nan(self) }
-
- /// Returns `true` if this value is positive infinity or negative infinity and
- /// false otherwise.
- ///
- /// ```
- /// use std::f64;
- ///
- /// let f = 7.0f64;
- /// let inf = f64::INFINITY;
- /// let neg_inf = f64::NEG_INFINITY;
- /// let nan = f64::NAN;
- ///
- /// assert!(!f.is_infinite());
- /// assert!(!nan.is_infinite());
- ///
- /// assert!(inf.is_infinite());
- /// assert!(neg_inf.is_infinite());
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn is_infinite(self) -> bool { num::Float::is_infinite(self) }
-
- /// Returns `true` if this number is neither infinite nor `NaN`.
- ///
- /// ```
- /// use std::f64;
- ///
- /// let f = 7.0f64;
- /// let inf: f64 = f64::INFINITY;
- /// let neg_inf: f64 = f64::NEG_INFINITY;
- /// let nan: f64 = f64::NAN;
- ///
- /// assert!(f.is_finite());
- ///
- /// assert!(!nan.is_finite());
- /// assert!(!inf.is_finite());
- /// assert!(!neg_inf.is_finite());
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn is_finite(self) -> bool { num::Float::is_finite(self) }
-
- /// Returns `true` if the number is neither zero, infinite,
- /// [subnormal][subnormal], or `NaN`.
- ///
- /// ```
- /// use std::f64;
- ///
- /// let min = f64::MIN_POSITIVE; // 2.2250738585072014e-308f64
- /// let max = f64::MAX;
- /// let lower_than_min = 1.0e-308_f64;
- /// let zero = 0.0f64;
- ///
- /// assert!(min.is_normal());
- /// assert!(max.is_normal());
- ///
- /// assert!(!zero.is_normal());
- /// assert!(!f64::NAN.is_normal());
- /// assert!(!f64::INFINITY.is_normal());
- /// // Values between `0` and `min` are Subnormal.
- /// assert!(!lower_than_min.is_normal());
- /// ```
- /// [subnormal]: https://en.wikipedia.org/wiki/Denormal_number
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn is_normal(self) -> bool { num::Float::is_normal(self) }
-
- /// Returns the floating point category of the number. If only one property
- /// is going to be tested, it is generally faster to use the specific
- /// predicate instead.
- ///
- /// ```
- /// use std::num::FpCategory;
- /// use std::f64;
- ///
- /// let num = 12.4_f64;
- /// let inf = f64::INFINITY;
- ///
- /// assert_eq!(num.classify(), FpCategory::Normal);
- /// assert_eq!(inf.classify(), FpCategory::Infinite);
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn classify(self) -> FpCategory { num::Float::classify(self) }
+ #[cfg(stage0)]
+ f64_core_methods!();
/// Returns the largest integer less than or equal to a number.
///
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
- pub fn abs(self) -> f64 { num::Float::abs(self) }
+ pub fn abs(self) -> f64 {
+ unsafe { intrinsics::fabsf64(self) }
+ }
/// Returns a number that represents the sign of `self`.
///
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
- pub fn signum(self) -> f64 { num::Float::signum(self) }
-
- /// Returns `true` if and only if `self` has a positive sign, including `+0.0`, `NaN`s with
- /// positive sign bit and positive infinity.
- ///
- /// ```
- /// let f = 7.0_f64;
- /// let g = -7.0_f64;
- ///
- /// assert!(f.is_sign_positive());
- /// assert!(!g.is_sign_positive());
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn is_sign_positive(self) -> bool { num::Float::is_sign_positive(self) }
-
- #[stable(feature = "rust1", since = "1.0.0")]
- #[rustc_deprecated(since = "1.0.0", reason = "renamed to is_sign_positive")]
- #[inline]
- pub fn is_positive(self) -> bool { num::Float::is_sign_positive(self) }
-
- /// Returns `true` if and only if `self` has a negative sign, including `-0.0`, `NaN`s with
- /// negative sign bit and negative infinity.
- ///
- /// ```
- /// let f = 7.0_f64;
- /// let g = -7.0_f64;
- ///
- /// assert!(!f.is_sign_negative());
- /// assert!(g.is_sign_negative());
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn is_sign_negative(self) -> bool { num::Float::is_sign_negative(self) }
-
- #[stable(feature = "rust1", since = "1.0.0")]
- #[rustc_deprecated(since = "1.0.0", reason = "renamed to is_sign_negative")]
- #[inline]
- pub fn is_negative(self) -> bool { num::Float::is_sign_negative(self) }
+ pub fn signum(self) -> f64 {
+ if self.is_nan() {
+ NAN
+ } else {
+ unsafe { intrinsics::copysignf64(1.0, self) }
+ }
+ }
/// Fused multiply-add. Computes `(self * a) + b` with only one rounding
/// error. This produces a more accurate result with better performance than
}
}
- /// Takes the reciprocal (inverse) of a number, `1/x`.
- ///
- /// ```
- /// let x = 2.0_f64;
- /// let abs_difference = (x.recip() - (1.0/x)).abs();
- ///
- /// assert!(abs_difference < 1e-10);
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn recip(self) -> f64 { num::Float::recip(self) }
-
/// Raises a number to an integer power.
///
/// Using this function is generally faster than using `powf`
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
- pub fn powi(self, n: i32) -> f64 { num::Float::powi(self, n) }
+ pub fn powi(self, n: i32) -> f64 {
+ unsafe { intrinsics::powif64(self, n) }
+ }
/// Raises a number to a floating point power.
///
self.log_wrapper(|n| { unsafe { intrinsics::log10f64(n) } })
}
- /// Converts radians to degrees.
- ///
- /// ```
- /// use std::f64::consts;
- ///
- /// let angle = consts::PI;
- ///
- /// let abs_difference = (angle.to_degrees() - 180.0).abs();
- ///
- /// assert!(abs_difference < 1e-10);
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn to_degrees(self) -> f64 { num::Float::to_degrees(self) }
-
- /// Converts degrees to radians.
- ///
- /// ```
- /// use std::f64::consts;
- ///
- /// let angle = 180.0_f64;
- ///
- /// let abs_difference = (angle.to_radians() - consts::PI).abs();
- ///
- /// assert!(abs_difference < 1e-10);
- /// ```
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn to_radians(self) -> f64 { num::Float::to_radians(self) }
-
- /// Returns the maximum of the two numbers.
- ///
- /// ```
- /// let x = 1.0_f64;
- /// let y = 2.0_f64;
- ///
- /// assert_eq!(x.max(y), y);
- /// ```
- ///
- /// If one of the arguments is NaN, then the other argument is returned.
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn max(self, other: f64) -> f64 {
- num::Float::max(self, other)
- }
-
- /// Returns the minimum of the two numbers.
- ///
- /// ```
- /// let x = 1.0_f64;
- /// let y = 2.0_f64;
- ///
- /// assert_eq!(x.min(y), x);
- /// ```
- ///
- /// If one of the arguments is NaN, then the other argument is returned.
- #[stable(feature = "rust1", since = "1.0.0")]
- #[inline]
- pub fn min(self, other: f64) -> f64 {
- num::Float::min(self, other)
- }
-
/// The positive difference of two numbers.
///
/// * If `self <= other`: `0:0`
}
}
}
-
- /// Raw transmutation to `u64`.
- ///
- /// This is currently identical to `transmute::<f64, u64>(self)` on all platforms.
- ///
- /// See `from_bits` for some discussion of the portability of this operation
- /// (there are almost no issues).
- ///
- /// Note that this function is distinct from `as` casting, which attempts to
- /// preserve the *numeric* value, and not the bitwise value.
- ///
- /// # Examples
- ///
- /// ```
- /// assert!((1f64).to_bits() != 1f64 as u64); // to_bits() is not casting!
- /// assert_eq!((12.5f64).to_bits(), 0x4029000000000000);
- ///
- /// ```
- #[stable(feature = "float_bits_conv", since = "1.20.0")]
- #[inline]
- pub fn to_bits(self) -> u64 {
- num::Float::to_bits(self)
- }
-
- /// Raw transmutation from `u64`.
- ///
- /// This is currently identical to `transmute::<u64, f64>(v)` on all platforms.
- /// It turns out this is incredibly portable, for two reasons:
- ///
- /// * Floats and Ints have the same endianness on all supported platforms.
- /// * IEEE-754 very precisely specifies the bit layout of floats.
- ///
- /// However there is one caveat: prior to the 2008 version of IEEE-754, how
- /// to interpret the NaN signaling bit wasn't actually specified. Most platforms
- /// (notably x86 and ARM) picked the interpretation that was ultimately
- /// standardized in 2008, but some didn't (notably MIPS). As a result, all
- /// signaling NaNs on MIPS are quiet NaNs on x86, and vice-versa.
- ///
- /// Rather than trying to preserve signaling-ness cross-platform, this
- /// implementation favours preserving the exact bits. This means that
- /// any payloads encoded in NaNs will be preserved even if the result of
- /// this method is sent over the network from an x86 machine to a MIPS one.
- ///
- /// If the results of this method are only manipulated by the same
- /// architecture that produced them, then there is no portability concern.
- ///
- /// If the input isn't NaN, then there is no portability concern.
- ///
- /// If you don't care about signalingness (very likely), then there is no
- /// portability concern.
- ///
- /// Note that this function is distinct from `as` casting, which attempts to
- /// preserve the *numeric* value, and not the bitwise value.
- ///
- /// # Examples
- ///
- /// ```
- /// use std::f64;
- /// let v = f64::from_bits(0x4029000000000000);
- /// let difference = (v - 12.5).abs();
- /// assert!(difference <= 1e-5);
- /// ```
- #[stable(feature = "float_bits_conv", since = "1.20.0")]
- #[inline]
- pub fn from_bits(v: u64) -> Self {
- num::Float::from_bits(v)
- }
}
#[cfg(test)]
#![feature(collections_range)]
#![feature(compiler_builtins_lib)]
#![feature(const_fn)]
-#![feature(core_float)]
+#![cfg_attr(stage0, feature(core_float))]
#![feature(core_intrinsics)]
#![feature(dropck_eyepatch)]
#![feature(exact_size_is_empty)]
#![feature(fs_read_write)]
#![feature(fixed_size_array)]
#![feature(float_from_str_radix)]
+#![cfg_attr(stage0, feature(float_internals))]
#![feature(fn_traits)]
#![feature(fnbox)]
#![cfg_attr(stage0, feature(generic_param_attrs))]
#![feature(doc_spotlight)]
#![cfg_attr(test, feature(update_panic_count))]
#![cfg_attr(windows, feature(used))]
+#![feature(doc_alias)]
#![default_lib_allocator]
// except according to those terms.
#[doc(primitive = "bool")]
+#[doc(alias = "true")]
+#[doc(alias = "false")]
//
/// The boolean type.
///
mod prim_bool { }
#[doc(primitive = "never")]
+#[doc(alias = "!")]
//
/// The `!` type, also called "never".
///
mod prim_array { }
#[doc(primitive = "slice")]
+#[doc(alias = "[")]
+#[doc(alias = "]")]
+#[doc(alias = "[]")]
//
/// A dynamically-sized view into a contiguous sequence, `[T]`.
///
mod prim_str { }
#[doc(primitive = "tuple")]
+#[doc(alias = "(")]
+#[doc(alias = ")")]
+#[doc(alias = "()")]
//
/// A finite heterogeneous sequence, `(T, U, ..)`.
///
mod prim_usize { }
#[doc(primitive = "reference")]
+#[doc(alias = "&")]
//
/// References, both shared and mutable.
///
}
impl Wtf8 {
- pub fn is_ascii(&self) -> bool {
- self.bytes.is_ascii()
- }
- pub fn to_ascii_uppercase(&self) -> Wtf8Buf {
- Wtf8Buf { bytes: self.bytes.to_ascii_uppercase() }
- }
- pub fn to_ascii_lowercase(&self) -> Wtf8Buf {
- Wtf8Buf { bytes: self.bytes.to_ascii_lowercase() }
- }
- pub fn eq_ignore_ascii_case(&self, other: &Wtf8) -> bool {
- self.bytes.eq_ignore_ascii_case(&other.bytes)
- }
-
pub fn make_ascii_uppercase(&mut self) { self.bytes.make_ascii_uppercase() }
- pub fn make_ascii_lowercase(&mut self) { self.bytes.make_ascii_lowercase() }
}
#[cfg(test)]
(active, proc_macro_mod, "1.27.0", None, None),
(active, proc_macro_expr, "1.27.0", None, None),
(active, proc_macro_non_items, "1.27.0", None, None),
+
+ // #[doc(alias = "...")]
+ (active, doc_alias, "1.27.0", Some(50146), None),
);
declare_features! (
gate_feature_post!(&self, doc_spotlight, attr.span,
"#[doc(spotlight)] is experimental"
);
+ } else if content.iter().any(|c| c.check_name("alias")) {
+ gate_feature_post!(&self, doc_alias, attr.span,
+ "#[doc(alias = \"...\")] is experimental"
+ );
}
}
}
#![no_std]
// OK
-#[lang = "str"]
+#[lang = "str_alloc"]
impl str {}
impl str {
--- /dev/null
+// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+const QUERY = '&';
+
+const EXPECTED = {
+ 'others': [
+ { 'path': 'std', 'name': 'reference' },
+ ],
+};
--- /dev/null
+// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+const QUERY = '+';
+
+const EXPECTED = {
+ 'others': [
+ { 'path': 'std::ops::AddAssign', 'name': 'AddAssign' },
+ { 'path': 'std::ops::Add', 'name': 'Add' },
+ ],
+};
--- /dev/null
+// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+const QUERY = '!';
+
+const EXPECTED = {
+ 'others': [
+ { 'path': 'std', 'name': 'never' },
+ ],
+};
--- /dev/null
+// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+const QUERY = '[';
+
+const EXPECTED = {
+ 'others': [
+ { 'path': 'std', 'name': 'slice' },
+ { 'path': 'std::ops::IndexMut', 'name': 'IndexMut' },
+ { 'path': 'std::ops::Index', 'name': 'Index' },
+ ],
+};
--- /dev/null
+// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+const QUERY = '!';
+
+const EXPECTED = {
+ 'others': [
+ { 'path': 'std', 'name': 'never' },
+ ],
+};
pub mod str {
#![doc(primitive = "str")]
- #[lang = "str"]
+ #[lang = "str_alloc"]
impl str {
// @has search-index.js foo
pub fn foo(&self) {}
--- /dev/null
+// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#[doc(alias = "foo")] //~ ERROR: #[doc(alias = "...")] is experimental
+pub struct Foo;
+
+fn main() {}
--- /dev/null
+error[E0658]: #[doc(alias = "...")] is experimental (see issue #50146)
+ --> $DIR/feature-gate-doc_alias.rs:11:1
+ |
+LL | #[doc(alias = "foo")] //~ ERROR: #[doc(alias = "...")] is experimental
+ | ^^^^^^^^^^^^^^^^^^^^^
+ |
+ = help: add #![feature(doc_alias)] to the crate attributes to enable
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0658`.
| ^^^^^^^^
|
= help: items from traits can only be used if the trait is implemented and in scope
- = note: the following traits define an item `is_empty`, perhaps you need to implement one of them:
+ = note: the following trait defines an item `is_empty`, perhaps you need to implement it:
candidate #1: `std::iter::ExactSizeIterator`
- candidate #2: `core::slice::SliceExt`
- candidate #3: `core::str::StrExt`
error: aborting due to previous error
macro_rules! real_method_stmt {
() => {
- 2.0.powi(2) //~ ERROR can't call method `powi` on ambiguous numeric type `{float}`
+ 2.0.recip() //~ ERROR can't call method `recip` on ambiguous numeric type `{float}`
}
}
macro_rules! real_method_expr {
() => {
- 2.0.powi(2) //~ ERROR can't call method `powi` on ambiguous numeric type `{float}`
+ 2.0.recip() //~ ERROR can't call method `recip` on ambiguous numeric type `{float}`
}
}
LL | fake_anon_field_stmt!();
| ------------------------ in this macro invocation
-error[E0689]: can't call method `powi` on ambiguous numeric type `{float}`
+error[E0689]: can't call method `recip` on ambiguous numeric type `{float}`
--> $DIR/macro-backtrace-invalid-internals.rs:51:15
|
-LL | 2.0.powi(2) //~ ERROR can't call method `powi` on ambiguous numeric type `{float}`
- | ^^^^
+LL | 2.0.recip() //~ ERROR can't call method `recip` on ambiguous numeric type `{float}`
+ | ^^^^^
...
LL | real_method_stmt!();
| -------------------- in this macro invocation
help: you must specify a concrete type for this numeric value, like `f32`
|
-LL | 2.0_f32.powi(2) //~ ERROR can't call method `powi` on ambiguous numeric type `{float}`
+LL | 2.0_f32.recip() //~ ERROR can't call method `recip` on ambiguous numeric type `{float}`
| ^^^^^^^
error[E0599]: no method named `fake` found for type `{integer}` in the current scope
LL | let _ = fake_anon_field_expr!();
| ----------------------- in this macro invocation
-error[E0689]: can't call method `powi` on ambiguous numeric type `{float}`
+error[E0689]: can't call method `recip` on ambiguous numeric type `{float}`
--> $DIR/macro-backtrace-invalid-internals.rs:57:15
|
-LL | 2.0.powi(2) //~ ERROR can't call method `powi` on ambiguous numeric type `{float}`
- | ^^^^
+LL | 2.0.recip() //~ ERROR can't call method `recip` on ambiguous numeric type `{float}`
+ | ^^^^^
...
LL | let _ = real_method_expr!();
| ------------------- in this macro invocation
help: you must specify a concrete type for this numeric value, like `f32`
|
-LL | 2.0_f32.powi(2) //~ ERROR can't call method `powi` on ambiguous numeric type `{float}`
+LL | 2.0_f32.recip() //~ ERROR can't call method `recip` on ambiguous numeric type `{float}`
| ^^^^^^^
error: aborting due to 8 previous errors
// except according to those terms.
fn main() {
- let x = 2.0.powi(2);
- //~^ ERROR can't call method `powi` on ambiguous numeric type `{float}`
+ let x = 2.0.recip();
+ //~^ ERROR can't call method `recip` on ambiguous numeric type `{float}`
let y = 2.0;
- let x = y.powi(2);
- //~^ ERROR can't call method `powi` on ambiguous numeric type `{float}`
+ let x = y.recip();
+ //~^ ERROR can't call method `recip` on ambiguous numeric type `{float}`
println!("{:?}", x);
}
-error[E0689]: can't call method `powi` on ambiguous numeric type `{float}`
+error[E0689]: can't call method `recip` on ambiguous numeric type `{float}`
--> $DIR/method-on-ambiguous-numeric-type.rs:12:17
|
-LL | let x = 2.0.powi(2);
- | ^^^^
+LL | let x = 2.0.recip();
+ | ^^^^^
help: you must specify a concrete type for this numeric value, like `f32`
|
-LL | let x = 2.0_f32.powi(2);
+LL | let x = 2.0_f32.recip();
| ^^^^^^^
-error[E0689]: can't call method `powi` on ambiguous numeric type `{float}`
+error[E0689]: can't call method `recip` on ambiguous numeric type `{float}`
--> $DIR/method-on-ambiguous-numeric-type.rs:15:15
|
-LL | let x = y.powi(2);
- | ^^^^
+LL | let x = y.recip();
+ | ^^^^^
help: you must specify a type for this binding, like `f32`
|
LL | let y: f32 = 2.0;
-Subproject commit 5ac4ab14f53fe8210befca9c9178a93f7df70a41
+Subproject commit 0a1add2d8689ad12a86f6c32d0a5cd0393dc5d80
var toolchain = argv[2];
var mainJs = readFile("build/" + toolchain + "/doc/main.js");
+ var ALIASES = readFile("build/" + toolchain + "/doc/aliases.js");
var searchIndex = readFile("build/" + toolchain + "/doc/search-index.js").split("\n");
if (searchIndex[searchIndex.length - 1].length === 0) {
searchIndex.pop();
"execSearch"];
finalJS += 'window = { "currentCrate": "std" };\n';
+ finalJS += ALIASES;
finalJS += loadThings(arraysToLoad, 'array', extractArrayVariable, mainJs);
finalJS += loadThings(variablesToLoad, 'variable', extractVariable, mainJs);
finalJS += loadThings(functionsToLoad, 'function', extractFunction, mainJs);