我正在尝试Graph从 JSON 数据创建一个 petgraph 。JSON 包含图的边,键代表起始顶点,值是相邻顶点的列表。可以生成带有边向量的图。
我设法创建了一个Vec<(String, String))>但不是Vec<(&str, &str)>预期的。
extern crate petgraph;
extern crate serde_json;
use petgraph::prelude::*;
use serde_json::{Value, Error};
fn main() {
let data = r#"{
"A": [ "B" ],
"B": [ "C", "D" ],
"D": [ "E", "F" ]
}"#;
let json_value: Value = serde_json::from_str(data).unwrap();
let mut edges: Vec<(String, String)> = vec![];
if let Value::Object(map) = json_value {
for (from_edge, array) in &map {
if let &Value::Array(ref array_value) = array …Run Code Online (Sandbox Code Playgroud) 我有一个类型别名type CardId = u64;,我想将其初始化为可以通过std::u64::MAX常量获得的最大数量。得知我无法使用别名做同样的事情而感到惊讶。
use std::u64;
type CardId = u64;
fn main() {
let this_works = u64::MAX;
let this_doesnt_work = CardId::MAX;
println!("Max amount in integer: {} and {}", this_works, this_doesnt_work);
}
Run Code Online (Sandbox Code Playgroud)
我期望MAX常量也可以从类型别名访问。当我将类型更改为u32时,这将对我有帮助,这将导致代码有两点需要修改,而不仅仅是类型别名的位置。为什么要做出这个决定,而我是否错过了可能使之成为可能的事情?
another_file在 Rust 中使用下划线作为单词分隔符工作正常。
我如何改用连字符 ( another-file.rs)?
// another-file.rs
pub fn method() { }
Run Code Online (Sandbox Code Playgroud)
// lib.rs
use another_file; // <-- ERROR can not find another_file.rs
another_file::method();
Run Code Online (Sandbox Code Playgroud) rustc 在使用println!.
代码:
fn main() {
println!("Hello, world!");
}
Run Code Online (Sandbox Code Playgroud)
运行它:
me@mclaptop:~
> rustc helloworld.rs
me@mclaptop:~
>
Run Code Online (Sandbox Code Playgroud)
为什么它不打印任何东西?
Rust文档说默认的整数类型是i32,这意味着变量默认可以保存的最大数字是2147483647ie 2e31 - 1。事实证明,这是千真万确的:如果我试图挽救数量大于2e31 - 1在x变,我得到的错误literal out of range。
码
fn main() {
let x = 2147483647;
println!("Maximum signed integer: {}", x);
let x = 2e100;
println!("x evalues to: {}", x);
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我2e100在x变量中保存值,为什么我不会出错?它的计算结果肯定大于2e31 - 1。
输出量
fn main() {
let x = 2147483647;
println!("Maximum signed integer: {}", x);
let x = 2e100;
println!("x evalues to: {}", x);
}
Run Code Online (Sandbox Code Playgroud)
码
fn main() {
let …Run Code Online (Sandbox Code Playgroud) 我正在寻找的是一种替换方法:
Run Code Online (Sandbox Code Playgroud)pub fn replace(&mut self, index: usize, element: T) -> T替换向量中位置索引处的元素,并返回现有值。
调用remove + insert对我来说似乎很浪费。
(希望如此)来自一个完整的 Rust 初学者的一个简单问题。我的循环有什么问题?
num评估为 '69' 相当快,但一旦num设置为 '69' ,循环永远不会退出。我错过了一些明显的东西,我敢肯定......
extern crate rand;
use rand::Rng;
fn main() {
let funny_number: u16 = 69;
let mut num: u16 = 0;
let mut rng = rand::thread_rng();
while num != funny_number {
let mut num: u16 = rng.gen_range(0, 100);
println!("{}", num);
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个哈希映射的引用(data在下面的代码中),我想将其克隆到一个新的、拥有的哈希映射中。克隆参考给了我一个新的参考,这不是我需要的。
我还尝试对data引用进行迭代 + 映射,并单独克隆键和值对,然后进行收集,但这也不起作用。这是一个最小的工作示例:
use core::cell::Cell;
use std::collections::HashMap;
use std::collections::HashSet;
use std::rc::Rc;
struct Dummy<K, V> {
dirty: Rc<Cell<bool>>,
data: Cell<Option<HashMap<K, HashSet<V>>>>,
}
impl<K, V> Dummy<K, V> {
fn persist(&self, prefix: &str, data: &HashMap<K, HashSet<V>>) {
self.dirty.set(true);
self.data.set(Some(data.clone()));
}
}
Run Code Online (Sandbox Code Playgroud)
这给出了以下错误:
error[E0308]: mismatched types
--> src/lib.rs:14:28
|
14 | self.data.set(Some(data.clone()));
| ^^^^^^^^^^^^ expected struct `std::collections::HashMap`, found reference
|
= note: expected type `std::collections::HashMap<K, std::collections::HashSet<V>>`
found type `&std::collections::HashMap<K, std::collections::HashSet<V>>`
Run Code Online (Sandbox Code Playgroud)
(固定链接到操场)
这段代码的目的是通过Dummystruct观察hashmap的内容,用于单元测试。
我猜这个问题是因为给定泛型类型没有办法确定如何深度克隆键和值对象?
有没有办法在给定对现有哈希图的引用的情况下创建新的哈希图?
我正在用 Rust 编写一个编译器。作为我的词法分析器的一部分,我试图将输入流中的单个字符与一系列字符(多个)进行匹配。我目前正在尝试使用..运算符的方法。
match input_token {
'Z'..'a' => { // I want to match any character from 'a' -> 'z' and 'A' -> 'Z' inclusive
... run some code
}
}
Run Code Online (Sandbox Code Playgroud)
是否可以在 Rust 匹配表达式/语句的单个分支中匹配多个值?
我的 json 数据看起来像这样:
{ "col1" : 123, "metadata" : { "opt1" : 456, "opt2" : 789 } }
Run Code Online (Sandbox Code Playgroud)
其中各种元数据字段(有很多)是可选的,并且可能存在也可能不存在。
我的查询是:
select col1, metadata.opt1 from "db-name".tablename
Run Code Online (Sandbox Code Playgroud)
如果opt1不存在于任何行中,我希望这会返回该列为空白的所有行opt1,但是如果爬网程序运行时没有包含 in 的行opt1(metadata并且当查询时可能仍然不存在于数据中)运行,因为它是可选的),查询失败,并显示:
SYNTAX_ERROR: line 2:1: Column '"metadata"."opt1"' cannot be resolved
Run Code Online (Sandbox Code Playgroud)
我可以在架构定义中手动指定这些字段(如果我不使用爬网程序),但它不会拾取可能到达的任何新元数据字段,并且指定静态架构似乎并不在雅典娜应该如何工作的精神。
如何让它按预期运行(最好不要放入虚拟行或自定义 SerDe)?
org.openx.data.jsonserde.JsonSerDe目前使用SerDe 。
感谢您的任何想法。