【发布时间】:2014-09-19 08:41:00
【问题描述】:
我无法编译将类型从整数转换为字符串的代码。我正在运行来自Rust for Rubyists tutorial 的示例,该示例具有各种类型转换,例如:
"Fizz".to_str() 和 num.to_str()(其中 num 是一个整数)。
我认为这些to_str() 函数调用中的大多数(如果不是全部)已被弃用。当前将整数转换为字符串的方法是什么?
我得到的错误是:
error: type `&'static str` does not implement any method in scope named `to_str`
error: type `int` does not implement any method in scope named `to_str`
【问题讨论】:
-
对不起,如果我不关注,但我已经尝试查找 int 源,它似乎使用 doc.rust-lang.org/0.11.0/std/num/strconv/index.html 但这只是返回一个字节向量。此外还有
to_string()方法,但它返回String而不是文字字符串。 -
哈哈没关系,我以为
to_str()是一个不同的返回值,我会用to_string() -
@user3358302,没有方法可以返回你称之为“文字字符串”的东西,除非它们确实返回静态已知的文字,因为这些值的类型为
&'static str,即具有静态生命周期的字符串切片,这是使用动态创建的数据无法获得的。您只能使用字符串文字来创建它们。 -
很高兴知道!我认为
to_str方法让我感到困惑(正如您所说,为了清楚起见,他们重命名)认为它返回的是字符串切片而不是String对象。
标签: string int type-conversion rust