该serde_json::to_string()函数将生成一个字符串,其中可能包含nullfor anOption<T>或0for a u32。这使得输出更大,所以我想忽略这些类型的值。
我想简化以下结构的 JSON 字符串输出:
use serde_derive::Serialize; // 1.0.82
#[derive(Serialize)]
pub struct WeightWithOptionGroup {
pub group: Option<String>,
pub proportion: u32,
}
Run Code Online (Sandbox Code Playgroud)
当group是None并且proportion是 0 时,JSON 字符串应该是"{}"
感谢您的回答如何更改 Serde 的默认实现以返回空对象而不是 null?,它可以解决Option问题,但0没有解决方案。
我知道该错误意味着什么,但我无法修复它。我正在用来mockers测试我的工作,在尝试验证提供给模拟特征函数的结构参数时,我陷入了困境。简化的代码:
#[cfg(test)]
extern crate mockers;
#[cfg(test)]
extern crate mockers_derive;
#[cfg(test)]
use mockers_derive::mocked;
#[derive(Ord, PartialOrd, Eq, PartialEq, Debug)]
pub struct Thing {
pub key: String,
pub class: String,
}
#[cfg_attr(test, mocked)]
pub trait DaoTrait {
fn get(&self, thing: &Thing) -> String;
}
struct DataService {
dao: Box<DaoTrait>,
}
impl DataService {
pub fn get(&self, thing: &Thing) -> String {
self.dao.get(thing)
}
}
#[cfg(test)]
mod test {
use super::*;
use mockers::matchers::eq;
use mockers::Scenario;
#[test]
fn my_test() {
use mockers::matchers::check;
let …Run Code Online (Sandbox Code Playgroud) 我想在一个模块中启动 Rocket main(),这样可以简化,main()但我失败了。我从火箭修改了Quicktart
编码:
mod myRocket {
#![feature(plugin)]
#![plugin(rocket_codegen)]
extern crate rocket;
#[get("/")]
fn index() -> &'static str {
"Hello, world!"
}
pub fn startup() {
rocket::ignite().mount("/", routes![index]).launch();
}
}
fn main() {
myRocket::startup();
}
Run Code Online (Sandbox Code Playgroud)
错误:
error: cannot find macro `routes!` in this scope
--> src\main.rs:12:37
|
12 | rocket::ignite().mount("/", routes![index]).launch();
|
Run Code Online (Sandbox Code Playgroud)
我不知道如何修复它。