@@ -486,26 +486,34 @@ div_impl_float! { f32 f64 }
486
486
///
487
487
/// # Examples
488
488
///
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.
491
493
///
492
494
/// ```
493
495
/// use std::ops::Rem;
494
496
///
495
- /// struct Foo;
497
+ /// #[derive(PartialEq, Debug)]
498
+ /// struct SplitSlice<'a, T: 'a> {
499
+ /// slice: &'a [T],
500
+ /// }
496
501
///
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> ;
499
504
///
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..]}
503
510
/// }
504
511
/// }
505
512
///
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] });
509
517
/// ```
510
518
#[ lang = "rem" ]
511
519
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
0 commit comments