我收到这个错误
预期
&str,发现char
对于这段代码
// Expected output
// -------
// h exists
// c exists
fn main() {
let list = ["c","h","p","u"];
let s = "Hot and Cold".to_string();
let mut v: Vec<String> = Vec::new();
for i in s.split(" ") {
let c = i.chars().nth(0).unwrap().to_lowercase().nth(0).unwrap();
println!("{}", c);
if list.contains(&c) {
println!("{} exists", c);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?
list从 s 数组更改&str为 s 数组char:
let list = ['c', 'h', 'p', 'u'];
Run Code Online (Sandbox Code Playgroud)
双引号""创建字符串文字,而单引号''创建字符文字。请参阅Rust 参考中的文字表达式。
| 归档时间: |
|
| 查看次数: |
1180 次 |
| 最近记录: |