【发布时间】:2021-05-12 18:58:08
【问题描述】:
我正在编写一个程序,它根据 CLI 标志将传入文件分成一个或两个输出文件。
至少它是这样工作的:
use std::{fs::File, io::Write};
fn main() {
// Would be a CLI flag
let write_all = true;
let mut evens = File::create("evens.txt").expect("Nuh-uh");
let mut odds: File;
if write_all {
odds = File::create("odds.txt").expect("Nuh-uh");
}
for i in 1..5 {
if i % 2 == 0 {
write!(&mut evens, "{}\n", i).expect("Can't write");
} else {
if write_all {
write!(&mut odds, "{}\n", i).expect("Can't write");
}
}
}
}
由于odds 在条件中创建时可能未初始化/超出范围,因此无法编译。
error[E0381]: borrow of possibly-uninitialized variable: `odds`
--> src/main.rs:17:24
|
17 | write!(&mut odds, "{}\n", i).expect("Can't write");
| ^^^^^^^^^ use of possibly-uninitialized `odds`
error: aborting due to previous error
我已经看到 this answer 关于使用 Option<File>,但看不到如何将其应用于我的问题。
我考虑过的另一种选择是创建odds.txt 文件,如果!write_all 为真,则在最后删除它,但我不喜欢这种解决方案。
【问题讨论】:
-
不是您的问题的答案,只是一个旁注:
odds确实在条件中初始化,但在编译时(应该)知道条件始终为真。我猜这里的编译器不是很聪明。 -
“但看不到如何将其应用于我的问题。” 那将是罪魁祸首。如果您仍然对此感到困惑,这本书有一个关于how the enum
Optionworks 的部分。你能展示一下使用它的尝试吗? -
@cornuz 该语言具有严格的语义,死代码并没有真正进入 that 方程。更不用说 OP 明确指出
write_all只是为了演示的一个常量本地。 -
不是将赔率作为文件,而是将其作为选项
,而不是一直检查 write_all,而是检查选项是否为 Some(file)。 -
@E_net4thejanitor 谢谢你的指点,我想我现在明白了。如果有机会可以看看我的回答吗?
标签: rust