我正在制作一个程序,计算用户输入的日期之间的天数之差。到目前为止,我能够使程序使用字符串文字,但不能使用字符串对象。
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.
如何将字符串对象转换为字符串文字或运行该程序的其他方法。
答案 0 :(得分:3)
问题是这样的:
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
是一个不同的变量(因此它可以具有不同的类型),但是它与前一个变量具有相同的名称,因此它“阴影化”了前一个变量(这意味着您无法引用它)的名字)。