Rust 运行时
本节记录定义 Rust 运行时某些方面的功能。
global_allocator 属性
*global_allocator 属性*选择[内存分配器]memory allocator。
Example
#![allow(unused)] fn main() { use core::alloc::{GlobalAlloc, Layout}; use std::alloc::System; struct MyAllocator; unsafe impl GlobalAlloc for MyAllocator { unsafe fn alloc(&self, layout: Layout) -> *mut u8 { unsafe { System.alloc(layout) } } unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { unsafe { System.dealloc(ptr, layout) } } } #[global_allocator] static GLOBAL: MyAllocator = MyAllocator; }
global_allocator 属性使用 MetaWord 语法。
global_allocator 属性只能应用于类型实现 GlobalAlloc trait 的静态项。
global_allocator 属性只能在项上使用一次。
global_allocator 属性只能在 crate 图中使用一次。
global_allocator 属性从标准库 prelude 导出。
windows_subsystem 属性
*windows_subsystem 属性*在 Windows 目标上链接时设置子系统。
Example
#![allow(unused)] #![windows_subsystem = "windows"] fn main() { }
windows_subsystem 属性使用 MetaNameValueStr 语法。接受的值是 "console" 和 "windows"。
windows_subsystem 属性只能应用于 crate 根。
只有第一次使用 windows_subsystem 才有效果。
Note
rustc会对第一次之后的任何使用发出 lint 警告。这可能在未来成为错误。
windows_subsystem 属性在非 Windows 目标和非 bin crate 类型上被忽略。
"console" 子系统是默认值。如果控制台进程从现有控制台运行,则它将附加到该控制台;否则将创建新的控制台窗口。
"windows" 子系统将与任何现有控制台分离运行。
Note
"windows"子系统通常被不希望在启动时显示控制台窗口的 GUI 应用程序使用。