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

The Rust runtime

This section documents features that define some aspects of the Rust runtime.

The global_allocator attribute

The global_allocator attribute selects a 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;
}

The global_allocator attribute uses the MetaWord syntax.

The global_allocator attribute may only be applied to a static item whose type implements the GlobalAlloc trait.

The global_allocator attribute may only be used once on an item.

The global_allocator attribute may only be used once in the crate graph.

The global_allocator attribute is exported from the standard library prelude.

The windows_subsystem attribute

The windows_subsystem attribute sets the subsystem when linking on a Windows target.

Example

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

The windows_subsystem attribute uses the MetaNameValueStr syntax. Accepted values are "console" and "windows".

The windows_subsystem attribute may only be applied to the crate root.

Only the first use of windows_subsystem is honored.

Note

rustc currently lints against uses following the first. This may become a hard error in the future.

The windows_subsystem attribute is ignored on non-Windows targets and non-bin crate types.

The "console" subsystem is the default. If a console process is run from an existing console then it will be attached to that console; otherwise a new console window will be created.

The "windows" subsystem will run detached from any existing console.

Note

The "windows" subsystem is commonly used by GUI applications that do not want to display a console window on startup.