Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Rust 运行时

本节记录了定义 Rust 运行时某些方面的特性。

global_allocator 属性

global_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 属性从标准库预导入中导出。

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 应用程序使用。