Skip to content

Commit e0c8ba1

Browse files
authored
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
2 parents afd8558 + b439189 commit e0c8ba1

File tree

1 file changed

+24
-2
lines changed
  • library/core/src/convert

1 file changed

+24
-2
lines changed

library/core/src/convert/num.rs

+24-2
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,18 @@ impl_from! { f32, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0"
172172
#[stable(feature = "float_from_bool", since = "1.68.0")]
173173
#[rustc_const_unstable(feature = "const_num_from_num", issue = "87852")]
174174
impl const From<bool> for f32 {
175-
/// Converts `bool` to `f32` losslessly.
175+
/// Converts `bool` to `f32` losslessly. The resulting value is positive
176+
/// `0.0` for `false` and `1.0` for `true` values.
177+
///
178+
/// # Examples
179+
/// ```
180+
/// let x: f32 = false.into();
181+
/// assert_eq!(x, 0.0);
182+
/// assert!(x.is_sign_positive());
183+
///
184+
/// let y: f32 = true.into();
185+
/// assert_eq!(y, 1.0);
186+
/// ```
176187
#[inline]
177188
fn from(small: bool) -> Self {
178189
small as u8 as Self
@@ -181,7 +192,18 @@ impl const From<bool> for f32 {
181192
#[stable(feature = "float_from_bool", since = "1.68.0")]
182193
#[rustc_const_unstable(feature = "const_num_from_num", issue = "87852")]
183194
impl const From<bool> for f64 {
184-
/// Converts `bool` to `f64` losslessly.
195+
/// Converts `bool` to `f64` losslessly. The resulting value is positive
196+
/// `0.0` for `false` and `1.0` for `true` values.
197+
///
198+
/// # Examples
199+
/// ```
200+
/// let x: f64 = false.into();
201+
/// assert_eq!(x, 0.0);
202+
/// assert!(x.is_sign_positive());
203+
///
204+
/// let y: f64 = true.into();
205+
/// assert_eq!(y, 1.0);
206+
/// ```
185207
#[inline]
186208
fn from(small: bool) -> Self {
187209
small as u8 as Self

0 commit comments

Comments
 (0)