Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.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宏中将标识视为字符串_Rust_Rust Macros - Fatal编程技术网

在Rust宏中将标识视为字符串

在Rust宏中将标识视为字符串,rust,rust-macros,Rust,Rust Macros,我有一个配置结构,其中包含一些顶级属性,我想将这些属性放在各个部分中。为了提供弃用警告,我制作了以下宏 macro_rules! deprecated { ($config:expr, $old:ident, $section:ident, $new:ident) => { if $config.$old.is_some() { println!( "$old configuration option is deprecated. Renam

我有一个配置结构,其中包含一些顶级属性,我想将这些属性放在各个部分中。为了提供弃用警告,我制作了以下宏

macro_rules! deprecated {
($config:expr, $old:ident, $section:ident, $new:ident) => {
    if $config.$old.is_some() {
        println!(
            "$old configuration option is deprecated. Rename it to $new and put it under the section [$section]",
        );

        &$config.$old
    } else {
        if let Some(section) = &$config.$section {
            &section.$new
        } else {
            &None
        }
    }
};
}


这似乎不像预期的那样有效,因为宏参数在字符串中没有被替换。如何更改宏以达到所需的效果?

字符串化宏可以将一个元素转换为字符串文字,而
concat宏”可以将多个文字连接成单个字符串文字:

println!(
    concat!(
        stringify!($old),
        " configuration option is deprecated. Rename it to ",
        stringify!($new),
        " and put it under the section [",
        stringify!($section),
        "]",
    )
);

字符串化宏可以将一个元素转换为字符串文字,而
concat宏”可以将多个文字连接成单个字符串文字:

println!(
    concat!(
        stringify!($old),
        " configuration option is deprecated. Rename it to ",
        stringify!($new),
        " and put it under the section [",
        stringify!($section),
        "]",
    )
);

不应该是
println!(“{}配置选项已弃用。将其重命名为{},并将其放在节[{}]”,$old,$new,$section下)@AlexLarionov那也不行。我不想在
$config.$old
中打印struct节的值,我只是想将
$old
作为文本打印。它不应该是
println吗!(“{}配置选项已弃用。将其重命名为{},并将其放在节[{}]”,$old,$new,$section下)@AlexLarionov那也不行。我不想在
$config.$old
中打印struct节的值,我只是想将
$old
作为文本打印。