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 属性 选择一个 内存分配器

例子

#![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 特型 的 静态项

global_allocator 属性在项上只能使用一次。

global_allocator 属性在 crate 图中只能使用一次。

global_allocator 属性从 标准库预导入 中导出。

windows_subsystem属性

windows_subsystem 属性 在 Windows target 上链接时设置 子系统

例子

#![allow(unused)]
#![windows_subsystem = "windows"]
fn main() {
}

windows_subsystem 属性使用 MetaNameValueStr 语法格式。可接受的值为 "console""windows"

windows_subsystem 属性只能应用于 crate 根。

只有第一次使用的 windows_subsystem 才会生效。

注意

rustc 会对第一次之后的任何使用发出 lint。这在将来可能会变成一个错误。

windows_subsystem 属性在非 Windows target 和非 bin crate 类型 上会被忽略。

"console" 子系统是默认值。如果从现有的控制台运行控制台进程,那么它将附加到该控制台;否则将创建一个新的控制台窗口。

"windows" 子系统将脱离任何现有的控制台运行。

注意

"windows" 子系统通常由不希望在启动时显示控制台窗口的 GUI 应用程序使用。