如何在启用溢出检查的情况下编译和运行优化的Rust程序 [英] How to compile and run an optimized Rust program with overflow checking enabled

查看:391
本文介绍了如何在启用溢出检查的情况下编译和运行优化的Rust程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序,它的计算量很大,并且在调试模式下运行很烦人。

I'm writing a program that's quite compute heavy, and it's annoyingly slow to run in debug mode.

我的程序也受到整数溢出的困扰,因为我'm从 u8 数组和 u8 类型读取数据时,通过类型推断会传播到意外的地方,Rust更喜欢溢出而不是而不是将整数提升为更大的类型。

My program is also plagued by integer overflows, because I'm reading data from u8 arrays and u8 type spreads to unexpected places via type inference, and Rust prefers to overflow rather than to promote integers to larger types.

在发布模式下构建会禁用溢出检查:

Building in release mode disables overflow checks:

cargo run --release

如何通过优化构建Rust可执行文件

How can I build Rust executable with optimizations and runtime overflow checks enabled as well?

推荐答案

您可以在发布模式下进行编译启用了溢出检查

You can compile in release mode with overflow checks enabled:

[profile.release]
overflow-checks = true

这会将 -Coverflow-checks = true 传递给编译器。在早期版本的Rust中,溢出检查 debug-assertions 开关的一部分,因此您可能需要使用

This passes -C overflow-checks=true to the compiler. In earlier versions of Rust, overflow-checks was part of the debug-assertions switch, so you may need to use that in certain cases.

其他时候,最简单的方法可能是构建在测试或开发人员模式下进行优化

Other times, the easiest thing might be to build in test or dev mode with optimizations:

[profile.dev]
opt-level = 3

这篇关于如何在启用溢出检查的情况下编译和运行优化的Rust程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆