作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在遍历 HashMap
并通过一些本地变量中的模式匹配将值放入其中。
委托(delegate)者
fn lyrics_no_bottles(song_template:&mut String){
let mut template_partials = HashMap::new();
template_partials.insert("start", "No more bottles");
template_partials.insert("repeat", "no more bottles");
template_partials.insert("remaining", "99 bottles");
template_partials.insert("message", "Go to the store and buy some more");
resolve_template(song_template, template_partials);
}
调用
fn resolve_template(song_template:&mut String, partials: HashMap<&str, &str>){
let start:&str;
let repeat:&str;
let remaining:&str;
let message:&str;
for key in partials.keys(){
match key {
"start" => start = partials.get(key),
"repeat" => repeat = partials.get(key),
"remaining" => remaining = partials.get(key),
"message" => message = partials.get(key)
}
}
*song_template = song_template.replace("%1", start);
*song_template = song_template.replace("%2", repeat);
*song_template = song_template.replace("%3", message);
*song_template = song_template.replace("%4", remaining);
}
错误输出
lib.rs:51:5: 58:6 error: type mismatch resolving `<std::collections::hash::map::Keys<'_, &str, &str> as core::iter::Iterator>::Item == &str`:
expected &-ptr,
found str [E0271]
lib.rs:51 for key in partials.keys(){
lib.rs:52 match key {
lib.rs:53 "start" => start = partials.get(key),
lib.rs:54 "repeat" => repeat = partials.get(key),
lib.rs:55 "remaining" => remaining = partials.get(key),
lib.rs:56 "message" => message = partials.get(key)
...
lib.rs:51:5: 58:6 help: run `rustc --explain E0271` to see a detailed explanation
lib.rs:53:32: 53:49 error: mismatched types:
expected `&str`,
found `core::option::Option<&&str>`
(expected &-ptr,
found enum `core::option::Option`) [E0308]
lib.rs:53 "start" => start = partials.get(key),
我不明白为什么编译器认为有一个 &&str
当我将参数声明为 HashMap<&str,&str>
时.
最佳答案
因为 that particular method returns a reference wrapped by an Option .
基本上,如果您存储 String
在那里使用 get
.. 你会得到一个 Option<&String>
作为调用 get
的结果.存储&str
会给你一个 Option<&&str>
作为调用 get
的结果.
值的示例以及调用 get
时它们的显示方式:
String -> Option<&String>
&str -> Option<&&str>
u32 -> Option<&u32>
&str
的所有权当您将它作为值插入时,它被传输到 HashMap 中。因此,hashmap 使用 get
为您返回对它们的引用。 ... 或 mutable references using get_mut .
Playground 目前已损坏(无法分享任何内容)...所以这里是一个有效的..分解的代码示例:
use std::collections::HashMap;
fn main() {
let mut map: HashMap<&str, &str> = HashMap::new();
map.insert("start", "value");
let mut start: &str = "Unknown";
let mut repeat: &str;
let mut remaining: &str;
let mut message: &str;
for key in map.keys() {
match &key[..] {
"start" => start = map.get(key).unwrap(),
"repeat" => repeat = map.get(key).unwrap(),
"remaining" => remaining = map.get(key).unwrap(),
"message" => message = map.get(key).unwrap(),
_ => unreachable!()
}
}
println!("Result: {}", start);
}
关于string - 为什么遍历 HashMap<&str,&str> 会产生 &&str?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37936058/
我是一名优秀的程序员,十分优秀!