小编Pas*_*ito的帖子

从外部 JSON 模式导入所有定义

我一直在尝试使用JSON 指针来引用和重用JSON 模式

按照这些示例,我可以引用另一个 JSON 模式中声明的特定属性,并且一切都按预期进行,但我还没有找到一种方法,可以使用另一个基本模式的定义来扩展基本 JSON 模式,而不必显式引用每一个财产。

看起来这会很有用,但我还没有发现有迹象表明它可能或不可能。

想象一下基本模式things

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "id": "http://example.com/thing.json",
    "type": "object",
    "additionalProperties": false,
    "properties": {
        "url": {
            "id": "url",
            "type": "string",
            "format": "uri"
        },
        "name": {
            "id": "name",
            "type": "string"
        }
    },
    "required": ["name"]
}
Run Code Online (Sandbox Code Playgroud)

如果我想要一个更具体的person模式来重用thing我可以这样做的两个属性:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "id": "http://example.com/thing/person.json",
    "type": "object",
    "additionalProperties": false,
    "properties": {
        "url": {
            "$ref": "http://example.com/thing.json#/properties/url",
        },
        "name": {
            "$ref": "http://example.com/thing.json#/properties/name",
        },
        "gender": {
            "id": "gender",
            "type": "string",
            "enum": …
Run Code Online (Sandbox Code Playgroud)

jsonschema linked-data json-schema-validator json-schema-defaults

6
推荐指数
1
解决办法
9013
查看次数

如何在 Rust 中实现带有 HashMap 突变的嵌套循环?

我有以下(删减)Rust 代码:

use std::collections::HashMap;

struct Node {
    weight:   f64,
    outbound: f64,
}

struct Graph {
    edges: HashMap<u32, HashMap<u32, f64>>,
    nodes: HashMap<u32, Node>,
}

impl Graph {
    fn mutate(&mut self) {
        for (key, value) in self.nodes.iter() {
            if self.edges.contains_key(key) {
                for (target, weight) in self.edges[key].iter() {
                    self.nodes.entry(*target).or_insert(Node::new()).weight;
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,由于 Rust 所有权规则(操场),我无法编译代码:

graph.rs:88:25: 88:35 error: cannot borrow `self.nodes` as mutable because it is also borrowed as immutable [E0502]
graph.rs:88                         self.nodes.entry(*target).or_insert(Node::new()).weight;
                                    ^~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)

如果我更改要使用的第一个循环HashMap::iter_mut(),则会收到不同的错误(playground): …

ownership rust borrow-checker

5
推荐指数
1
解决办法
721
查看次数