Table of Content
Rust 요약(메모리 관리)
Rust 는 꽤 독특한 메모리 관리 시스템을 가지고 있다.
소유권
소유권 참조
메모리 할당
/// A macro similar to `vec![$elem; $size]` which returns a boxed array.
///
/// ```rustc
/// let _: Box<[u8; 1024]> = box_array![0; 1024];
/// ```
macro_rules! box_array {
($val:expr ; $len:expr) => {{
// Use a generic function so that the pointer cast remains type-safe
fn vec_to_boxed_array<T>(vec: Vec<T>) -> Box<[T; $len]> {
let boxed_slice = vec.into_boxed_slice();
let ptr = ::std::boxed::Box::into_raw(boxed_slice) as *mut [T; $len];
unsafe { Box::from_raw(ptr) }
}
vec_to_boxed_array(vec![$val; $len])
}};
}
fn main() {
const LEN: usize = 10_000_000;
let _huge_heap_array: Box<[u8; LEN]> = box_array![0; LEN];
}