diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2023-03-10 12:32:00 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-03-10 12:32:00 +0100 |
| commit | e0c8ba19299bcf6609d4e72d6afb140e1f225458 (patch) | |
| tree | 6583aedd598eff50f64141f335a39515a30bc0da | |
| parent | afd8558d62f28de101c58f4314ea2ba2c1aa60ec (diff) | |
| parent | b439189236a8956eaf9cd37bd601f3df074716f2 (diff) | |
| download | rust-e0c8ba19299bcf6609d4e72d6afb140e1f225458.tar.gz rust-e0c8ba19299bcf6609d4e72d6afb140e1f225458.zip | |
Rollup merge of #108946 - bmoxb:bool-to-float-docs, r=cuviper
Document the resulting values produced when using `From<bool>` on floats Have the documentation of the implementation of `From<bool>` on `f32` and `f64` indicate the output values (`0.0` for `false` and `1.0` for `true`). closes #108939
| -rw-r--r-- | library/core/src/convert/num.rs | 26 |
1 files changed, 24 insertions, 2 deletions
diff --git a/library/core/src/convert/num.rs b/library/core/src/convert/num.rs index 4da7c323492..a74a56bc5b2 100644 --- a/library/core/src/convert/num.rs +++ b/library/core/src/convert/num.rs @@ -172,7 +172,18 @@ impl_from! { f32, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0" #[stable(feature = "float_from_bool", since = "1.68.0")] #[rustc_const_unstable(feature = "const_num_from_num", issue = "87852")] impl const From<bool> for f32 { - /// Converts `bool` to `f32` losslessly. + /// Converts `bool` to `f32` losslessly. The resulting value is positive + /// `0.0` for `false` and `1.0` for `true` values. + /// + /// # Examples + /// ``` + /// let x: f32 = false.into(); + /// assert_eq!(x, 0.0); + /// assert!(x.is_sign_positive()); + /// + /// let y: f32 = true.into(); + /// assert_eq!(y, 1.0); + /// ``` #[inline] fn from(small: bool) -> Self { small as u8 as Self @@ -181,7 +192,18 @@ impl const From<bool> for f32 { #[stable(feature = "float_from_bool", since = "1.68.0")] #[rustc_const_unstable(feature = "const_num_from_num", issue = "87852")] impl const From<bool> for f64 { - /// Converts `bool` to `f64` losslessly. + /// Converts `bool` to `f64` losslessly. The resulting value is positive + /// `0.0` for `false` and `1.0` for `true` values. + /// + /// # Examples + /// ``` + /// let x: f64 = false.into(); + /// assert_eq!(x, 0.0); + /// assert!(x.is_sign_positive()); + /// + /// let y: f64 = true.into(); + /// assert_eq!(y, 1.0); + /// ``` #[inline] fn from(small: bool) -> Self { small as u8 as Self |
