相关疑难解决方法(0)

是否有可能有条件地启用像"derive"这样的属性?

我在我的箱子中添加了一个功能,增加了serde支持.但是,我不太明白如何正确使用它:

// #[derive(Debug, Serialize, Deserialize, Clone)] // goes to:

#[derive(Debug, Clone)]
#[cfg(feature = "serde_support")]
#[derive(Serialize, Deserialize)]
pub struct MyStruct;
Run Code Online (Sandbox Code Playgroud)

这段代码将下面的所有内容cfg(feature)视为有条件编译,因此如果没有我的serde_support功能,我的箱子也没有MyStruct.

我试图用大括号包装它,但它给出了另一个错误:

码:

#[derive(Debug, Clone)]
#[cfg(feature = "serde_support")] {
#[derive(Serialize, Deserialize)]
}
pub struct MyStruct;
Run Code Online (Sandbox Code Playgroud)

错误:

error: expected item after attributes
  --> mycrate/src/lib.rs:65:33
   |
65 | #[cfg(feature = "serde_support")] {
   |                                 ^
Run Code Online (Sandbox Code Playgroud)

那怎么做?

rust

19
推荐指数
1
解决办法
1775
查看次数

使用功能标志"货物测试"运行其他测试

我有一些测试,我想在使用cargo test时忽略它们,并且仅在显式传递功能标志时运行.我知道这可以通过使用#[ignore]和来完成cargo test -- --ignored,但是我想因其他原因而有多组被忽略的测试.

我试过这个:

#[test]
#[cfg_attr(not(feature = "online_tests"), ignore)]
fn get_github_sample() {}
Run Code Online (Sandbox Code Playgroud)

当我cargo test按照需要运行时会忽略它,但我无法运行它.

我尝试了多种运行Cargo的方法,但测试仍然被忽略:

cargo test --features "online_tests"

cargo test --all-features
Run Code Online (Sandbox Code Playgroud)

然后我Cargo.toml根据此页面将功能定义添加到我的内容中,但它们仍会被忽略.

我在Cargo使用工作区.我尝试在两个Cargo.toml文件中添加功能定义没有区别.

testing rust rust-cargo

8
推荐指数
1
解决办法
605
查看次数

我可以有条件地为 Windows 子系统编译 Rust 程序吗?

我有一个 Rust 程序,当我构建它进行分发时,我想为“windows”子系统编译该程序。目前我在 main.rs 中使用它:

#![feature(windows_subsystem)]
#![windows_subsystem = "windows"]
Run Code Online (Sandbox Code Playgroud)

这是可行的,但是当我在 Windows 计算机上运行测试时,Windows 子系统无法访问控制台,因此我看不到输出。我需要注释掉上面的代码行才能看到测试结果。

有没有办法有条件地编译我正在运行的子系统以使测试正常工作?

windows compilation rust

4
推荐指数
1
解决办法
2179
查看次数

标签 统计

rust ×3

compilation ×1

rust-cargo ×1

testing ×1

windows ×1