Skip to content

Commit 0ed4bc5

Browse files
authored
Rollup merge of #76615 - GuillaumeGomez:missing-examples-binary-ops, r=jyn514
Add missing examples on binary core traits r? @jyn514
2 parents 90c5b8f + bb9ce7c commit 0ed4bc5

File tree

1 file changed

+134
-0
lines changed

1 file changed

+134
-0
lines changed

Diff for: library/core/src/ops/bit.rs

+134
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ pub trait Not {
3636
type Output;
3737

3838
/// Performs the unary `!` operation.
39+
///
40+
/// # Examples
41+
///
42+
/// ```
43+
/// assert_eq!(!true, false);
44+
/// assert_eq!(!false, true);
45+
/// assert_eq!(!1u8, 254);
46+
/// assert_eq!(!0u8, 255);
47+
/// ```
3948
#[must_use]
4049
#[stable(feature = "rust1", since = "1.0.0")]
4150
fn not(self) -> Self::Output;
@@ -122,6 +131,15 @@ pub trait BitAnd<Rhs = Self> {
122131
type Output;
123132

124133
/// Performs the `&` operation.
134+
///
135+
/// # Examples
136+
///
137+
/// ```
138+
/// assert_eq!(true & false, false);
139+
/// assert_eq!(true & true, true);
140+
/// assert_eq!(5u8 & 1u8, 1);
141+
/// assert_eq!(5u8 & 2u8, 0);
142+
/// ```
125143
#[must_use]
126144
#[stable(feature = "rust1", since = "1.0.0")]
127145
fn bitand(self, rhs: Rhs) -> Self::Output;
@@ -208,6 +226,15 @@ pub trait BitOr<Rhs = Self> {
208226
type Output;
209227

210228
/// Performs the `|` operation.
229+
///
230+
/// # Examples
231+
///
232+
/// ```
233+
/// assert_eq!(true | false, true);
234+
/// assert_eq!(false | false, false);
235+
/// assert_eq!(5u8 | 1u8, 5);
236+
/// assert_eq!(5u8 | 2u8, 7);
237+
/// ```
211238
#[must_use]
212239
#[stable(feature = "rust1", since = "1.0.0")]
213240
fn bitor(self, rhs: Rhs) -> Self::Output;
@@ -297,6 +324,15 @@ pub trait BitXor<Rhs = Self> {
297324
type Output;
298325

299326
/// Performs the `^` operation.
327+
///
328+
/// # Examples
329+
///
330+
/// ```
331+
/// assert_eq!(true ^ false, true);
332+
/// assert_eq!(true ^ true, false);
333+
/// assert_eq!(5u8 ^ 1u8, 4);
334+
/// assert_eq!(5u8 ^ 2u8, 7);
335+
/// ```
300336
#[must_use]
301337
#[stable(feature = "rust1", since = "1.0.0")]
302338
fn bitxor(self, rhs: Rhs) -> Self::Output;
@@ -387,6 +423,13 @@ pub trait Shl<Rhs = Self> {
387423
type Output;
388424

389425
/// Performs the `<<` operation.
426+
///
427+
/// # Examples
428+
///
429+
/// ```
430+
/// assert_eq!(5u8 << 1, 10);
431+
/// assert_eq!(1u8 << 1, 2);
432+
/// ```
390433
#[must_use]
391434
#[stable(feature = "rust1", since = "1.0.0")]
392435
fn shl(self, rhs: Rhs) -> Self::Output;
@@ -498,6 +541,13 @@ pub trait Shr<Rhs = Self> {
498541
type Output;
499542

500543
/// Performs the `>>` operation.
544+
///
545+
/// # Examples
546+
///
547+
/// ```
548+
/// assert_eq!(5u8 >> 1, 2);
549+
/// assert_eq!(2u8 >> 1, 1);
550+
/// ```
501551
#[must_use]
502552
#[stable(feature = "rust1", since = "1.0.0")]
503553
fn shr(self, rhs: Rhs) -> Self::Output;
@@ -612,6 +662,26 @@ shr_impl_all! { u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize }
612662
)]
613663
pub trait BitAndAssign<Rhs = Self> {
614664
/// Performs the `&=` operation.
665+
///
666+
/// # Examples
667+
///
668+
/// ```
669+
/// let mut x = true;
670+
/// x &= false;
671+
/// assert_eq!(x, false);
672+
///
673+
/// let mut x = true;
674+
/// x &= true;
675+
/// assert_eq!(x, true);
676+
///
677+
/// let mut x: u8 = 5;
678+
/// x &= 1;
679+
/// assert_eq!(x, 1);
680+
///
681+
/// let mut x: u8 = 5;
682+
/// x &= 2;
683+
/// assert_eq!(x, 0);
684+
/// ```
615685
#[stable(feature = "op_assign_traits", since = "1.8.0")]
616686
fn bitand_assign(&mut self, rhs: Rhs);
617687
}
@@ -663,6 +733,26 @@ bitand_assign_impl! { bool usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
663733
)]
664734
pub trait BitOrAssign<Rhs = Self> {
665735
/// Performs the `|=` operation.
736+
///
737+
/// # Examples
738+
///
739+
/// ```
740+
/// let mut x = true;
741+
/// x |= false;
742+
/// assert_eq!(x, true);
743+
///
744+
/// let mut x = false;
745+
/// x |= false;
746+
/// assert_eq!(x, false);
747+
///
748+
/// let mut x: u8 = 5;
749+
/// x |= 1;
750+
/// assert_eq!(x, 5);
751+
///
752+
/// let mut x: u8 = 5;
753+
/// x |= 2;
754+
/// assert_eq!(x, 7);
755+
/// ```
666756
#[stable(feature = "op_assign_traits", since = "1.8.0")]
667757
fn bitor_assign(&mut self, rhs: Rhs);
668758
}
@@ -714,6 +804,26 @@ bitor_assign_impl! { bool usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
714804
)]
715805
pub trait BitXorAssign<Rhs = Self> {
716806
/// Performs the `^=` operation.
807+
///
808+
/// # Examples
809+
///
810+
/// ```
811+
/// let mut x = true;
812+
/// x ^= false;
813+
/// assert_eq!(x, true);
814+
///
815+
/// let mut x = true;
816+
/// x ^= true;
817+
/// assert_eq!(x, false);
818+
///
819+
/// let mut x: u8 = 5;
820+
/// x ^= 1;
821+
/// assert_eq!(x, 4);
822+
///
823+
/// let mut x: u8 = 5;
824+
/// x ^= 2;
825+
/// assert_eq!(x, 7);
826+
/// ```
717827
#[stable(feature = "op_assign_traits", since = "1.8.0")]
718828
fn bitxor_assign(&mut self, rhs: Rhs);
719829
}
@@ -763,6 +873,18 @@ bitxor_assign_impl! { bool usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
763873
)]
764874
pub trait ShlAssign<Rhs = Self> {
765875
/// Performs the `<<=` operation.
876+
///
877+
/// # Examples
878+
///
879+
/// ```
880+
/// let mut x: u8 = 5;
881+
/// x <<= 1;
882+
/// assert_eq!(x, 10);
883+
///
884+
/// let mut x: u8 = 1;
885+
/// x <<= 1;
886+
/// assert_eq!(x, 2);
887+
/// ```
766888
#[stable(feature = "op_assign_traits", since = "1.8.0")]
767889
fn shl_assign(&mut self, rhs: Rhs);
768890
}
@@ -833,6 +955,18 @@ shl_assign_impl_all! { u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize }
833955
)]
834956
pub trait ShrAssign<Rhs = Self> {
835957
/// Performs the `>>=` operation.
958+
///
959+
/// # Examples
960+
///
961+
/// ```
962+
/// let mut x: u8 = 5;
963+
/// x >>= 1;
964+
/// assert_eq!(x, 2);
965+
///
966+
/// let mut x: u8 = 2;
967+
/// x >>= 1;
968+
/// assert_eq!(x, 1);
969+
/// ```
836970
#[stable(feature = "op_assign_traits", since = "1.8.0")]
837971
fn shr_assign(&mut self, rhs: Rhs);
838972
}

0 commit comments

Comments
 (0)