Skip to content

Commit b439189

Browse files
committed
Add examples section which demonstrates the behaviour (specifically the sign positive aspect)
1 parent 8d2bdb8 commit b439189

File tree

1 file changed

+20
-0
lines changed
  • library/core/src/convert

1 file changed

+20
-0
lines changed

library/core/src/convert/num.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,16 @@ impl_from! { f32, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0"
174174
impl const From<bool> for f32 {
175175
/// Converts `bool` to `f32` losslessly. The resulting value is positive
176176
/// `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+
/// ```
177187
#[inline]
178188
fn from(small: bool) -> Self {
179189
small as u8 as Self
@@ -184,6 +194,16 @@ impl const From<bool> for f32 {
184194
impl const From<bool> for f64 {
185195
/// Converts `bool` to `f64` losslessly. The resulting value is positive
186196
/// `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+
/// ```
187207
#[inline]
188208
fn from(small: bool) -> Self {
189209
small as u8 as Self

0 commit comments

Comments
 (0)