gpt4 book ai didi

rust - 使用不推荐使用的项目 'std::ascii::AsciiExt' : use inherent methods instead

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

我写了这个

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字母。我应该怎么做才能将它们转换为小写?
感谢您回答这个问题!这是代码的链接。
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=7612f6a78520ec3f1457454f0e5cf36f

最佳答案

消息指出时,请使用固有方法(例如 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;
}
}

关于rust - 使用不推荐使用的项目 'std::ascii::AsciiExt' : use inherent methods instead,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64340804/

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