为了了解Rust的工作原理,我决定查看一个名为Iota的基于终端的文本编辑器.我克隆了存储库并且cargo build只是被告知:
Run Code Online (Sandbox Code Playgroud)error: *if let* syntax is experimental help: add #![feature(if_let)] to the crate attributes to enable
我应该在哪里添加#![feature(if_let)]到箱子属性?
之间有什么区别
#[allow(dead_code)]
// ...some code
Run Code Online (Sandbox Code Playgroud)
和
#[allow(unused)]
// ...some code
Run Code Online (Sandbox Code Playgroud) 我正在学习Rust,并且发现在变量名的开头添加下划线将使编译器在未使用时不会发出警告.我想知道为什么这个功能存在,因为未使用的变量是不受欢迎的.
我正在尝试禁用死代码警告.我尝试了以下内容
cargo build -- -A dead_code
Run Code Online (Sandbox Code Playgroud)
➜rla git :(主)✗货物构建 - -A dead_code错误:参数无效.
所以我想知道如何将rustc参数传递给货物?
我已经看到了一些其他问题和答案,说明let _ = foo()在语句结束时而不是在范围退出时销毁结果,这就是做什么的let _a = foo().
我无法找到任何关于此的官方描述,也没有任何关于此语法的理由.
我对一些交织在一起的东西很感兴趣:
我有返回的行Result。结果并不重要,我不想要unwrap()或任何日志记录。(预期频繁失败)
我怎样才能让这些行的未使用警告静音?
#[allow(unused_must_use)] 应用于 fn 级别时似乎有效,但这不起作用:
#[allow(unused_must_use)]
std::fs::remove_file(&path1);
#[allow(unused_must_use)]
std::fs::remove_file(&path2);
Run Code Online (Sandbox Code Playgroud)
编辑:
whilelet _ = ..作为一种解决方法,#[allow(unused..)]专门针对这种情况而存在,并且编译器也建议使用它。阅读let _ = ..增加了另一层思维。(分配然后放弃)所以我更喜欢#[allow(..)]虽然它更冗长。(如果我let _ = ..经常看到并且习惯了,我可能会改变我的偏好)
所以我四处搜索,发现了一些将宏应用于语句级别的代码。这个问题询问为什么#[allow(unused_must_use)]一行不起作用 - 语法错误?漏洞?只是尚未实现unused_must_use?
编辑: 根据这个:
如何在 Rust 中消除单个语句的警告? 我的代码应该可以工作。
阅读这些:
https://github.com/rust-lang/rust/issues/36675
https://github.com/rust-lang/rust/issues/15701
我在范围级别尝试了允许并且它起作用了。
#[allow(unused_must_use)] {
std::fs::remove_file(&path1);
std::fs::remove_file(&path2);
}
Run Code Online (Sandbox Code Playgroud) 我注意到为了使一段代码不被归类为死代码,它必须可以从所有二进制文件中访问。例子:
Cargo.toml:
[[bin]]
name = "main_one"
path = "src/main_one.rs"
[[bin]]
name = "main_two"
path = "src/main_two.rs"
main_one.rs:
mod utils;
fn main() {
print!("Hello, ");
utils::function_in_question();
}
main_two.rs:
mod utils;
fn main() {
print!("Hello, ");
// utils::function_in_question();
}
utils.rs:
pub fn function_in_question() {
println!("world!");
}
Run Code Online (Sandbox Code Playgroud)
这报告function_in_question为死代码,即使它可以从main_one.rs. 取消注释可解决此问题。如果它仅存在于main_two.rs.
虽然这种行为背后有一些基本原理,但让 VSCode 一直抱怨这一点是很容易的 + Clippy 的输出被这些警告发送垃圾邮件。是否有至少在全球范围内抑制死代码检测的解决方案?应避免使用货物工作空间重构整个项目。
这段代码:
#[allow(dead_code)]
macro_rules! test {
($x:expr) => {{}}
}
fn main() {
println!("Results:")
}
Run Code Online (Sandbox Code Playgroud)
对未使用的宏定义产生以下警告:
warning: unused macro definition
--> /home/xxx/.emacs.d/rust-playground/at-2017-08-02-031315/snippet.rs:10:1
|
10 | / macro_rules! test {
11 | | ($x:expr) => {{}}
12 | | }
| |_^
|
= note: #[warn(unused_macros)] on by default
Run Code Online (Sandbox Code Playgroud)
有可能抑制它吗?正如您所看到的#[allow(dead_code),在宏的情况下无济于事.
I'm fairly new to Rust and coming from Python there are some things that are done very differently. In Python, one can import a single function from a .py file by typing from foo import bar, but I still haven't found any equivalent in Rust.
I have the following files:
.
??? main.rs
??? module.rs
Run Code Online (Sandbox Code Playgroud)
With the following contents:
mod module;
fn main() {
module::hello();
}
Run Code Online (Sandbox Code Playgroud)
pub fn hello() {
println!("Hello");
}
pub fn bye() {
println!("Bye"); …Run Code Online (Sandbox Code Playgroud) 根据这个答案,#[allow(dead_code)]应该可以,但不行
fn main() {
#[allow(dead_code)]
let x = 0;
}
Run Code Online (Sandbox Code Playgroud)