Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rust 如何将toml rs结果转换为std::collections::HashMap_Rust - Fatal编程技术网

Rust 如何将toml rs结果转换为std::collections::HashMap

Rust 如何将toml rs结果转换为std::collections::HashMap,rust,Rust,我对生锈还不熟悉,我试着做一些简单的东西。我想从.toml文件中加载数据,并使用它渲染出一些文本 Rustache似乎将HashMap作为其数据源,通过查看文档,我确信我应该能够将其表和数组类型转换为HashMap和Vec类型,我怀疑这与解码器有关,但我无法理解 如果有人能提供一个简短的例子来说明如何做到这一点,我将不胜感激。如果您的数据结构具有固定的已知深度,那么您只需将正确的类型传递给toml::decode(): 然而,就我所见,rustache提供了某种支持任意嵌套的构建器结构。在这种情

我对生锈还不熟悉,我试着做一些简单的东西。我想从
.toml
文件中加载数据,并使用它渲染出一些文本

Rustache似乎将HashMap作为其数据源,通过查看文档,我确信我应该能够将其
数组
类型转换为
HashMap
Vec
类型,我怀疑这与
解码器
有关,但我无法理解


如果有人能提供一个简短的例子来说明如何做到这一点,我将不胜感激。

如果您的数据结构具有固定的已知深度,那么您只需将正确的类型传递给
toml::decode()

然而,就我所见,rustache提供了某种支持任意嵌套的构建器结构。在这种情况下,您需要将
toml::Value
应用于
rustache::HashBuilder
。您不需要为此使用
Decodable
(尽管您可能可以,使用一些新类型)-您只需要编写几个简单的函数:

fn toml_into_hashbuilder<'a>(value: toml::Table, mut hb: rustache::HashBuilder<'a>) -> rustache::HashBuilder<'a> {
    for (k, v) in value {
        match v {
            toml::Value::String(s) => hb.insert_string(k, s),
            toml::Value::Integer(i) => hb.insert_int(k, i),
            toml::Value::Float(f) => hb.insert_float(k, f),
            toml::Value::Boolean(b) => hb.insert_bool(k, b),
            toml::Value::Datetime(s) => hb.insert_string(k, s),
            toml::Value::Array(arr) => hb.insert_vector(k, |vb| toml_into_vecbuilder(arr.clone(), vb)),
            toml::Value::Table(tbl) => hb.insert_hash(k, |hb| toml_into_hashbuilder(tbl.clone(), hb))
        }
    }
    hb
}

fn toml_into_vecbuilder<'a>(value: toml::Array, mut vb: rustache::VecBuilder<'a>) -> rustache::VecBuilder<'a> {
    for v in value {
        match v {
            toml::Value::String(s) => vb.push_string(s),
            toml::Value::Integer(i) => vb.push_int(i),
            toml::Value::Float(f) => vb.push_float(f),
            toml::Value::Boolean(b) => vb.push_bool(b),
            toml::Value::Datetime(s) => vb.push_string(s),
            toml::Value::Array(arr) => vb.push_vector(|vb| toml_into_vecbuilder(arr.clone(), vb)),
            toml::Value::Table(tbl) => vb.push_hash(|hb| toml_into_hashbuilder(tbl.clone(), hb))
        }
    }
    vb
}

let value: toml::Table = Parser::new(input).parse().unwrap();
let hb = toml_into_hashbuilder(value, rustache::HashBuilder::new());
let result = rustache::render_text(your_template, hb);
fn toml\u进入\u hashbuilder)->rustache::hashbuilder(值:toml::Array,mut vb:rustache::VecBuilder{
对于v值{
比赛五{
toml::Value::String=>vb.push_字符串,
toml::Value::Integer(i)=>vb.push_int(i),
toml::Value::Float(f)=>vb.push_Float(f),
toml::Value::Boolean(b)=>vb.push_bool(b),
toml::Value::Datetime=>vb.push_字符串,
toml::Value::Array(arr)=>vb.push_vector(|vb | toml_)进入_vecbuilder(arr.clone(),vb)),
toml::Value::Table(tbl)=>vb.push_hash(| hb | toml_)进入_hashbuilder(tbl.clone(),hb))
}
}
vb
}
let值:toml::Table=Parser::new(input.parse().unwrap();
将hb=toml_放入_hashbuilder(值,rustache::hashbuilder::new());
让result=rustache::render_text(您的_模板,hb);

处理嵌套表和数组时会出现不幸的克隆-这是rustache中的一个结果。如果它被修复,
clone()
可以被删除,那么闭包应该被移动。

先生,你是一位绅士和学者。回答得好。我欠你的债。
x = [1, 2, 3]
y = [4, 5, 6]
fn toml_into_hashbuilder<'a>(value: toml::Table, mut hb: rustache::HashBuilder<'a>) -> rustache::HashBuilder<'a> {
    for (k, v) in value {
        match v {
            toml::Value::String(s) => hb.insert_string(k, s),
            toml::Value::Integer(i) => hb.insert_int(k, i),
            toml::Value::Float(f) => hb.insert_float(k, f),
            toml::Value::Boolean(b) => hb.insert_bool(k, b),
            toml::Value::Datetime(s) => hb.insert_string(k, s),
            toml::Value::Array(arr) => hb.insert_vector(k, |vb| toml_into_vecbuilder(arr.clone(), vb)),
            toml::Value::Table(tbl) => hb.insert_hash(k, |hb| toml_into_hashbuilder(tbl.clone(), hb))
        }
    }
    hb
}

fn toml_into_vecbuilder<'a>(value: toml::Array, mut vb: rustache::VecBuilder<'a>) -> rustache::VecBuilder<'a> {
    for v in value {
        match v {
            toml::Value::String(s) => vb.push_string(s),
            toml::Value::Integer(i) => vb.push_int(i),
            toml::Value::Float(f) => vb.push_float(f),
            toml::Value::Boolean(b) => vb.push_bool(b),
            toml::Value::Datetime(s) => vb.push_string(s),
            toml::Value::Array(arr) => vb.push_vector(|vb| toml_into_vecbuilder(arr.clone(), vb)),
            toml::Value::Table(tbl) => vb.push_hash(|hb| toml_into_hashbuilder(tbl.clone(), hb))
        }
    }
    vb
}

let value: toml::Table = Parser::new(input).parse().unwrap();
let hb = toml_into_hashbuilder(value, rustache::HashBuilder::new());
let result = rustache::render_text(your_template, hb);