@@ -151,6 +151,7 @@ mod arith;
151
151
mod bit;
152
152
mod deref;
153
153
mod function;
154
+ mod index;
154
155
mod place;
155
156
mod range;
156
157
mod try;
@@ -171,6 +172,9 @@ pub use self::deref::{Deref, DerefMut};
171
172
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
172
173
pub use self :: function:: { Fn , FnMut , FnOnce } ;
173
174
175
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
176
+ pub use self :: index:: { Index , IndexMut } ;
177
+
174
178
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
175
179
pub use self :: range:: { Range , RangeFrom , RangeFull , RangeTo } ;
176
180
@@ -278,155 +282,6 @@ pub trait Drop {
278
282
fn drop ( & mut self ) ;
279
283
}
280
284
281
- /// The `Index` trait is used to specify the functionality of indexing operations
282
- /// like `container[index]` when used in an immutable context.
283
- ///
284
- /// `container[index]` is actually syntactic sugar for `*container.index(index)`,
285
- /// but only when used as an immutable value. If a mutable value is requested,
286
- /// [`IndexMut`] is used instead. This allows nice things such as
287
- /// `let value = v[index]` if `value` implements [`Copy`].
288
- ///
289
- /// [`IndexMut`]: ../../std/ops/trait.IndexMut.html
290
- /// [`Copy`]: ../../std/marker/trait.Copy.html
291
- ///
292
- /// # Examples
293
- ///
294
- /// The following example implements `Index` on a read-only `NucleotideCount`
295
- /// container, enabling individual counts to be retrieved with index syntax.
296
- ///
297
- /// ```
298
- /// use std::ops::Index;
299
- ///
300
- /// enum Nucleotide {
301
- /// A,
302
- /// C,
303
- /// G,
304
- /// T,
305
- /// }
306
- ///
307
- /// struct NucleotideCount {
308
- /// a: usize,
309
- /// c: usize,
310
- /// g: usize,
311
- /// t: usize,
312
- /// }
313
- ///
314
- /// impl Index<Nucleotide> for NucleotideCount {
315
- /// type Output = usize;
316
- ///
317
- /// fn index(&self, nucleotide: Nucleotide) -> &usize {
318
- /// match nucleotide {
319
- /// Nucleotide::A => &self.a,
320
- /// Nucleotide::C => &self.c,
321
- /// Nucleotide::G => &self.g,
322
- /// Nucleotide::T => &self.t,
323
- /// }
324
- /// }
325
- /// }
326
- ///
327
- /// let nucleotide_count = NucleotideCount {a: 14, c: 9, g: 10, t: 12};
328
- /// assert_eq!(nucleotide_count[Nucleotide::A], 14);
329
- /// assert_eq!(nucleotide_count[Nucleotide::C], 9);
330
- /// assert_eq!(nucleotide_count[Nucleotide::G], 10);
331
- /// assert_eq!(nucleotide_count[Nucleotide::T], 12);
332
- /// ```
333
- #[ lang = "index" ]
334
- #[ rustc_on_unimplemented = "the type `{Self}` cannot be indexed by `{Idx}`" ]
335
- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
336
- pub trait Index < Idx : ?Sized > {
337
- /// The returned type after indexing
338
- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
339
- type Output : ?Sized ;
340
-
341
- /// The method for the indexing (`container[index]`) operation
342
- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
343
- fn index ( & self , index : Idx ) -> & Self :: Output ;
344
- }
345
-
346
- /// The `IndexMut` trait is used to specify the functionality of indexing
347
- /// operations like `container[index]` when used in a mutable context.
348
- ///
349
- /// `container[index]` is actually syntactic sugar for
350
- /// `*container.index_mut(index)`, but only when used as a mutable value. If
351
- /// an immutable value is requested, the [`Index`] trait is used instead. This
352
- /// allows nice things such as `v[index] = value` if `value` implements [`Copy`].
353
- ///
354
- /// [`Index`]: ../../std/ops/trait.Index.html
355
- /// [`Copy`]: ../../std/marker/trait.Copy.html
356
- ///
357
- /// # Examples
358
- ///
359
- /// A very simple implementation of a `Balance` struct that has two sides, where
360
- /// each can be indexed mutably and immutably.
361
- ///
362
- /// ```
363
- /// use std::ops::{Index,IndexMut};
364
- ///
365
- /// #[derive(Debug)]
366
- /// enum Side {
367
- /// Left,
368
- /// Right,
369
- /// }
370
- ///
371
- /// #[derive(Debug, PartialEq)]
372
- /// enum Weight {
373
- /// Kilogram(f32),
374
- /// Pound(f32),
375
- /// }
376
- ///
377
- /// struct Balance {
378
- /// pub left: Weight,
379
- /// pub right:Weight,
380
- /// }
381
- ///
382
- /// impl Index<Side> for Balance {
383
- /// type Output = Weight;
384
- ///
385
- /// fn index<'a>(&'a self, index: Side) -> &'a Weight {
386
- /// println!("Accessing {:?}-side of balance immutably", index);
387
- /// match index {
388
- /// Side::Left => &self.left,
389
- /// Side::Right => &self.right,
390
- /// }
391
- /// }
392
- /// }
393
- ///
394
- /// impl IndexMut<Side> for Balance {
395
- /// fn index_mut<'a>(&'a mut self, index: Side) -> &'a mut Weight {
396
- /// println!("Accessing {:?}-side of balance mutably", index);
397
- /// match index {
398
- /// Side::Left => &mut self.left,
399
- /// Side::Right => &mut self.right,
400
- /// }
401
- /// }
402
- /// }
403
- ///
404
- /// fn main() {
405
- /// let mut balance = Balance {
406
- /// right: Weight::Kilogram(2.5),
407
- /// left: Weight::Pound(1.5),
408
- /// };
409
- ///
410
- /// // In this case balance[Side::Right] is sugar for
411
- /// // *balance.index(Side::Right), since we are only reading
412
- /// // balance[Side::Right], not writing it.
413
- /// assert_eq!(balance[Side::Right],Weight::Kilogram(2.5));
414
- ///
415
- /// // However in this case balance[Side::Left] is sugar for
416
- /// // *balance.index_mut(Side::Left), since we are writing
417
- /// // balance[Side::Left].
418
- /// balance[Side::Left] = Weight::Kilogram(3.0);
419
- /// }
420
- /// ```
421
- #[ lang = "index_mut" ]
422
- #[ rustc_on_unimplemented = "the type `{Self}` cannot be mutably indexed by `{Idx}`" ]
423
- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
424
- pub trait IndexMut < Idx : ?Sized > : Index < Idx > {
425
- /// The method for the mutable indexing (`container[index]`) operation
426
- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
427
- fn index_mut ( & mut self , index : Idx ) -> & mut Self :: Output ;
428
- }
429
-
430
285
/// Trait that indicates that this is a pointer or a wrapper for one,
431
286
/// where unsizing can be performed on the pointee.
432
287
///
0 commit comments