我无法编译将整数转换为字符串类型的代码。我正在运行来自Rust for Rubyists教程 的示例,其中包含各种类型转换,例如:
"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`
to_str()
返回的是不同的值,我会使用to_string()
。 - user3358302&'static str
,即带有静态生命周期的字符串切片,使用动态创建的数据无法获得。你只能使用字符串字面值来创建它们。 - Vladimir Matveevto_str
方法让我感到困惑(正如你所说,他们为了清晰度而重命名),我以为它返回的是一个字符串切片,而不是一个String
对象。 - user3358302