首页 > 解决方案 > vec 拼接预期字符串找到 &str

问题描述

我正在尝试向 vec 添加几个条目,但我不断收到不匹配的类型:

pub fn create_lib_file(name: &str, targets: Vec<&str>) -> String {
    let mut my_vec = vec![
        "[lib]",
        "",
        "",
        "[files]",
        "",
        "",
        "[general]",
        "",
    ];

    // The targets are just strings passed from the CLI command.
    for t in targets {
        match t {
            "one" => {
                let file_name = format!("user/test/{}.dll", name);
                let slice = &[file_name];
                my_vec.splice(2..2, slice.iter().cloned());
            }
        }
    }

    return my_vec.join("\n");
}

这给了我一个错误:

error[E0271]: type mismatch resolving `<std::slice::Iter<'_, std::string::String> as std::iter::Iterator>::Item == &&str`
  --> src/lib.rs:19:24
   |
19 |                 my_vec.splice(2..2, slice.iter().cloned());
   |                        ^^^^^^ expected struct `std::string::String`, found `&str`
   |
   = note: expected reference `&std::string::String`
              found reference `&&str`
   = note: required because of the requirements on the impl of `std::iter::Iterator` for `std::iter::Cloned<std::slice::Iter<'_, std::string::String>>`

我认为这与file_name我尝试使用的有关to_owned()to_string()但到目前为止我没有尝试将其转换file_nameString有效。

标签: rust

解决方案


推荐阅读