diff options
| author | Jonathan Turner <jonathandturner@users.noreply.github.com> | 2016-08-22 15:34:21 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2016-08-22 15:34:21 -0700 |
| commit | 61f62ec5959578be3df90dec3840ddf64acf71e9 (patch) | |
| tree | 4ead2195656f1c357c5bd5b5df8bc356886150a0 /src/libcore | |
| parent | 21c3287478e9c478f9a8170ee8f846a155fe51ec (diff) | |
| parent | 825fd11bd6b06bdd7195e2d946c6120d9b15a78c (diff) | |
| download | rust-61f62ec5959578be3df90dec3840ddf64acf71e9.tar.gz rust-61f62ec5959578be3df90dec3840ddf64acf71e9.zip | |
Rollup merge of #35861 - matthew-piziak:rem-example, r=GuillaumeGomez
replace `Rem` example with something more evocative r? @steveklabnik
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/ops.rs | 30 |
1 files changed, 19 insertions, 11 deletions
diff --git a/src/libcore/ops.rs b/src/libcore/ops.rs index a212ea67980..22d6bb826e5 100644 --- a/src/libcore/ops.rs +++ b/src/libcore/ops.rs @@ -523,26 +523,34 @@ div_impl_float! { f32 f64 } /// /// # Examples /// -/// A trivial implementation of `Rem`. When `Foo % Foo` happens, it ends up -/// calling `rem`, and therefore, `main` prints `Remainder-ing!`. +/// This example implements `Rem` on a `SplitSlice` object. After `Rem` is +/// implemented, one can use the `%` operator to find out what the remaining +/// elements of the slice would be after splitting it into equal slices of a +/// given length. /// /// ``` /// use std::ops::Rem; /// -/// struct Foo; +/// #[derive(PartialEq, Debug)] +/// struct SplitSlice<'a, T: 'a> { +/// slice: &'a [T], +/// } /// -/// impl Rem for Foo { -/// type Output = Foo; +/// impl<'a, T> Rem<usize> for SplitSlice<'a, T> { +/// type Output = SplitSlice<'a, T>; /// -/// fn rem(self, _rhs: Foo) -> Foo { -/// println!("Remainder-ing!"); -/// self +/// fn rem(self, modulus: usize) -> Self { +/// let len = self.slice.len(); +/// let rem = len % modulus; +/// let start = len - rem; +/// SplitSlice {slice: &self.slice[start..]} /// } /// } /// -/// fn main() { -/// Foo % Foo; -/// } +/// // If we were to divide &[0, 1, 2, 3, 4, 5, 6, 7] into slices of size 3, +/// // the remainder would be &[6, 7] +/// assert_eq!(SplitSlice { slice: &[0, 1, 2, 3, 4, 5, 6, 7] } % 3, +/// SplitSlice { slice: &[6, 7] }); /// ``` #[lang = "rem"] #[stable(feature = "rust1", since = "1.0.0")] |
