我正在通过编写一个属性宏来混淆源代码中的字符串,它在调试模式下工作得很好,但在发布模式下,它似乎没有任何效果。
我的猜测是,编译器认为将SyncLazy始终产生相同的值,并且在优化期间,它会继续并在编译时进行评估,尽管我不希望出现这种行为。
我尝试将评估包装在 a 中,black_box但编译器似乎没有接受提示。我也尝试过使用该optimize_attribute功能,尽管看起来还没有optimize(none)可用。
基于以下结果的示例cargo expand:
#![feature(once_cell)]
#![feature(bench_black_box)]
use std::hint;
use std::lazy::SyncLazy;
// everything in the lazy was generated by the macro to get the original string back at run-time
static VAL: SyncLazy<String> = SyncLazy::new(|| {
hint::black_box(String::from_utf8(
[
140, 155, 158, 142, 158, 142, 166, 156, 150, 130, 148, 171, 128, 134, 132, 150, 137,
]
.iter()
.enumerate()
.map(|(i, e)| i as u8 ^ !e as u8)
.collect::<Vec<u8>>(),
)) …Run Code Online (Sandbox Code Playgroud) 我想100在当前时间上添加秒数。我正在使用这个库
use time::PrimitiveDateTime as DateTime;
pub fn after(start: DateTime) -> DateTime {
start + 100 seconds // something like this to the start DateTime
}
Run Code Online (Sandbox Code Playgroud) 我有一个像这样运行的程序的一部分,我需要一种使用枚举来过滤集合的方法,但我不确定允许“子枚举”的所有可能性的最佳方法。
在示例中,我想打印所有武器,无论它是什么类型。
use std::collections::BTreeMap;
#[derive(PartialEq, Eq)]
enum Item {
Armor,
Consumable,
Weapons(WeaponTypes),
}
#[derive(PartialEq, Eq)]
enum WeaponTypes {
Axe,
Bow,
Sword,
}
fn main() {
let mut stuff = BTreeMap::<&str, Item>::new();
stuff.insert("helmet of awesomeness", Item::Armor);
stuff.insert("boots of the belligerent", Item::Armor);
stuff.insert("potion of eternal life", Item::Consumable);
stuff.insert("axe of the almighty", Item::Weapons(WeaponTypes::Axe));
stuff.insert("shortbow", Item::Weapons(WeaponTypes::Bow));
stuff.insert("sword of storm giants", Item::Weapons(WeaponTypes::Sword));
stuff
.iter()
// this filter works exactly as intended
.filter(|e| *e.1 == Item::Armor)
// using this filter instead doesn't work because it expects …Run Code Online (Sandbox Code Playgroud)