Skip to content

Commit be18613

Browse files
committed
Auto merge of #43252 - vbrandl:doc/default-values, r=GuillaumeGomez
Document default values for primitive types All primitive types implement the `Default` trait but the documentation just says `Returns the "default value" for a type.` and doesn't give a hint about the actual default value. I think it would be good to document the default values in a proper way. I changed the `default_impl` macro to accept a doc string as a third parameter and use this string to overwrite the documentation of `default()` for each primitive type. The generated documentation now looks like this: ![Documentation of default() on the bool primitive](https://i.imgur.com/nK6TApo.png)
2 parents 086eaa7 + caf125f commit be18613

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

src/libcore/default.rs

+19-18
Original file line numberDiff line numberDiff line change
@@ -126,32 +126,33 @@ pub trait Default: Sized {
126126
}
127127

128128
macro_rules! default_impl {
129-
($t:ty, $v:expr) => {
129+
($t:ty, $v:expr, $doc:expr) => {
130130
#[stable(feature = "rust1", since = "1.0.0")]
131131
impl Default for $t {
132132
#[inline]
133+
#[doc = $doc]
133134
fn default() -> $t { $v }
134135
}
135136
}
136137
}
137138

138-
default_impl! { (), () }
139-
default_impl! { bool, false }
140-
default_impl! { char, '\x00' }
139+
default_impl! { (), (), "Returns the default value of `()`" }
140+
default_impl! { bool, false, "Returns the default value of `false`" }
141+
default_impl! { char, '\x00', "Returns the default value of `\\x00`" }
141142

142-
default_impl! { usize, 0 }
143-
default_impl! { u8, 0 }
144-
default_impl! { u16, 0 }
145-
default_impl! { u32, 0 }
146-
default_impl! { u64, 0 }
147-
default_impl! { u128, 0 }
143+
default_impl! { usize, 0, "Returns the default value of `0`" }
144+
default_impl! { u8, 0, "Returns the default value of `0`" }
145+
default_impl! { u16, 0, "Returns the default value of `0`" }
146+
default_impl! { u32, 0, "Returns the default value of `0`" }
147+
default_impl! { u64, 0, "Returns the default value of `0`" }
148+
default_impl! { u128, 0, "Returns the default value of `0`" }
148149

149-
default_impl! { isize, 0 }
150-
default_impl! { i8, 0 }
151-
default_impl! { i16, 0 }
152-
default_impl! { i32, 0 }
153-
default_impl! { i64, 0 }
154-
default_impl! { i128, 0 }
150+
default_impl! { isize, 0, "Returns the default value of `0`" }
151+
default_impl! { i8, 0, "Returns the default value of `0`" }
152+
default_impl! { i16, 0, "Returns the default value of `0`" }
153+
default_impl! { i32, 0, "Returns the default value of `0`" }
154+
default_impl! { i64, 0, "Returns the default value of `0`" }
155+
default_impl! { i128, 0, "Returns the default value of `0`" }
155156

156-
default_impl! { f32, 0.0f32 }
157-
default_impl! { f64, 0.0f64 }
157+
default_impl! { f32, 0.0f32, "Returns the default value of `0.0`" }
158+
default_impl! { f64, 0.0f64, "Returns the default value of `0.0`" }

0 commit comments

Comments
 (0)