-
Rust❤️When iterating over `chars`, why is the item not `&char`?코딩Coding/★Rust★기초★Syntax 2022. 9. 28. 22:18728x90
When iterating over `chars`, why is the item not `&char`? It seems that `chars` is not a consuming iterator, and I tried this:
```
fn print_type_of<T>(_: T) {
println!("{}", std::any::type_name::<T>())
} fn main() {
let a = "123".to_string();
for b in a.chars() {
print_type_of(b);
}
let c = vec![1, 2, 3];
for d in c.iter() {
print_type_of(d);
}
for &e in c.iter() {
print_type_of(e);
}
}
```fn print_type_of<T>(_: T) { println!("{}", std::any::type_name::<T>()) } fn main() { let a = "123".to_string(); for b in a.chars() { print_type_of(b); } let c = vec![1, 2, 3]; for d in c.iter() { print_type_of(d); } for &e in c.iter() { print_type_of(e); } }
The result is:
```
char
char
char
&i32
&i32
&i32
i32
i32
i32
```
Why is `b` not `&char`?
반응형'코딩Coding > ★Rust★기초★Syntax' 카테고리의 다른 글
Rust's Most Important Containers 📦 10 Useful Patterns (0) 2022.10.23 I spent a year learning Rust to do a 5 minute PHP job (0) 2022.10.11 A quick Introduction to WASM-4 (0) 2022.10.04 Rust❤️`lifetime` problem when trying to imitate C++ code using refference (0) 2022.09.28 Rust❤️Why is char 4 bytes? (0) 2022.09.28 Rust 1.61 한글 설명서❤️영문이 강제 번역 된러가 한글이 좀 이상하다. 감안해서 보자 (0) 2022.09.24 Rust ready 매크로에 관해서 (0) 2022.09.24 Rust❤️Why to use “mut” twice here? (0) 2022.08.28