pub unsafe fn alloc(layout: Layout) -> *mut u8Expand description
使用全局分配器分配内存。
如果存在,则此函数将调用转发到用 #[global_allocator] 属性注册的分配器的 GlobalAlloc::alloc 方法,或者将其默认为 std crate。
当 Global 类型的 alloc 方法和 Allocator trait 变得稳定时,应优先使用此函数,而不是 Global 类型的 alloc 方法。
Safety
请参见 GlobalAlloc::alloc。
Examples
use std::alloc::{alloc, dealloc, handle_alloc_error, Layout};
unsafe {
    let layout = Layout::new::<u16>();
    let ptr = alloc(layout);
    if ptr.is_null() {
        handle_alloc_error(layout);
    }
    *(ptr as *mut u16) = 42;
    assert_eq!(*(ptr as *mut u16), 42);
    dealloc(ptr, layout);
}