gpt4 book ai didi

string - 预期&str发现有 rust 的炭?

转载 作者:行者123 更新时间:2023-12-03 11:40:36 25 4
gpt4 key购买 nike

我收到此错误

expected &str, found 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);
}
}
}
我该如何解决?

最佳答案

list&str数组更改为char数组:

let list = ['c', 'h', 'p', 'u'];
双引号 ""创建字符串文字,而单引号 ''创建字 rune 字。请参阅Rust引用中的 Literal Expressions

关于string - 预期&str发现有 rust 的炭?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66255166/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com