- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有点困惑为什么取消引用 &&str
在第二种情况下似乎不起作用:
use std::collections::HashSet;
fn main() {
let days = vec!["mon", "tue", "wed"];
let mut hs: HashSet<String> = HashSet::new();
for d in &days {
// works
hs.insert(String::from(*d));
// doesn't
hs.insert(*d.to_string());
}
println!("{:#?}", hs);
}
str
确实实现了 ToString
特征,但它仍然给我错误:
error[E0308]: mismatched types
--> src/main.rs:12:19
|
12 | hs.insert(*d.to_string());
| ^^^^^^^^^^^^^^ expected struct `std::string::String`, found str
|
= note: expected type `std::string::String`
found type `str`
我在这里弄错了什么语法?
最佳答案
to_string
被称为 d
在它被取消引用之前,所以你将取消引用 String
,结果为 str
.
改成
hs.insert(d.to_string());
这是有效的,因为 d
自动取消引用为 str
, 将转换为 String
然后。这叫做 Deref coercions .
If you have a type
U
, and it implementsDeref<Target=T>
, values of&U
will automatically coerce to a&T
...
Deref will also kick in when calling a method
这是 exactly the case here : impl Deref<Target = str> for String
.参见 here for an example :
A value of type
&&&&&&&&&&&&&&&&Foo
can still have methods defined onFoo
called, because the compiler will insert as many*
operations as necessary to get it right. And since it’s inserting*s
, that usesDeref
.
这example证明了这一点:
struct Foo;
impl Foo {
fn foo(&self) { println!("Foo"); }
}
let f = &&Foo;
// prints "foo"
f.foo();
顺便说一下,
hs.insert((*d).to_string());
还将work , 因为它首先被取消引用为 &str
.
关于rust - 为什么 String::from(*d) 在 &&str 上给出的结果与 *d.to_string() 不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49059545/
C++ 没有办法获取枚举的字符串表示形式。人们通过编写包含大量样板代码的自定义函数来解决这个问题 switch with case XYZ return "XYZ"; 这当然需要枚举的用户知道自定义函
boost::to_string 的目的是什么? (在 boost/exception/to_string.hpp 中找到)以及它与 boost::lexical_cast 有何不同和 std::to
我在编译代码时遇到了问题。我想使用 std::to_string,因为它是一种方便的将 int 转换为字符串的方式。但是,我不断收到“'to_string' 不是'std' 的成员”,所以这似乎是一个
我想知道为我自己的类实现字符串转换的最佳方法是什么。我进行了广泛的搜索(并查阅了一些书籍),但令人惊讶的是我没有在任何地方找到任何建议。在我看来,选项是: 转换为字符串的内部类定义:operator
美好的一天 我找遍了高处和低处,但没有解决方案。 IDE:NetBeans IDE 8.2MinGW:使用“最新的”MinGW 编译器。 原始问题: 我想将一个整数转换为字符串。 使用 this方法,
我正在研究 rust book .在字符串章节中,很多示例使用了 to_string() ,我的编译器版本(rustc 1.0.0-dev)给出了以下警告 strings.rs:3:23: 3:34
我有以下泛型函数,现在想尽可能将任何给定类型转换为字符串。 fn write_row(row: T) { let s: String = row.to_string(); } 但这显然行不通,
我正在尝试获取 double 的整数部分和小数部分。这就是我实现它的方式: void func(double d) { int intpart, decpart; intpart =
对于一个作业,我应该在头文件 'date.h' 中使用以下类,但是当我试图编译我的程序时,我得到一个错误说明: 'to_string': unknown override specifier. 下面的
我刚刚知道 C++ 有一个 std::to_string() 在 中定义.现在我想知道为什么 to_string()仅适用于数字类型。有什么特别的原因,为什么没有更一般的 template std
我试图让 to_string(NUMBER) 函数在我的 Ubuntu 计算机上运行数周,但它在 QT 环境或其他任何地方都无法运行。我的代码在我的 Mac osx 上完美运行,但是当我尝试在 Ubu
我是 Rust 新手。在尝试连接字符串文字和整数时,由于“to_string”函数,我遇到了许多错误。之后意识到我需要在整数前面放置一个引用/与号(&)。但是,我不明白为什么。你能解释一下为什么我需要
我有一个数据框,里面有文本值和int。我应用 df.to_string() 没有问题,然后调用 get_value() 函数并消除开头和结尾可能的空格: df.get_value(index,colu
与我使用 VS2010 的主题完全一样: 我有: std::string s = std::to_string(42); 错误 Error 4 error C2668: 'std::to_st
使用下面的代码, int main() { for(;;) { float d; cin >> d; cout << std::to_strin
我正在尝试使用格式化消息返回错误。 let msg = format!("wrong num of dimensions: {}", dimensions.len()).to_string(); re
我正在尝试读取格式如下的文件: ruby 2.6.2 elixir 1.8.3 然后像这样伪代码转换成二维数组: [ ["ruby", "2.6.2"] ["elixir", "1.8.3"]
我正在尝试创建实现 to_string() 的 Error 枚举。我已经尝试为他们derive(Debug),但这似乎还不够。 这是我正在处理的枚举: #[derive(Debug, Clone)]
为什么 std::to_string 函数不是 std::string 类的方法?因为它在逻辑上更合适,不是吗?我想知道他们为什么不成功: std::string 类的静态成员函数 为什么他们不将它与
我有两个文件:error.h 和 error.cpp。编译 g++ -std=c++0x 给我一个错误: error.cpp:9:33:**call of overloaded "to_strin
我是一名优秀的程序员,十分优秀!