string - 预期结构 `std::string::String`,找到结构 `std::str::Split`

标签 string rust

我正在制作一个程序来计算用户输入的日期天数之间的差异。到目前为止,我能够使程序使用字符串文字但不能使用字符串对象。

use std::io;

fn main() {
    let mut date_1 = "22/8/2019".split("/");
    let mut date_2 = "30/8/2019".split("/");
    let vec_1: Vec<&str> = date_1.collect();
    let vec_2: Vec<&str> = date_2.collect();
    println!("{:#?}", vec_1);
    println!("{:#?}", vec_2);
    let my_int_2 = vec_2[0].parse::<i32>().unwrap();
    let my_int_1 = vec_1[0].parse::<i32>().unwrap();
    let result = my_int_2 - my_int_1;
    println!("The difference between two dates is: {}", result);
}

输出:

[
    "22",
    "8",
    "2019",
]
[
    "30",
    "8",
    "2019",
]
The difference between two dates is: 8

我想询问用户的日期:

use std::io;

fn main() {
    let mut date_1 = String::new();
    println!("Enter a date in (dd/mm/yy) format: ");
    io::stdin()
        .read_line(&mut date_1)
        .ok()
        .expect("Couldn't read line");

    let mut date_2 = String::new();
    println!("Enter a date in (dd/mm/yy) format: ");
    io::stdin()
        .read_line(&mut date_2)
        .ok()
        .expect("Couldn't read line");
    date_1 = date_1.split("/");
    date_2 = date_2.split("/");
    let vec_1: Vec<&str> = date_1.collect();
    let vec_2: Vec<&str> = date_2.collect();
    println!("{:#?}", vec_1);
    println!("{:#?}", vec_2);
    let my_int_2 = vec_2[0].parse::<i32>().unwrap();
    let my_int_1 = vec_1[0].parse::<i32>().unwrap();
    let result = my_int_2 - my_int_1;
    println!("The difference between two dates is: {}", result);
}

输出:

   Compiling twentythree v0.1.0 (C:\Users\Muhammad.3992348\Desktop\rust\hackathon\twentythree)
error[E0308]: mismatched types
  --> src\main.rs:15:14
   |
15 |     date_1 = date_1.split("/");
   |              ^^^^^^^^^^^^^^^^^ expected struct `std::string::String`, found struct `std::str::Split`
   |
   = note: expected type `std::string::String`
              found type `std::str::Split<'_, &str>`

error[E0308]: mismatched types
  --> src\main.rs:16:14
   |
16 |     date_2 = date_2.split("/");
   |              ^^^^^^^^^^^^^^^^^ expected struct `std::string::String`, found struct `std::str::Split`
   |
   = note: expected type `std::string::String`
              found type `std::str::Split<'_, &str>`

error[E0599]: no method named `collect` found for type `std::string::String` in the current scope
  --> src\main.rs:19:35
   |
19 |     let vec_1: Vec<&str> = date_1.collect();
   |                                   ^^^^^^^
   |
   = note: the method `collect` exists but the following trait bounds were not satisfied:
           `&mut std::string::String : std::iter::Iterator`
           `&mut str : std::iter::Iterator`

error[E0599]: no method named `collect` found for type `std::string::String` in the current scope
  --> src\main.rs:20:35
   |
20 |     let vec_2: Vec<&str> = date_2.collect();
   |                                   ^^^^^^^
   |
   = note: the method `collect` exists but the following trait bounds were not satisfied:
           `&mut std::string::String : std::iter::Iterator`
           `&mut str : std::iter::Iterator`

error: aborting due to 4 previous errors

Some errors occurred: E0308, E0599.
For more information about an error, try `rustc --explain E0308`.
error: Could not compile `twentythree`.

To learn more, run the command again with --verbose.

如何将字符串对象转换为字符串文字或任何其他运行程序的 war 。

最佳答案

问题是这样的:

let mut date_1 = String::new(); /* type: std::string::String */
// ...
date_1 = date_1.split("/"); /* type: std::str::Split<'_, &str> */

您已经声明了一个具有一种类型 (String) 的变量,但您正试图为其分配另一种类型 (std::str::Split) .


解决此问题的一种“Rust-y”方法通常是重新声明一个具有相同名称的变量:

let date_1 = String::new(); /* type: std::string::String */
// ...
let date_1 = date_1.split("/"); /* type: std::str::Split<'_, &str> */

第二个 date_1 是一个不同的变量(因此它可以有不同的类型),但它与前一个变量同名,所以它“遮盖”了前一个变量(这意味着你不能不再按名称引用它)。

关于string - 预期结构 `std::string::String`,找到结构 `std::str::Split`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56312704/

相关文章:

c string 一个字符串内的多次替换

python - 在python中搜索包含通配符的字符串

c++ - 将符号插入字符串 C++

rust - 如何从 C 正确链接到 Rust 动态库?

java - 为什么索引越界了?

Python 查找所有匹配的子字符串模式并替换子字符串

Rust 支持 cookie 的 HTTP 客户端

unit-testing - 如何测试依赖于环境变量的 Rust 方法?

rust - 错误 : xxx does not live long enough

rust - 将包含字符串数组的 C 符号从 Rust 公开到 C