[英]use of deprecated item 'std::ascii::AsciiExt': use inherent methods instead
我寫了這個
let somestring = "hello happy haddock hahaha".to_string();
let mut freq: HashMap<char, u32> = HashMap::new();
for c in somestring.chars() {
let c = AsciiExt::to_ascii_lowercase(&c);
*freq.entry(c).or_insert(0) += 1;
}
然后我得到了這個
warning: use of deprecated item 'std::ascii::AsciiExt': use inherent methods instead
--> src/main.rs:2:5
|
2 | use std::ascii::AsciiExt;
| ^^^^^^^^^^^^^^^^^^^^
|
= note: `#[warn(deprecated)]` on by default
warning: use of deprecated item 'std::ascii::AsciiExt::to_ascii_lowercase': use inherent methods instead
--> src/main.rs:8:11
|
8 | let c1 = AsciiExt::to_ascii_lowercase(&c);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
以下轉換在沒有警告的情況下工作,但比to_ascii_lowercase()
慢得多。
let c = c.to_lowercase().collect::<Vec<_>>()[0];
我在somestring
變量中只有 ASCII 字母。 我應該怎么做才能將它們轉換為小寫?
感謝您回答這個問題! 這是代碼的鏈接。
正如消息所述,使用固有方法(如char::to_ascii_lowercase
而不是 trait 方法:
use std::collections::HashMap;
fn main() {
let somestring = "hello happy haddock hahaha".to_string();
let mut freq: HashMap<char, u32> = HashMap::new();
for c in somestring.chars() {
let c = c.to_ascii_lowercase();
*freq.entry(c).or_insert(0) += 1;
}
}
刪除use std::ascii:AsciiExt;
線。
str::to_ascii_lowercase
自 v1.23.0 以來一直穩定。
您可以使用char::to_ascii_lowercase
並利用somestring.chars()
返回迭代器的事實:
use std::collections::HashMap;
fn main() {
let somestring = "hello happy haddock hahaha".to_string();
let mut freq: HashMap<char, u32> = HashMap::new();
for c in somestring.chars().map(|ch: char| ch.to_ascii_lowercase()) {
*freq.entry(c).or_insert(0) += 1;
}
println!("{:?}", freq)
}
輸出:
{'a': 5, 'y': 1, 'l': 2, 'h': 6, 'e': 1, 'o': 2, 'c': 1, ' ': 3, 'p': 2, 'd': 2, 'k': 1}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.