Skip to content

Commit deed0a8

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 631fe3b + 825fd11 commit deed0a8

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

0 commit comments

Comments
 (0)