@@ -523,26 +523,34 @@ div_impl_float! { f32 f64 }
523
523
///
524
524
/// # Examples
525
525
///
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.
528
530
///
529
531
/// ```
530
532
/// use std::ops::Rem;
531
533
///
532
- /// struct Foo;
534
+ /// #[derive(PartialEq, Debug)]
535
+ /// struct SplitSlice<'a, T: 'a> {
536
+ /// slice: &'a [T],
537
+ /// }
533
538
///
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> ;
536
541
///
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..]}
540
547
/// }
541
548
/// }
542
549
///
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] });
546
554
/// ```
547
555
#[ lang = "rem" ]
548
556
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
0 commit comments