Skip to content

Commit 825fd11

Browse files
replace Rem example with something more evocative
r? @steveklabnik
1 parent 4901896 commit 825fd11

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

src/libcore/ops.rs

+19-11
Original file line numberDiff line numberDiff line change
@@ -485,26 +485,34 @@ div_impl_float! { f32 f64 }
485485
///
486486
/// # Examples
487487
///
488-
/// A trivial implementation of `Rem`. When `Foo % Foo` happens, it ends up
489-
/// calling `rem`, and therefore, `main` prints `Remainder-ing!`.
488+
/// This example implements `Rem` on a `SplitSlice` object. After `Rem` is
489+
/// implemented, one can use the `%` operator to find out what the remaining
490+
/// elements of the slice would be after splitting it into equal slices of a
491+
/// given length.
490492
///
491493
/// ```
492494
/// use std::ops::Rem;
493495
///
494-
/// struct Foo;
496+
/// #[derive(PartialEq, Debug)]
497+
/// struct SplitSlice<'a, T: 'a> {
498+
/// slice: &'a [T],
499+
/// }
495500
///
496-
/// impl Rem for Foo {
497-
/// type Output = Foo;
501+
/// impl<'a, T> Rem<usize> for SplitSlice<'a, T> {
502+
/// type Output = SplitSlice<'a, T>;
498503
///
499-
/// fn rem(self, _rhs: Foo) -> Foo {
500-
/// println!("Remainder-ing!");
501-
/// self
504+
/// fn rem(self, modulus: usize) -> Self {
505+
/// let len = self.slice.len();
506+
/// let rem = len % modulus;
507+
/// let start = len - rem;
508+
/// SplitSlice {slice: &self.slice[start..]}
502509
/// }
503510
/// }
504511
///
505-
/// fn main() {
506-
/// Foo % Foo;
507-
/// }
512+
/// // If we were to divide &[0, 1, 2, 3, 4, 5, 6, 7] into slices of size 3,
513+
/// // the remainder would be &[6, 7]
514+
/// assert_eq!(SplitSlice { slice: &[0, 1, 2, 3, 4, 5, 6, 7] } % 3,
515+
/// SplitSlice { slice: &[6, 7] });
508516
/// ```
509517
#[lang = "rem"]
510518
#[stable(feature = "rust1", since = "1.0.0")]

0 commit comments

Comments
 (0)