Skip to content

Commit 61f62ec

Browse files
author
Jonathan Turner
authored
Rollup merge of rust-lang#35861 - matthew-piziak:rem-example, r=GuillaumeGomez
replace `Rem` example with something more evocative r? @steveklabnik
2 parents 21c3287 + 825fd11 commit 61f62ec

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

src/libcore/ops.rs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -523,26 +523,34 @@ div_impl_float! { f32 f64 }
523523
///
524524
/// # Examples
525525
///
526-
/// A trivial implementation of `Rem`. When `Foo % Foo` happens, it ends up
527-
/// calling `rem`, and therefore, `main` prints `Remainder-ing!`.
526+
/// This example implements `Rem` on a `SplitSlice` object. After `Rem` is
527+
/// implemented, one can use the `%` operator to find out what the remaining
528+
/// elements of the slice would be after splitting it into equal slices of a
529+
/// given length.
528530
///
529531
/// ```
530532
/// use std::ops::Rem;
531533
///
532-
/// struct Foo;
534+
/// #[derive(PartialEq, Debug)]
535+
/// struct SplitSlice<'a, T: 'a> {
536+
/// slice: &'a [T],
537+
/// }
533538
///
534-
/// impl Rem for Foo {
535-
/// type Output = Foo;
539+
/// impl<'a, T> Rem<usize> for SplitSlice<'a, T> {
540+
/// type Output = SplitSlice<'a, T>;
536541
///
537-
/// fn rem(self, _rhs: Foo) -> Foo {
538-
/// println!("Remainder-ing!");
539-
/// self
542+
/// fn rem(self, modulus: usize) -> Self {
543+
/// let len = self.slice.len();
544+
/// let rem = len % modulus;
545+
/// let start = len - rem;
546+
/// SplitSlice {slice: &self.slice[start..]}
540547
/// }
541548
/// }
542549
///
543-
/// fn main() {
544-
/// Foo % Foo;
545-
/// }
550+
/// // If we were to divide &[0, 1, 2, 3, 4, 5, 6, 7] into slices of size 3,
551+
/// // the remainder would be &[6, 7]
552+
/// assert_eq!(SplitSlice { slice: &[0, 1, 2, 3, 4, 5, 6, 7] } % 3,
553+
/// SplitSlice { slice: &[6, 7] });
546554
/// ```
547555
#[lang = "rem"]
548556
#[stable(feature = "rust1", since = "1.0.0")]

0 commit comments

Comments
 (0)