gpt4 book ai didi

rust - 内置测试仪 'define' 未按预期工作

转载 作者:行者123 更新时间:2023-11-29 08:31:19 25 4
gpt4 key购买 nike

我有一个模板,其中有一个部分应该在定义变量时呈现。但我无法让它发挥作用。我的项目中有这样的东西:

use serde::Serialize;

#[derive(Default,Serialize)]
struct AStruct {
parameter: Option<String>
}

// AStruct { parameter: None }
let a_struct_instance = AStruct {..Default::default()};
TEMPLATES.render("template.conf", &a_struct_instance) {...}
// template.conf example:
//
// {% if parameter is defined %}
// SOMETHING TO SHOW
// {% endif %}

如果定义了参数,模板就会呈现,这是为什么呢?

参数字段为 None,但在内置测试器函数中,'value' 为 Some(Null) 并且 is_some() 在此返回 true。

https://github.com/Keats/tera/blob/master/src/builtins/testers.rs#L36

/// Returns true if `value` is defined. Otherwise, returns false.
pub fn defined(value: Option<Value>, params: Vec<Value>) -> Result<bool> {
number_args_allowed("defined", 0, params.len())?;
Ok(value.is_some())
}

我做错了什么?

最佳答案

好吧,我的问题是测试人员“已定义”或“未定义”检查变量是否已定义/未定义,而不是变量的值。

关于rust - 内置测试仪 'define' 未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56946523/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com