我试图理解 Rust 中的泛型。我无法理解这里出了什么问题
fn say<T>(msg: &T) {
let slen = msg.chars().count();
if slen > 0 {
println!("Char Count {} " ,slen);
}
}
fn main() {
let msg = String::from("Hello World from Rust!!!");
say::<String>(&msg);
}
编译错误:
error[E0599]: no method named `chars` found for reference `&T` in the current scope
--> hell.rs:4:20
|
4 | let slen = msg.chars().count();
| ^^^^^ method not found in `&T`