{
"type": "object",
"properties": {
"first_name": { "type": "string" },
"last_name": { "type": "string" },
"birthday": { "type": "string", "format": "date" },
"address": {
"type": "object",
"properties": {
"street_address": { "type": "string" },
"city": { "type": "string" },
"state": { "type": "string" },
"country": { "type" : "string" }
}
}
}
}
{
"first_name": "George",
"last_name": "Washington",
"birthday": "1732-02-22",
"address": {
"street_address": "3200 Mount Vernon Memorial Highway",
"city": "Mount Vernon",
"state": "Virginia",
"country": "United States"
}
}
{
"name": "George Washington",
"birthday": "February 22, 1732",
"address": "Mount Vernon, Virginia, United States"
}
use jsonschema::{Draft, JSONSchema};
use serde_json::json;
fn main() {
let schema = json!({"maxLength": 5});
let instance = json!("foo");
# 编译 Schema
let compiled = JSONSchema::compile(&schema)
.expect("A valid schema");
# 验证实例
let result = compiled.validate(&instance);
if let Err(errors) = result {
for error in errors {
println!("Validation error: {}", error);
println!(
"Instance path: {}", error.instance_path
);
}
}
}
pub struct AThing(u8, &'static str);
const A_VEC_CONST: ManuallyDrop<Vec<AThing>> = vec_const!(AThing, AThing(5, "wow"), AThing(2, "cool"));
fn main()
{
assert_eq!(*TEST, vec!(AThing(5, "wow"), AThing(2, "cool")));
}
$ cargo install cargo-auto
$ cargo auto new
$ cargo auto build
$ cargo auto release
$ cargo auto docs
for_ch! {
for x in 0..10; // forall x in 0..10,
// you can add a label before `for`
for y in x..10, for _ in 0..5; // forall y in x..x+5,
// zipping
if let Some(z) = foo(x, y).await?; // exists z. Some(z) = foo(x, y).await?
// if let guard
if x - y < z; // satisfies x - y < z
// guard
println!("x = {}, y = {}, z = {}", x, y, z);
}
# 结果为
for x in 0..10 {
for y in (x..10).zip(0..5) {
if let Some(z) = foo(x, y).await? {
if x - y < z {
println!("x = {}, y = {}, z = {}", x, y, z);
}
}
}
}
Any 动态地对类型进行专门化的函数。基本思想是,如果 T 和 U 实际上是同一类型,则允许将任何 T “强制转换” 为任何其他 U。use identity_cast::IdentityCast;
fn print_i32_specially<T: 'static>(v: T) {
match v.into_same::<i32>() {
Ok(v) => {
println!("This is a `i32` with value {}", v);
}
Err(_) => {
println!("This is some `T`");
}
}
}
print_i32_specially("123");
# This is some `T`
print_i32_specially(123);
# This is a `i32` with value 123
From 日报小组 长琴
原文作者:Rust语言中文社区;如有侵权,请联系管理员删除。