Skip to content

Commit

Permalink
Rollup merge of rust-lang#35864 - matthew-piziak:index-example, r=Gui…
Browse files Browse the repository at this point in the history
…llaumeGomez

replace `Index` example with something more evocative of indexing

r? @steveklabnik
  • Loading branch information
Jonathan Turner authored Aug 22, 2016
2 parents deed0a8 + 1dfc5db commit 4bb6c06
Showing 1 changed file with 29 additions and 13 deletions.
42 changes: 29 additions & 13 deletions src/libcore/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1498,28 +1498,44 @@ shr_assign_impl_all! { u8 u16 u32 u64 usize i8 i16 i32 i64 isize }
///
/// # Examples
///
/// A trivial implementation of `Index`. When `Foo[Bar]` happens, it ends up
/// calling `index`, and therefore, `main` prints `Indexing!`.
/// This example implements `Index` on a read-only `NucleotideCount` container,
/// enabling individual counts to be retrieved with index syntax.
///
/// ```
/// use std::ops::Index;
///
/// #[derive(Copy, Clone)]
/// struct Foo;
/// struct Bar;
/// enum Nucleotide {
/// A,
/// C,
/// G,
/// T,
/// }
///
/// impl Index<Bar> for Foo {
/// type Output = Foo;
/// struct NucleotideCount {
/// a: usize,
/// c: usize,
/// g: usize,
/// t: usize,
/// }
///
/// fn index<'a>(&'a self, _index: Bar) -> &'a Foo {
/// println!("Indexing!");
/// self
/// impl Index<Nucleotide> for NucleotideCount {
/// type Output = usize;
///
/// fn index(&self, nucleotide: Nucleotide) -> &usize {
/// match nucleotide {
/// Nucleotide::A => &self.a,
/// Nucleotide::C => &self.c,
/// Nucleotide::G => &self.g,
/// Nucleotide::T => &self.t,
/// }
/// }
/// }
///
/// fn main() {
/// Foo[Bar];
/// }
/// let nucleotide_count = NucleotideCount {a: 14, c: 9, g: 10, t: 12};
/// assert_eq!(nucleotide_count[Nucleotide::A], 14);
/// assert_eq!(nucleotide_count[Nucleotide::C], 9);
/// assert_eq!(nucleotide_count[Nucleotide::G], 10);
/// assert_eq!(nucleotide_count[Nucleotide::T], 12);
/// ```
#[lang = "index"]
#[rustc_on_unimplemented = "the type `{Self}` cannot be indexed by `{Idx}`"]
Expand Down

0 comments on commit 4bb6c06

Please sign in to comment.