Muhammad Naufil
Muhammad Naufil

Reputation: 2647

expected struct `std::string::String`, found struct `std::str::Split`

I am making a program which calculates the difference between number of days in date, input by the user. So far I am able to make the program work with string literal but not with the string object.

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);
}

Output:

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

I want ask the date from user:

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);
}

Output:

   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.

How to convert string object into string literal or any other war around, which runs the program.

Upvotes: 1

Views: 8716

Answers (1)

Freyja
Freyja

Reputation: 40894

The problem is this:

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

You've declared a variable with one type (String) but you're trying to assign it with a different type (std::str::Split).


One "Rust-y" way of solving this is typically to redeclare a variable with the same name:

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

The second date_1 is a different variable (hence it can have a different type), but it has the same name as the former variable, so it "shadows" the former variable (meaning you cannot refer to it by name anymore).

Upvotes: 4

Related Questions