1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Overloadable operators
13 //! Implementing these traits allows you to get an effect similar to
14 //! overloading operators.
16 //! Some of these traits are imported by the prelude, so they are available in
17 //! every Rust program.
19 //! Many of the operators take their operands by value. In non-generic
20 //! contexts involving built-in types, this is usually not a problem.
21 //! However, using these operators in generic code, requires some
22 //! attention if values have to be reused as opposed to letting the operators
23 //! consume them. One option is to occasionally use `clone()`.
24 //! Another option is to rely on the types involved providing additional
25 //! operator implementations for references. For example, for a user-defined
26 //! type `T` which is supposed to support addition, it is probably a good
27 //! idea to have both `T` and `&T` implement the traits `Add<T>` and `Add<&T>`
28 //! so that generic code can be written without unnecessary cloning.
32 //! This example creates a `Point` struct that implements `Add` and `Sub`, and then
33 //! demonstrates adding and subtracting two `Point`s.
36 //! use std::ops::{Add, Sub};
44 //! impl Add for Point {
45 //! type Output = Point;
47 //! fn add(self, other: Point) -> Point {
48 //! Point {x: self.x + other.x, y: self.y + other.y}
52 //! impl Sub for Point {
53 //! type Output = Point;
55 //! fn sub(self, other: Point) -> Point {
56 //! Point {x: self.x - other.x, y: self.y - other.y}
60 //! println!("{:?}", Point {x: 1, y: 0} + Point {x: 2, y: 3});
61 //! println!("{:?}", Point {x: 1, y: 0} - Point {x: 2, y: 3});
65 //! See the documentation for each trait for a minimum implementation that prints
66 //! something to the screen.
68 #![stable(feature = "rust1", since = "1.0.0")]
73 /// The `Drop` trait is used to run some code when a value goes out of scope. This
74 /// is sometimes called a 'destructor'.
78 /// A trivial implementation of `Drop`. The `drop` method is called when `_x` goes
79 /// out of scope, and therefore `main` prints `Dropping!`.
84 /// impl Drop for HasDrop {
85 /// fn drop(&mut self) {
86 /// println!("Dropping!");
95 #[stable(feature = "rust1", since = "1.0.0")]
97 /// The `drop` method, called when the value goes out of scope.
98 #[stable(feature = "rust1", since = "1.0.0")]
102 // implements the unary operator "op &T"
103 // based on "op T" where T is expected to be `Copy`able
104 macro_rules! forward_ref_unop {
105 (impl $imp:ident, $method:ident for $t:ty) => {
106 #[unstable(feature = "core",
107 reason = "recently added, waiting for dust to settle")]
108 impl<'a> $imp for &'a $t {
109 type Output = <$t as $imp>::Output;
112 fn $method(self) -> <$t as $imp>::Output {
119 // implements binary operators "&T op U", "T op &U", "&T op &U"
120 // based on "T op U" where T and U are expected to be `Copy`able
121 macro_rules! forward_ref_binop {
122 (impl $imp:ident, $method:ident for $t:ty, $u:ty) => {
123 #[unstable(feature = "core",
124 reason = "recently added, waiting for dust to settle")]
125 impl<'a> $imp<$u> for &'a $t {
126 type Output = <$t as $imp<$u>>::Output;
129 fn $method(self, other: $u) -> <$t as $imp<$u>>::Output {
130 $imp::$method(*self, other)
134 #[unstable(feature = "core",
135 reason = "recently added, waiting for dust to settle")]
136 impl<'a> $imp<&'a $u> for $t {
137 type Output = <$t as $imp<$u>>::Output;
140 fn $method(self, other: &'a $u) -> <$t as $imp<$u>>::Output {
141 $imp::$method(self, *other)
145 #[unstable(feature = "core",
146 reason = "recently added, waiting for dust to settle")]
147 impl<'a, 'b> $imp<&'a $u> for &'b $t {
148 type Output = <$t as $imp<$u>>::Output;
151 fn $method(self, other: &'a $u) -> <$t as $imp<$u>>::Output {
152 $imp::$method(*self, *other)
158 /// The `Add` trait is used to specify the functionality of `+`.
162 /// A trivial implementation of `Add`. When `Foo + Foo` happens, it ends up
163 /// calling `add`, and therefore, `main` prints `Adding!`.
166 /// use std::ops::Add;
171 /// impl Add for Foo {
172 /// type Output = Foo;
174 /// fn add(self, _rhs: Foo) -> Foo {
175 /// println!("Adding!");
185 #[stable(feature = "rust1", since = "1.0.0")]
186 pub trait Add<RHS=Self> {
187 /// The resulting type after applying the `+` operator
188 #[stable(feature = "rust1", since = "1.0.0")]
191 /// The method for the `+` operator
192 #[stable(feature = "rust1", since = "1.0.0")]
193 fn add(self, rhs: RHS) -> Self::Output;
196 macro_rules! add_impl {
198 #[stable(feature = "rust1", since = "1.0.0")]
203 fn add(self, other: $t) -> $t { self + other }
206 forward_ref_binop! { impl Add, add for $t, $t }
210 add_impl! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 f32 f64 }
212 /// The `Sub` trait is used to specify the functionality of `-`.
216 /// A trivial implementation of `Sub`. When `Foo - Foo` happens, it ends up
217 /// calling `sub`, and therefore, `main` prints `Subtracting!`.
220 /// use std::ops::Sub;
225 /// impl Sub for Foo {
226 /// type Output = Foo;
228 /// fn sub(self, _rhs: Foo) -> Foo {
229 /// println!("Subtracting!");
239 #[stable(feature = "rust1", since = "1.0.0")]
240 pub trait Sub<RHS=Self> {
241 /// The resulting type after applying the `-` operator
242 #[stable(feature = "rust1", since = "1.0.0")]
245 /// The method for the `-` operator
246 #[stable(feature = "rust1", since = "1.0.0")]
247 fn sub(self, rhs: RHS) -> Self::Output;
250 macro_rules! sub_impl {
252 #[stable(feature = "rust1", since = "1.0.0")]
257 fn sub(self, other: $t) -> $t { self - other }
260 forward_ref_binop! { impl Sub, sub for $t, $t }
264 sub_impl! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 f32 f64 }
266 /// The `Mul` trait is used to specify the functionality of `*`.
270 /// A trivial implementation of `Mul`. When `Foo * Foo` happens, it ends up
271 /// calling `mul`, and therefore, `main` prints `Multiplying!`.
274 /// use std::ops::Mul;
279 /// impl Mul for Foo {
280 /// type Output = Foo;
282 /// fn mul(self, _rhs: Foo) -> Foo {
283 /// println!("Multiplying!");
293 #[stable(feature = "rust1", since = "1.0.0")]
294 pub trait Mul<RHS=Self> {
295 /// The resulting type after applying the `*` operator
296 #[stable(feature = "rust1", since = "1.0.0")]
299 /// The method for the `*` operator
300 #[stable(feature = "rust1", since = "1.0.0")]
301 fn mul(self, rhs: RHS) -> Self::Output;
304 macro_rules! mul_impl {
306 #[stable(feature = "rust1", since = "1.0.0")]
311 fn mul(self, other: $t) -> $t { self * other }
314 forward_ref_binop! { impl Mul, mul for $t, $t }
318 mul_impl! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 f32 f64 }
320 /// The `Div` trait is used to specify the functionality of `/`.
324 /// A trivial implementation of `Div`. When `Foo / Foo` happens, it ends up
325 /// calling `div`, and therefore, `main` prints `Dividing!`.
328 /// use std::ops::Div;
333 /// impl Div for Foo {
334 /// type Output = Foo;
336 /// fn div(self, _rhs: Foo) -> Foo {
337 /// println!("Dividing!");
347 #[stable(feature = "rust1", since = "1.0.0")]
348 pub trait Div<RHS=Self> {
349 /// The resulting type after applying the `/` operator
350 #[stable(feature = "rust1", since = "1.0.0")]
353 /// The method for the `/` operator
354 #[stable(feature = "rust1", since = "1.0.0")]
355 fn div(self, rhs: RHS) -> Self::Output;
358 macro_rules! div_impl {
360 #[stable(feature = "rust1", since = "1.0.0")]
365 fn div(self, other: $t) -> $t { self / other }
368 forward_ref_binop! { impl Div, div for $t, $t }
372 div_impl! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 f32 f64 }
374 /// The `Rem` trait is used to specify the functionality of `%`.
378 /// A trivial implementation of `Rem`. When `Foo % Foo` happens, it ends up
379 /// calling `rem`, and therefore, `main` prints `Remainder-ing!`.
382 /// use std::ops::Rem;
387 /// impl Rem for Foo {
388 /// type Output = Foo;
390 /// fn rem(self, _rhs: Foo) -> Foo {
391 /// println!("Remainder-ing!");
401 #[stable(feature = "rust1", since = "1.0.0")]
402 pub trait Rem<RHS=Self> {
403 /// The resulting type after applying the `%` operator
404 #[stable(feature = "rust1", since = "1.0.0")]
407 /// The method for the `%` operator
408 #[stable(feature = "rust1", since = "1.0.0")]
409 fn rem(self, rhs: RHS) -> Self::Output;
412 macro_rules! rem_impl {
414 #[stable(feature = "rust1", since = "1.0.0")]
419 fn rem(self, other: $t) -> $t { self % other }
422 forward_ref_binop! { impl Rem, rem for $t, $t }
426 macro_rules! rem_float_impl {
427 ($t:ty, $fmod:ident) => {
428 #[stable(feature = "rust1", since = "1.0.0")]
433 fn rem(self, other: $t) -> $t {
434 extern { fn $fmod(a: $t, b: $t) -> $t; }
435 unsafe { $fmod(self, other) }
439 forward_ref_binop! { impl Rem, rem for $t, $t }
443 rem_impl! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 }
444 rem_float_impl! { f32, fmodf }
445 rem_float_impl! { f64, fmod }
447 /// The `Neg` trait is used to specify the functionality of unary `-`.
451 /// A trivial implementation of `Neg`. When `-Foo` happens, it ends up calling
452 /// `neg`, and therefore, `main` prints `Negating!`.
455 /// use std::ops::Neg;
460 /// impl Neg for Foo {
461 /// type Output = Foo;
463 /// fn neg(self) -> Foo {
464 /// println!("Negating!");
474 #[stable(feature = "rust1", since = "1.0.0")]
476 /// The resulting type after applying the `-` operator
477 #[stable(feature = "rust1", since = "1.0.0")]
480 /// The method for the unary `-` operator
481 #[stable(feature = "rust1", since = "1.0.0")]
482 fn neg(self) -> Self::Output;
485 macro_rules! neg_impl {
487 #[stable(feature = "rust1", since = "1.0.0")]
489 #[stable(feature = "rust1", since = "1.0.0")]
493 #[stable(feature = "rust1", since = "1.0.0")]
494 fn neg(self) -> $t { -self }
497 forward_ref_unop! { impl Neg, neg for $t }
501 macro_rules! neg_uint_impl {
502 ($t:ty, $t_signed:ty) => {
503 #[stable(feature = "rust1", since = "1.0.0")]
508 fn neg(self) -> $t { -(self as $t_signed) as $t }
511 forward_ref_unop! { impl Neg, neg for $t }
515 neg_impl! { isize i8 i16 i32 i64 f32 f64 }
517 neg_uint_impl! { usize, isize }
518 neg_uint_impl! { u8, i8 }
519 neg_uint_impl! { u16, i16 }
520 neg_uint_impl! { u32, i32 }
521 neg_uint_impl! { u64, i64 }
524 /// The `Not` trait is used to specify the functionality of unary `!`.
528 /// A trivial implementation of `Not`. When `!Foo` happens, it ends up calling
529 /// `not`, and therefore, `main` prints `Not-ing!`.
532 /// use std::ops::Not;
537 /// impl Not for Foo {
538 /// type Output = Foo;
540 /// fn not(self) -> Foo {
541 /// println!("Not-ing!");
551 #[stable(feature = "rust1", since = "1.0.0")]
553 /// The resulting type after applying the `!` operator
554 #[stable(feature = "rust1", since = "1.0.0")]
557 /// The method for the unary `!` operator
558 #[stable(feature = "rust1", since = "1.0.0")]
559 fn not(self) -> Self::Output;
562 macro_rules! not_impl {
564 #[stable(feature = "rust1", since = "1.0.0")]
569 fn not(self) -> $t { !self }
572 forward_ref_unop! { impl Not, not for $t }
576 not_impl! { bool usize u8 u16 u32 u64 isize i8 i16 i32 i64 }
578 /// The `BitAnd` trait is used to specify the functionality of `&`.
582 /// A trivial implementation of `BitAnd`. When `Foo & Foo` happens, it ends up
583 /// calling `bitand`, and therefore, `main` prints `Bitwise And-ing!`.
586 /// use std::ops::BitAnd;
591 /// impl BitAnd for Foo {
592 /// type Output = Foo;
594 /// fn bitand(self, _rhs: Foo) -> Foo {
595 /// println!("Bitwise And-ing!");
605 #[stable(feature = "rust1", since = "1.0.0")]
606 pub trait BitAnd<RHS=Self> {
607 /// The resulting type after applying the `&` operator
608 #[stable(feature = "rust1", since = "1.0.0")]
611 /// The method for the `&` operator
612 #[stable(feature = "rust1", since = "1.0.0")]
613 fn bitand(self, rhs: RHS) -> Self::Output;
616 macro_rules! bitand_impl {
618 #[stable(feature = "rust1", since = "1.0.0")]
623 fn bitand(self, rhs: $t) -> $t { self & rhs }
626 forward_ref_binop! { impl BitAnd, bitand for $t, $t }
630 bitand_impl! { bool usize u8 u16 u32 u64 isize i8 i16 i32 i64 }
632 /// The `BitOr` trait is used to specify the functionality of `|`.
636 /// A trivial implementation of `BitOr`. When `Foo | Foo` happens, it ends up
637 /// calling `bitor`, and therefore, `main` prints `Bitwise Or-ing!`.
640 /// use std::ops::BitOr;
645 /// impl BitOr for Foo {
646 /// type Output = Foo;
648 /// fn bitor(self, _rhs: Foo) -> Foo {
649 /// println!("Bitwise Or-ing!");
659 #[stable(feature = "rust1", since = "1.0.0")]
660 pub trait BitOr<RHS=Self> {
661 /// The resulting type after applying the `|` operator
662 #[stable(feature = "rust1", since = "1.0.0")]
665 /// The method for the `|` operator
666 #[stable(feature = "rust1", since = "1.0.0")]
667 fn bitor(self, rhs: RHS) -> Self::Output;
670 macro_rules! bitor_impl {
672 #[stable(feature = "rust1", since = "1.0.0")]
677 fn bitor(self, rhs: $t) -> $t { self | rhs }
680 forward_ref_binop! { impl BitOr, bitor for $t, $t }
684 bitor_impl! { bool usize u8 u16 u32 u64 isize i8 i16 i32 i64 }
686 /// The `BitXor` trait is used to specify the functionality of `^`.
690 /// A trivial implementation of `BitXor`. When `Foo ^ Foo` happens, it ends up
691 /// calling `bitxor`, and therefore, `main` prints `Bitwise Xor-ing!`.
694 /// use std::ops::BitXor;
699 /// impl BitXor for Foo {
700 /// type Output = Foo;
702 /// fn bitxor(self, _rhs: Foo) -> Foo {
703 /// println!("Bitwise Xor-ing!");
713 #[stable(feature = "rust1", since = "1.0.0")]
714 pub trait BitXor<RHS=Self> {
715 /// The resulting type after applying the `^` operator
716 #[stable(feature = "rust1", since = "1.0.0")]
719 /// The method for the `^` operator
720 #[stable(feature = "rust1", since = "1.0.0")]
721 fn bitxor(self, rhs: RHS) -> Self::Output;
724 macro_rules! bitxor_impl {
726 #[stable(feature = "rust1", since = "1.0.0")]
731 fn bitxor(self, other: $t) -> $t { self ^ other }
734 forward_ref_binop! { impl BitXor, bitxor for $t, $t }
738 bitxor_impl! { bool usize u8 u16 u32 u64 isize i8 i16 i32 i64 }
740 /// The `Shl` trait is used to specify the functionality of `<<`.
744 /// A trivial implementation of `Shl`. When `Foo << Foo` happens, it ends up
745 /// calling `shl`, and therefore, `main` prints `Shifting left!`.
748 /// use std::ops::Shl;
753 /// impl Shl<Foo> for Foo {
754 /// type Output = Foo;
756 /// fn shl(self, _rhs: Foo) -> Foo {
757 /// println!("Shifting left!");
767 #[stable(feature = "rust1", since = "1.0.0")]
769 /// The resulting type after applying the `<<` operator
770 #[stable(feature = "rust1", since = "1.0.0")]
773 /// The method for the `<<` operator
774 #[stable(feature = "rust1", since = "1.0.0")]
775 fn shl(self, rhs: RHS) -> Self::Output;
778 macro_rules! shl_impl {
780 #[stable(feature = "rust1", since = "1.0.0")]
781 impl Shl<$f> for $t {
785 fn shl(self, other: $f) -> $t {
790 forward_ref_binop! { impl Shl, shl for $t, $f }
794 macro_rules! shl_impl_all {
797 shl_impl! { $t, u16 }
798 shl_impl! { $t, u32 }
799 shl_impl! { $t, u64 }
800 shl_impl! { $t, usize }
803 shl_impl! { $t, i16 }
804 shl_impl! { $t, i32 }
805 shl_impl! { $t, i64 }
806 shl_impl! { $t, isize }
810 shl_impl_all! { u8 u16 u32 u64 usize i8 i16 i32 i64 isize }
812 /// The `Shr` trait is used to specify the functionality of `>>`.
816 /// A trivial implementation of `Shr`. When `Foo >> Foo` happens, it ends up
817 /// calling `shr`, and therefore, `main` prints `Shifting right!`.
820 /// use std::ops::Shr;
825 /// impl Shr<Foo> for Foo {
826 /// type Output = Foo;
828 /// fn shr(self, _rhs: Foo) -> Foo {
829 /// println!("Shifting right!");
839 #[stable(feature = "rust1", since = "1.0.0")]
841 /// The resulting type after applying the `>>` operator
842 #[stable(feature = "rust1", since = "1.0.0")]
845 /// The method for the `>>` operator
846 #[stable(feature = "rust1", since = "1.0.0")]
847 fn shr(self, rhs: RHS) -> Self::Output;
850 macro_rules! shr_impl {
852 impl Shr<$f> for $t {
856 fn shr(self, other: $f) -> $t {
861 forward_ref_binop! { impl Shr, shr for $t, $f }
865 macro_rules! shr_impl_all {
868 shr_impl! { $t, u16 }
869 shr_impl! { $t, u32 }
870 shr_impl! { $t, u64 }
871 shr_impl! { $t, usize }
874 shr_impl! { $t, i16 }
875 shr_impl! { $t, i32 }
876 shr_impl! { $t, i64 }
877 shr_impl! { $t, isize }
881 shr_impl_all! { u8 u16 u32 u64 usize i8 i16 i32 i64 isize }
883 /// The `Index` trait is used to specify the functionality of indexing operations
884 /// like `arr[idx]` when used in an immutable context.
888 /// A trivial implementation of `Index`. When `Foo[Bar]` happens, it ends up
889 /// calling `index`, and therefore, `main` prints `Indexing!`.
892 /// use std::ops::Index;
898 /// impl Index<Bar> for Foo {
899 /// type Output = Foo;
901 /// fn index<'a>(&'a self, _index: Bar) -> &'a Foo {
902 /// println!("Indexing!");
912 #[rustc_on_unimplemented = "the type `{Self}` cannot be indexed by `{Idx}`"]
913 #[stable(feature = "rust1", since = "1.0.0")]
914 pub trait Index<Idx: ?Sized> {
915 /// The returned type after indexing
916 #[stable(feature = "rust1", since = "1.0.0")]
919 /// The method for the indexing (`Foo[Bar]`) operation
920 #[stable(feature = "rust1", since = "1.0.0")]
921 fn index<'a>(&'a self, index: Idx) -> &'a Self::Output;
924 /// The `IndexMut` trait is used to specify the functionality of indexing
925 /// operations like `arr[idx]`, when used in a mutable context.
929 /// A trivial implementation of `IndexMut`. When `Foo[Bar]` happens, it ends up
930 /// calling `index_mut`, and therefore, `main` prints `Indexing!`.
933 /// use std::ops::{Index, IndexMut};
939 /// impl Index<Bar> for Foo {
940 /// type Output = Foo;
942 /// fn index<'a>(&'a self, _index: Bar) -> &'a Foo {
947 /// impl IndexMut<Bar> for Foo {
948 /// fn index_mut<'a>(&'a mut self, _index: Bar) -> &'a mut Foo {
949 /// println!("Indexing!");
959 #[rustc_on_unimplemented = "the type `{Self}` cannot be mutably indexed by `{Idx}`"]
960 #[stable(feature = "rust1", since = "1.0.0")]
961 pub trait IndexMut<Idx: ?Sized>: Index<Idx> {
962 /// The method for the indexing (`Foo[Bar]`) operation
963 #[stable(feature = "rust1", since = "1.0.0")]
964 fn index_mut<'a>(&'a mut self, index: Idx) -> &'a mut Self::Output;
967 /// An unbounded range.
968 #[derive(Copy, Clone, PartialEq, Eq)]
970 #[stable(feature = "rust1", since = "1.0.0")]
971 pub struct RangeFull;
973 #[stable(feature = "rust1", since = "1.0.0")]
974 impl fmt::Debug for RangeFull {
975 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
976 fmt::Debug::fmt("..", fmt)
980 /// A (half-open) range which is bounded at both ends.
981 #[derive(Clone, PartialEq, Eq)]
983 #[stable(feature = "rust1", since = "1.0.0")]
984 pub struct Range<Idx> {
985 /// The lower bound of the range (inclusive).
986 #[stable(feature = "rust1", since = "1.0.0")]
988 /// The upper bound of the range (exclusive).
989 #[stable(feature = "rust1", since = "1.0.0")]
993 #[stable(feature = "rust1", since = "1.0.0")]
994 impl<Idx: fmt::Debug> fmt::Debug for Range<Idx> {
995 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
996 write!(fmt, "{:?}..{:?}", self.start, self.end)
1000 /// A range which is only bounded below.
1001 #[derive(Clone, PartialEq, Eq)]
1002 #[lang="range_from"]
1003 #[stable(feature = "rust1", since = "1.0.0")]
1004 pub struct RangeFrom<Idx> {
1005 /// The lower bound of the range (inclusive).
1006 #[stable(feature = "rust1", since = "1.0.0")]
1010 #[stable(feature = "rust1", since = "1.0.0")]
1011 impl<Idx: fmt::Debug> fmt::Debug for RangeFrom<Idx> {
1012 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
1013 write!(fmt, "{:?}..", self.start)
1017 /// A range which is only bounded above.
1018 #[derive(Copy, Clone, PartialEq, Eq)]
1020 #[stable(feature = "rust1", since = "1.0.0")]
1021 pub struct RangeTo<Idx> {
1022 /// The upper bound of the range (exclusive).
1023 #[stable(feature = "rust1", since = "1.0.0")]
1027 #[stable(feature = "rust1", since = "1.0.0")]
1028 impl<Idx: fmt::Debug> fmt::Debug for RangeTo<Idx> {
1029 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
1030 write!(fmt, "..{:?}", self.end)
1034 /// The `Deref` trait is used to specify the functionality of dereferencing
1035 /// operations like `*v`.
1039 /// A struct with a single field which is accessible via dereferencing the
1043 /// use std::ops::Deref;
1045 /// struct DerefExample<T> {
1049 /// impl<T> Deref for DerefExample<T> {
1050 /// type Target = T;
1052 /// fn deref<'a>(&'a self) -> &'a T {
1058 /// let x = DerefExample { value: 'a' };
1059 /// assert_eq!('a', *x);
1063 #[stable(feature = "rust1", since = "1.0.0")]
1065 /// The resulting type after dereferencing
1066 #[stable(feature = "rust1", since = "1.0.0")]
1067 type Target: ?Sized;
1069 /// The method called to dereference a value
1070 #[stable(feature = "rust1", since = "1.0.0")]
1071 fn deref<'a>(&'a self) -> &'a Self::Target;
1074 #[stable(feature = "rust1", since = "1.0.0")]
1075 impl<'a, T: ?Sized> Deref for &'a T {
1078 fn deref(&self) -> &T { *self }
1081 #[stable(feature = "rust1", since = "1.0.0")]
1082 impl<'a, T: ?Sized> Deref for &'a mut T {
1085 fn deref(&self) -> &T { *self }
1088 /// The `DerefMut` trait is used to specify the functionality of dereferencing
1089 /// mutably like `*v = 1;`
1093 /// A struct with a single field which is modifiable via dereferencing the
1097 /// use std::ops::{Deref, DerefMut};
1099 /// struct DerefMutExample<T> {
1103 /// impl<T> Deref for DerefMutExample<T> {
1104 /// type Target = T;
1106 /// fn deref<'a>(&'a self) -> &'a T {
1111 /// impl<T> DerefMut for DerefMutExample<T> {
1112 /// fn deref_mut<'a>(&'a mut self) -> &'a mut T {
1118 /// let mut x = DerefMutExample { value: 'a' };
1120 /// assert_eq!('b', *x);
1124 #[stable(feature = "rust1", since = "1.0.0")]
1125 pub trait DerefMut: Deref {
1126 /// The method called to mutably dereference a value
1127 #[stable(feature = "rust1", since = "1.0.0")]
1128 fn deref_mut<'a>(&'a mut self) -> &'a mut Self::Target;
1131 #[stable(feature = "rust1", since = "1.0.0")]
1132 impl<'a, T: ?Sized> DerefMut for &'a mut T {
1133 fn deref_mut(&mut self) -> &mut T { *self }
1136 /// A version of the call operator that takes an immutable receiver.
1138 #[stable(feature = "rust1", since = "1.0.0")]
1139 #[rustc_paren_sugar]
1140 pub trait Fn<Args> : FnMut<Args> {
1141 /// This is called when the call operator is used.
1142 extern "rust-call" fn call(&self, args: Args) -> Self::Output;
1145 /// A version of the call operator that takes a mutable receiver.
1147 #[stable(feature = "rust1", since = "1.0.0")]
1148 #[rustc_paren_sugar]
1149 pub trait FnMut<Args> : FnOnce<Args> {
1150 /// This is called when the call operator is used.
1151 extern "rust-call" fn call_mut(&mut self, args: Args) -> Self::Output;
1154 /// A version of the call operator that takes a by-value receiver.
1156 #[stable(feature = "rust1", since = "1.0.0")]
1157 #[rustc_paren_sugar]
1158 pub trait FnOnce<Args> {
1159 /// The returned type after the call operator is used.
1162 /// This is called when the call operator is used.
1163 extern "rust-call" fn call_once(self, args: Args) -> Self::Output;