Rust-reference/runtime
来自菜鸟教程
The Rust runtime
Rust运行时
commit: f8e76ee9368f498f7f044c719de68c7d95da9972
本章译文最后维护日期:2020-11-3
本节介绍 Rust运行时的某些方面的特性。
The panic_handler attribute
panic_handler属性
panic_handler属性只能应用于签名为 fn(&PanicInfo) -> ! 的函数。有此属性标记的函数定义了 panic 的行为。核心库内的结构体 PanicInfo 可以收集 panic 发生点的一些信息。在二进制、dylib 或 cdylib 类型的 crate 的依赖关系图(dependency graph)中必须有一个panic_handler 函数。
下面展示了一个 panic_handler 函数,它记录(log) panic 消息,然后终止(halts)线程。
#![no_std]
use core::fmt::{self, Write};
use core::panic::PanicInfo;
struct Sink {
// ..
# _0: (),
}
#
# impl Sink {
# fn new() -> Sink { Sink { _0: () }}
# }
#
# impl fmt::Write for Sink {
# fn write_str(&mut self, _: &str) -> fmt::Result { Ok(()) }
# }
#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
let mut sink = Sink::new();
// logs "panicked at '$reason', src/main.rs:27:4" to some `sink`
let _ = writeln!(sink, "{}", info);
loop {}
}
Standard behavior
标准行为
标准库提供了 panic_handler 的一个实现,它的默认设置是展开堆栈,但也可以更改为中止(abort)进程。标准库的 panic 行为可以使用 set_hook 函数在运行时里去修改。
The global_allocator attribute
global_allocator属性
在实现 GlobalAlloc trait 的静态项上使用 global_allocator属性来设置全局分配器。
The windows_subsystem attribute
windows_subsystem属性
当为一个 Windows 编译目标配置链接属性时,windows_subsystem属性可以用来在 crate 级别上配置子系统(subsystem)类别。它使用 MetaNameValueStr元项属性句法用 console 或 windows 两个可行值指定子系统。对于非windows 编译目标和非二进制的 crate类型,该属性将被忽略。
#![windows_subsystem = "windows"]