从函数

时间:2018-01-29 19:56:16

标签: function hashmap rust return-type

我已经构建了一个基本功能,可以从配置文件中加载应用程序设置。这些设置被解析并插入到HashMap中,以便稍后我可以参考它们。我似乎无法找到正确的方法来返回我的HashMap及其包含的数据:

use std::collections::HashMap;
use std::fs::File;
use std::io::BufReader;

fn load_config_files(settings: &str) -> HashMap {
    println!("Loading configuration file...");

    let mut settings_data = HashMap::new();

    //function returns a vector with the application settings...
    let handle = File::open(settings).unwrap();

    for line in BufReader::new(handle).lines() {
        let mut line_data = &line.unwrap();

        //get the first charater, if its a # then the line is ignored... e.g. comments
        let first_character = &line_data[0..1];
        if first_character != "#" {
            //sort into a vector
            let mut settings_info_split = line_data.split("=");
            let settings_info = settings_info_split.collect::<Vec<&str>>();

            //need to add the values to the "settings_vector"
            settings_data.insert(settings_info[0], settings_info[1]);
        }
    }

    return settings_data;
}

这不起作用:

error[E0243]: wrong number of type arguments: expected at least 2, found 0
 --> src/main.rs:5:41
  |
5 | fn load_config_files(settings: &str) -> HashMap {
  |                                         ^^^^^^^ expected at least 2 type arguments

我已尝试将其后的参数包括在内,但我找不到合适的参数......例如它不喜欢&str,它不喜欢String

0 个答案:

没有答案