什么是“标准"?连接字符串的方法? [英] What is the "standard" way to concatenate strings?

查看:58
本文介绍了什么是“标准"?连接字符串的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然我基本上了解 strstd::string::String 是什么以及它们如何相互关联,但我发现组合字符串有点麻烦无需花费太多时间和思考即可完成各个部分.所以像往常一样,我怀疑我还没有看到正确的方法,这让它变得直观和轻而易举.

While I understand basically what str and std::string::String are and how they relate to each other, I find it a bit cumbersome to compose strings out of various parts without spending too much time and thought on it. So as usual I suspect I did not see the proper way to do it yet, which makes it intuitive and a breeze.

let mut s = std::string::String::with_capacity(200);
let precTimeToJSON = | pt : prectime::PrecTime, isLast : bool | {
    s.push_str(
        "{ \"sec\": " 
       + &(pt.sec.to_string()) 
       + " \"usec\": " 
       + &(pt.usec.to_string()) 
       + if isLast {"}"} else {"},"})
    };    

上面的代码被编译器认可,并带有如下错误消息:

The code above is honored by the compiler with error messages like:

src\main.rs:25:20: 25:33 错误:二进制操作 + 不能应用于类型 &'static str [E0369]

src\main.rs:25:20: 25:33 error: binary operation + cannot be applied to type &'static str [E0369]

即使经过半小时的摆弄和随机添加 &,我也无法使其可编译.所以,这里是我的问题:

And even after half an hours worth of fiddling and randomly adding &, I could not make this compilable. So, here my questions:

  • 我必须写什么才能达到显而易见的目的?
  • 在 Rust 中执行此操作的标准"方法是什么?

推荐答案

Rust 编译器是对的(当然):没有用于字符串文字的 + 运算符.

The Rust compiler is right (of course): there's no + operator for string literals.

我相信 format!() 是做你想做的事情的惯用方式.它使用 std::fmt 语法,它主要由格式化字符串和格式化参数组成(a la C 的 printf).对于您的示例,它看起来像这样:

I believe the format!() macro is the idiomatic way to do what you're trying to do. It uses the std::fmt syntax, which essentially consists of a formatting string and the arguments to format (a la C's printf). For your example, it would look something like this:

let mut s: String = String::new();
let precTimeToJSON = | pt : prectime::PrecTime, isLast : bool | {
    s = format!("{{ \"sec\": {} \"usec\": {} }}{}",
        pt.sec,
        pt.usec,
        if isLast { "" } else { "," }
    )
};

因为它是一个宏,所以你可以在参数列表中自由混合类型,只要类型实现了std::fmt::Display trait(适用于所有内置类型).此外,您必须转义文字 {} 作为 >{{}} 分别.最后,请注意格式字符串必须是字符串文字,因为宏会解析它,并且扩展后的代码看起来与原始的 format! 表达式完全不同.

Because it's a macro, you can intermix types in the argument list freely, so long as the type implements the std::fmt::Display trait (which is true for all built-in types). Also, you must escape literal { and } as {{ and }}, respectively. Last, note that the format string must be a string literal, because the macro parses it and the expanded code looks nothing like the original format! expression.

这是上述示例的游乐场链接.

再给你两分.首先,如果您正在阅读和编写 JSON,请查看一个库,例如 rustc-serialize.不那么痛苦了!

Two more points for you. First, if you're reading and writing JSON, have a look at a library such as rustc-serialize. It's much less painful!

第二,如果您只想连接 &'static str 字符串(即字符串文字),您可以使用 concat!().它不会对您的上述情况有所帮助,但可能对其他类似情况有所帮助.

Second, if you just want to concatenate &'static str strings (that is, string literals), you can do that with zero run-time cost with the concat!() macro. It won't help you in your case above, but it might with other similar ones.

这篇关于什么是“标准"?连接字符串的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆