本文簡要介紹rust語言中 Function std::mem::size_of 的用法。
用法
pub const fn size_of<T>() -> usize返回類型的大小(以字節為單位)。
更具體地說,這是數組中具有該項目類型的連續元素之間的字節偏移量,包括對齊填充。因此,對於任何類型 T 和長度 n , [T; n] 的大小為 n * size_of::<T>() 。
一般來說,一個類型的大小在編譯過程中是不穩定的,但特定類型(如原語)是穩定的。
下表給出了基元的大小。
| 類型 | size_of::<類型>() |
|---|---|
| () | 0 |
| bool | 1 |
| u8 | 1 |
| u16 | 2 |
| u32 | 4 |
| u64 | 8 |
| u128 | 16 |
| i8 | 1 |
| i16 | 2 |
| i32 | 4 |
| i64 | 8 |
| i128 | 16 |
| f32 | 4 |
| f64 | 8 |
| char | 4 |
此外,usize和isize具有相同的大小。
*const T 、 &T 、 Box<T> 、 Option<&T> 和 Option<Box<T>> 類型都具有相同的大小。如果 T 是 Sized,則所有這些類型的大小都與 usize 相同。
指針的可變性不會改變它的大小。因此,&T 和&mut T 具有相同的大小。對於 *const T 和 *mut T 也是如此。
#[repr(C)] 項目的大小
項目的C 表示具有定義的布局。使用這種布局,項目的大小也是穩定的,隻要所有字段都有一個穩定的大小。
結構的大小
對於 structs ,大小由以下算法確定。
對於按聲明順序排序的結構中的每個字段:
- 添加字段的大小。
- 將當前大小向上舍入到下一個字段的 alignment 最接近的倍數。
最後,將結構體的大小舍入為其 alignment 最接近的倍數。結構體的對齊方式通常是其所有字段中最大的對齊方式;這可以通過使用 repr(align(N)) 進行更改。
與 C 不同,零大小的結構不會四舍五入到一個字節的大小。
枚舉的大小
除了判別式之外不攜帶任何數據的枚舉與編譯它們的平台上的 C 枚舉具有相同的大小。
工會規模
聯合的大小是其最大字段的大小。
與 C 不同,零大小的聯合不會四舍五入到一個字節的大小。
例子
use std::mem;
// Some primitives
assert_eq!(4, mem::size_of::<i32>());
assert_eq!(8, mem::size_of::<f64>());
assert_eq!(0, mem::size_of::<()>());
// Some arrays
assert_eq!(8, mem::size_of::<[i32; 2]>());
assert_eq!(12, mem::size_of::<[i32; 3]>());
assert_eq!(0, mem::size_of::<[i32; 0]>());
// Pointer size equality
assert_eq!(mem::size_of::<&i32>(), mem::size_of::<*const i32>());
assert_eq!(mem::size_of::<&i32>(), mem::size_of::<Box<i32>>());
assert_eq!(mem::size_of::<&i32>(), mem::size_of::<Option<&i32>>());
assert_eq!(mem::size_of::<Box<i32>>(), mem::size_of::<Option<Box<i32>>>());使用 #[repr(C)] 。
use std::mem;
#[repr(C)]
struct FieldStruct {
first: u8,
second: u16,
third: u8
}
// The size of the first field is 1, so add 1 to the size. Size is 1.
// The alignment of the second field is 2, so add 1 to the size for padding. Size is 2.
// The size of the second field is 2, so add 2 to the size. Size is 4.
// The alignment of the third field is 1, so add 0 to the size for padding. Size is 4.
// The size of the third field is 1, so add 1 to the size. Size is 5.
// Finally, the alignment of the struct is 2 (because the largest alignment amongst its
// fields is 2), so add 1 to the size for padding. Size is 6.
assert_eq!(6, mem::size_of::<FieldStruct>());
#[repr(C)]
struct TupleStruct(u8, u16, u8);
// Tuple structs follow the same rules.
assert_eq!(6, mem::size_of::<TupleStruct>());
// Note that reordering the fields can lower the size. We can remove both padding bytes
// by putting `third` before `second`.
#[repr(C)]
struct FieldStructOptimized {
first: u8,
third: u8,
second: u16
}
assert_eq!(4, mem::size_of::<FieldStructOptimized>());
// Union size is the size of the largest field.
#[repr(C)]
union ExampleUnion {
smaller: u8,
larger: u16
}
assert_eq!(2, mem::size_of::<ExampleUnion>());相關用法
- Rust size_of_val用法及代碼示例
- Rust size_of_val_raw用法及代碼示例
- Rust sink用法及代碼示例
- Rust str.make_ascii_uppercase用法及代碼示例
- Rust slice.sort_unstable_by_key用法及代碼示例
- Rust slice.iter_mut用法及代碼示例
- Rust symlink用法及代碼示例
- Rust slice.windows用法及代碼示例
- Rust slice.repeat用法及代碼示例
- Rust slice.group_by_mut用法及代碼示例
- Rust slice.align_to_mut用法及代碼示例
- Rust slice.as_chunks_unchecked用法及代碼示例
- Rust str.strip_suffix用法及代碼示例
- Rust str.trim_left用法及代碼示例
- Rust slice.fill用法及代碼示例
- Rust slice.array_windows用法及代碼示例
- Rust slice.sort_unstable_by用法及代碼示例
- Rust slice.sort用法及代碼示例
- Rust str.char_indices用法及代碼示例
- Rust str.to_ascii_lowercase用法及代碼示例
- Rust str用法及代碼示例
- Rust slice.rotate_left用法及代碼示例
- Rust slice.as_mut_ptr用法及代碼示例
- Rust str.trim用法及代碼示例
- Rust stringify用法及代碼示例
注:本文由純淨天空篩選整理自rust-lang.org大神的英文原創作品 Function std::mem::size_of。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
