文本文件解析功能由于类型不匹配错误而无法编译 [英] Textfile-parsing function fails to compile owing to type-mismatch error
问题描述
我正在尝试解析一个简单的配置文本文件,该文件每行包含一个三个单词的条目,布局如下:
I'm trying to parse a simple config text file, which contains one three-word entry per line, laid out as follows:
ITEM name value
ITEM name value
//etc.
我已经在此处复制了进行解析(以及随后的编译错误)的函数(并且在Rust Playpen上):
I've reproduced the function which does the parsing (and the subsequent compilation error) here (and on the Rust Playpen):
pub fn parse(path: &Path) -> config_struct {
let file = File::open(&path).unwrap();
let reader = BufReader::new(&file);
let line_iterator = reader.lines();
let mut connection_map = HashMap::new();
let mut target_map = HashMap::new();
for line in line_iterator {
let line_slice = line.unwrap();
let word_vector: Vec<&str> = line_slice.split_whitespace().collect();
if word_vector.len() != 3 { continue; }
match word_vector[0] {
"CONNECTION" => connection_map.insert(word_vector[1], word_vector[2]),
"TARGET" => target_map.insert(word_vector[1], word_vector[2]),
_ => continue,
}
}
config_struct { connections: connection_map, targets: target_map }
}
pub struct config_struct<'a> {
// <name, value>
connections: HashMap<&'a str, &'a str>,
// <name, value>
targets: HashMap<&'a str, &'a str>,
}
src/parse_conf_file.rs:23:3: 27:4 error: mismatched types:
expected `()`,
found `core::option::Option<&str>`
(expected (),
found enum `core::option::Option`) [E0308]
src/parse_conf_file.rs:23 match word_vector[0] {
src/parse_conf_file.rs:24 "CONNECTION" => connection_map.insert(word_vector[1], word_vector[2]),
src/parse_conf_file.rs:25 "TARGET" => target_map.insert(word_vector[1], word_vector[2]),
src/parse_conf_file.rs:26 _ => continue,
src/parse_conf_file.rs:27 }
从本质上讲,我似乎已经创建了一个match
语句,该语句期望一个空的元组,并且还找到要包装在Option
中的Vec<&str>
的内容!
In essence, I seem to have created a match
statement that expects an empty tuple, and also finds the contents of a Vec<&str>
to be wrapped in an Option
!
NB.这篇文章最初包含两个问题(我认为这是一个错误地表示了自己的错误),但是根据评论中的建议,我将其分为两个单独的帖子.后面的帖子是此处.
NB. This post originally contained two questions (that I'd believed were one error manifesting itself differently), but as-per advice in the comments I've split it into two separate posts. The latter post is here.
推荐答案
您的原始问题只是在循环主体的末尾有一个非()
表达式.您的match
表达式具有类型Option<&str>
(因为这是HashMap::insert
的返回类型),而不是类型()
.只需在匹配表达式后加上分号即可解决此问题:
Your original problem is just that you have a non-()
expression at the end of your loop body. Your match
expression has type Option<&str>
(because that is the return type of HashMap::insert
), not type ()
. This problem is solved by simply putting a semicolon after the match expression:
match word_vector[0] {
"CONNECTION" => connection_map.insert(word_vector[1], word_vector[2]),
"TARGET" => target_map.insert(word_vector[1], word_vector[2]),
_ => continue,
};
对于后者,不是用不指向line_slice的自有对象填充word_vector吗?
For the latter, isn't word_vector populated with owned objects that don't point to line_slice?
不,这正是问题所在. word_vector
包含类型为&str
的元素,即借用的字符串.它们指向line_slice
,仅在当前循环迭代结束之前有效.在将它们插入地图之前,您可能希望将它们转换为String
s(使用String::from
).
No, which is precisely the issue. word_vector
contains elements of type &str
, i.e. borrowed strings. These point into line_slice
, which only lives until the end of the current loop iteration. You probably want to convert them to String
s (using String::from
) before inserting them into the map.
这篇关于文本文件解析功能由于类型不匹配错误而无法编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!