Skip to content

Commit db7b381

Browse files
authored
Rollup merge of rust-lang#71845 - steveklabnik:add-const-examples, r=dtolnay
Add const examples I only added them to `std::f32` to get feedback on this approach before adding the other constants. When looking at rust-lang#68952, I found the docs a little confusing. Unless you're intimately aware of what's going on here, I don't think it's super clear what is deprecated and what you're supposed to do instead. I think short examples really clarify what's meant here, so that's what I did.
2 parents 04776b1 + 55e37f9 commit db7b381

File tree

3 files changed

+318
-2
lines changed

3 files changed

+318
-2
lines changed

src/libcore/num/f32.rs

+147
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,46 @@ use crate::num::FpCategory;
1818

1919
/// The radix or base of the internal representation of `f32`.
2020
/// Use [`f32::RADIX`](../../std/primitive.f32.html#associatedconstant.RADIX) instead.
21+
///
22+
/// # Examples
23+
///
24+
/// ```rust
25+
/// // deprecated way
26+
/// let r = std::f32::RADIX;
27+
///
28+
/// // intended way
29+
/// let r = f32::RADIX;
30+
/// ```
2131
#[stable(feature = "rust1", since = "1.0.0")]
2232
pub const RADIX: u32 = f32::RADIX;
2333

2434
/// Number of significant digits in base 2.
2535
/// Use [`f32::MANTISSA_DIGITS`](../../std/primitive.f32.html#associatedconstant.MANTISSA_DIGITS) instead.
36+
///
37+
/// # Examples
38+
///
39+
/// ```rust
40+
/// // deprecated way
41+
/// let d = std::f32::MANTISSA_DIGITS;
42+
///
43+
/// // intended way
44+
/// let d = f32::MANTISSA_DIGITS;
45+
/// ```
2646
#[stable(feature = "rust1", since = "1.0.0")]
2747
pub const MANTISSA_DIGITS: u32 = f32::MANTISSA_DIGITS;
48+
2849
/// Approximate number of significant digits in base 10.
2950
/// Use [`f32::DIGITS`](../../std/primitive.f32.html#associatedconstant.DIGITS) instead.
51+
///
52+
/// # Examples
53+
///
54+
/// ```rust
55+
/// // deprecated way
56+
/// let d = std::f32::DIGITS;
57+
///
58+
/// // intended way
59+
/// let d = f32::DIGITS;
60+
/// ```
3061
#[stable(feature = "rust1", since = "1.0.0")]
3162
pub const DIGITS: u32 = f32::DIGITS;
3263

@@ -36,50 +67,166 @@ pub const DIGITS: u32 = f32::DIGITS;
3667
/// This is the difference between `1.0` and the next larger representable number.
3768
///
3869
/// [Machine epsilon]: https://en.wikipedia.org/wiki/Machine_epsilon
70+
///
71+
/// # Examples
72+
///
73+
/// ```rust
74+
/// // deprecated way
75+
/// let e = std::f32::EPSILON;
76+
///
77+
/// // intended way
78+
/// let e = f32::EPSILON;
79+
/// ```
3980
#[stable(feature = "rust1", since = "1.0.0")]
4081
pub const EPSILON: f32 = f32::EPSILON;
4182

4283
/// Smallest finite `f32` value.
4384
/// Use [`f32::MIN`](../../std/primitive.f32.html#associatedconstant.MIN) instead.
85+
///
86+
/// # Examples
87+
///
88+
/// ```rust
89+
/// // deprecated way
90+
/// let min = std::f32::MIN;
91+
///
92+
/// // intended way
93+
/// let min = f32::MIN;
94+
/// ```
4495
#[stable(feature = "rust1", since = "1.0.0")]
4596
pub const MIN: f32 = f32::MIN;
97+
4698
/// Smallest positive normal `f32` value.
4799
/// Use [`f32::MIN_POSITIVE`](../../std/primitive.f32.html#associatedconstant.MIN_POSITIVE) instead.
100+
///
101+
/// # Examples
102+
///
103+
/// ```rust
104+
/// // deprecated way
105+
/// let min = std::f32::MIN_POSITIVE;
106+
///
107+
/// // intended way
108+
/// let min = f32::MIN_POSITIVE;
109+
/// ```
48110
#[stable(feature = "rust1", since = "1.0.0")]
49111
pub const MIN_POSITIVE: f32 = f32::MIN_POSITIVE;
112+
50113
/// Largest finite `f32` value.
51114
/// Use [`f32::MAX`](../../std/primitive.f32.html#associatedconstant.MAX) instead.
115+
///
116+
/// # Examples
117+
///
118+
/// ```rust
119+
/// // deprecated way
120+
/// let max = std::f32::MAX;
121+
///
122+
/// // intended way
123+
/// let max = f32::MAX;
124+
/// ```
52125
#[stable(feature = "rust1", since = "1.0.0")]
53126
pub const MAX: f32 = f32::MAX;
54127

55128
/// One greater than the minimum possible normal power of 2 exponent.
56129
/// Use [`f32::MIN_EXP`](../../std/primitive.f32.html#associatedconstant.MIN_EXP) instead.
130+
///
131+
/// # Examples
132+
///
133+
/// ```rust
134+
/// // deprecated way
135+
/// let min = std::f32::MIN_EXP;
136+
///
137+
/// // intended way
138+
/// let min = f32::MIN_EXP;
139+
/// ```
57140
#[stable(feature = "rust1", since = "1.0.0")]
58141
pub const MIN_EXP: i32 = f32::MIN_EXP;
142+
59143
/// Maximum possible power of 2 exponent.
60144
/// Use [`f32::MAX_EXP`](../../std/primitive.f32.html#associatedconstant.MAX_EXP) instead.
145+
///
146+
/// # Examples
147+
///
148+
/// ```rust
149+
/// // deprecated way
150+
/// let max = std::f32::MAX_EXP;
151+
///
152+
/// // intended way
153+
/// let max = f32::MAX_EXP;
154+
/// ```
61155
#[stable(feature = "rust1", since = "1.0.0")]
62156
pub const MAX_EXP: i32 = f32::MAX_EXP;
63157

64158
/// Minimum possible normal power of 10 exponent.
65159
/// Use [`f32::MIN_10_EXP`](../../std/primitive.f32.html#associatedconstant.MIN_10_EXP) instead.
160+
///
161+
/// # Examples
162+
///
163+
/// ```rust
164+
/// // deprecated way
165+
/// let min = std::f32::MIN_10_EXP;
166+
///
167+
/// // intended way
168+
/// let min = f32::MIN_10_EXP;
169+
/// ```
66170
#[stable(feature = "rust1", since = "1.0.0")]
67171
pub const MIN_10_EXP: i32 = f32::MIN_10_EXP;
172+
68173
/// Maximum possible power of 10 exponent.
69174
/// Use [`f32::MAX_10_EXP`](../../std/primitive.f32.html#associatedconstant.MAX_10_EXP) instead.
175+
///
176+
/// # Examples
177+
///
178+
/// ```rust
179+
/// // deprecated way
180+
/// let max = std::f32::MAX_10_EXP;
181+
///
182+
/// // intended way
183+
/// let max = f32::MAX_10_EXP;
184+
/// ```
70185
#[stable(feature = "rust1", since = "1.0.0")]
71186
pub const MAX_10_EXP: i32 = f32::MAX_10_EXP;
72187

73188
/// Not a Number (NaN).
74189
/// Use [`f32::NAN`](../../std/primitive.f32.html#associatedconstant.NAN) instead.
190+
///
191+
/// # Examples
192+
///
193+
/// ```rust
194+
/// // deprecated way
195+
/// let nan = std::f32::NAN;
196+
///
197+
/// // intended way
198+
/// let nan = f32::NAN;
199+
/// ```
75200
#[stable(feature = "rust1", since = "1.0.0")]
76201
pub const NAN: f32 = f32::NAN;
202+
77203
/// Infinity (∞).
78204
/// Use [`f32::INFINITY`](../../std/primitive.f32.html#associatedconstant.INFINITY) instead.
205+
///
206+
/// # Examples
207+
///
208+
/// ```rust
209+
/// // deprecated way
210+
/// let inf = std::f32::INFINITY;
211+
///
212+
/// // intended way
213+
/// let inf = f32::INFINITY;
214+
/// ```
79215
#[stable(feature = "rust1", since = "1.0.0")]
80216
pub const INFINITY: f32 = f32::INFINITY;
217+
81218
/// Negative infinity (−∞).
82219
/// Use [`f32::NEG_INFINITY`](../../std/primitive.f32.html#associatedconstant.NEG_INFINITY) instead.
220+
///
221+
/// # Examples
222+
///
223+
/// ```rust
224+
/// // deprecated way
225+
/// let ninf = std::f32::NEG_INFINITY;
226+
///
227+
/// // intended way
228+
/// let ninf = f32::NEG_INFINITY;
229+
/// ```
83230
#[stable(feature = "rust1", since = "1.0.0")]
84231
pub const NEG_INFINITY: f32 = f32::NEG_INFINITY;
85232

0 commit comments

Comments
 (0)