@@ -177,15 +177,59 @@ pub fn forget<T>(t: T) {
177
177
178
178
/// Returns the size of a type in bytes.
179
179
///
180
- /// More specifically, this is the offset in bytes between successive
181
- /// items of the same type, including alignment padding.
180
+ /// More specifically, this is the offset in bytes between successive elements
181
+ /// in an array with that item type including alignment padding. Thus, for any
182
+ /// type `T` and length `n`, `[T; n]` has a size of `n * size_of::<T>()`.
183
+ ///
184
+ /// In general, the size of a type is not stable across compilations, but
185
+ /// specific types such as primitives are.
186
+ ///
187
+ /// The following table gives the size for primitives.
188
+ ///
189
+ /// Type | size_of::\<Type>()
190
+ /// ---- | ---------------
191
+ /// () | 0
192
+ /// u8 | 1
193
+ /// u16 | 2
194
+ /// u32 | 4
195
+ /// u64 | 8
196
+ /// i8 | 1
197
+ /// i16 | 2
198
+ /// i32 | 4
199
+ /// i64 | 8
200
+ /// f32 | 4
201
+ /// f64 | 8
202
+ /// char | 4
203
+ ///
204
+ /// Furthermore, `usize` and `isize` have the same size.
205
+ ///
206
+ /// The types `*const T`, `&T`, `Box<T>`, `Option<&T>`, and `Option<Box<T>>` all have
207
+ /// the same size. If `T` is Sized, all of those types have the same size as `usize`.
208
+ ///
209
+ /// The mutability of a pointer does not change its size. As such, `&T` and `&mut T`
210
+ /// have the same size. Likewise for `*const T` and `*mut T`.
182
211
///
183
212
/// # Examples
184
213
///
185
214
/// ```
186
215
/// use std::mem;
187
216
///
217
+ /// // Some primitives
188
218
/// assert_eq!(4, mem::size_of::<i32>());
219
+ /// assert_eq!(8, mem::size_of::<f64>());
220
+ /// assert_eq!(0, mem::size_of::<()>());
221
+ ///
222
+ /// // Some arrays
223
+ /// assert_eq!(8, mem::size_of::<[i32; 2]>());
224
+ /// assert_eq!(12, mem::size_of::<[i32; 3]>());
225
+ /// assert_eq!(0, mem::size_of::<[i32; 0]>());
226
+ ///
227
+ ///
228
+ /// // Pointer size equality
229
+ /// assert_eq!(mem::size_of::<&i32>(), mem::size_of::<*const i32>());
230
+ /// assert_eq!(mem::size_of::<&i32>(), mem::size_of::<Box<i32>>());
231
+ /// assert_eq!(mem::size_of::<&i32>(), mem::size_of::<Option<&i32>>());
232
+ /// assert_eq!(mem::size_of::<Box<i32>>(), mem::size_of::<Option<Box<i32>>>());
189
233
/// ```
190
234
#[ inline]
191
235
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
0 commit comments