gpt4 book ai didi

string - 与 `char::is_ascii_alphanumeric` 匹配的字符串失败,而 `char::is_alphanumeric` 编译

转载 作者:行者123 更新时间:2023-11-29 08:04:36 25 4
gpt4 key购买 nike

我只想匹配字符串的字母数字 ascii 字符,但 matches 函数仅适用于 is_alphanumeric。下面的例子应该清楚。我也放在了Rust playground , 以便于重现。

fn main() {
"s".matches(char::is_alphanumeric).collect(); // Works
"s".matches(char::is_ascii_alphanumeric).collect(); // Doesn't work
}

产生了两个错误,我认为第二个错误取决于第一个错误。错误消息指出:

error[E0631]: type mismatch in function arguments
--> src/main.rs:3:9
|
3 | "s".matches(char::is_ascii_alphanumeric).collect(); // Doesn't work
| ^^^^^^^
| |
| expected signature of `fn(char) -> _`
| found signature of `for<'r> fn(&'r char) -> _`
|
= note: required because of the requirements on the impl of `std::str::pattern::Pattern<'_>` for `for<'r> fn(&'r char) -> bool {std::char::methods::<impl char>::is_ascii_alphanumeric}`

error[E0599]: no method named `collect` found for type `std::str::Matches<'_, for<'r> fn(&'r char) -> bool {std::char::methods::<impl char>::is_ascii_alphanumeric}>` in the current scope
--> src/main.rs:3:46
|
3 | "s".matches(char::is_ascii_alphanumeric).collect(); // Doesn't work
| ^^^^^^^
|
= note: the method `collect` exists but the following trait bounds were not satisfied:
`&mut std::str::Matches<'_, for<'r> fn(&'r char) -> bool {std::char::methods::<impl char>::is_ascii_alphanumeric}> : std::iter::Iterator`
`std::str::Matches<'_, for<'r> fn(&'r char) -> bool {std::char::methods::<impl char>::is_ascii_alphanumeric}> : std::iter::Iterator`

谁能给我解释一下,这个错误是什么意思,为什么它适用于一个功能而不适用于另一个功能?我查看了这两个函数的类型签名,但它们在我看来是一样的:

我使用 Rust v1.37.0

最佳答案

why it works for one function but not the other?

让我们看一下签名:

fn       is_alphanumeric( self) -> bool
fn is_ascii_alphanumeric(&self) -> bool

一个函数需要&self (一个引用)另一个需要self (按值)作为参数。在这种情况下,这是一个重要的区别。现在让我们再次检查错误:

error[E0631]: type mismatch in function arguments
--> src/main.rs:3:9
|
3 | "s".matches(char::is_ascii_alphanumeric).collect(); // Doesn't work
| ^^^^^^^
| |
| expected signature of `fn(char) -> _`
| found signature of `for<'r> fn(&'r char) -> _`

特别是最后一部分(由我对齐):

  |         expected signature of `        fn(    char) -> _`
| found signature of `for<'r> fn(&'r char) -> _`

它表明一个函数取 char正如预期的那样,但是一个函数采用 &'r char因为发现了争论。忽略 for<'r>'r在这里,它们在这种情况下并不重要。

为什么是fn(char)预期的?嗯,看着 str::matches 它表明它接受任何实现 Pattern 的东西.在 Pattern 的实现者中,我们发现:

impl<'a, F> Pattern<'a> for F
where
F: FnMut(char) -> bool,

这就是为什么函数采用 char 的原因按值而不是按引用是预期的。

如何解决?您始终可以提供一个闭包,并只使用引用的值调用该方法。这可能没有任何运行时开销,因为闭包可以很容易地被优化器内联。

"s".matches(|c| char::is_ascii_alphanumeric(&c))

一些额外的事情:

  • 第二个错误消息只是第一个错误消息的结果。它在应用修复后消失。
  • 您仍然需要指定要将迭代器收集到的集合的类型。例如Vec<_> :
    let x: Vec<_> = "s".matches(|c| char::is_ascii_alphanumeric(&c)).collect();
  • 为什么char的这两个方法有不同的签名?!这看起来很奇怪,确实占用了self按值(value)将是两者的首选。巧合的是我加了is_ascii_alphanumericchar直接在this PR .这不一致是 brought up我解释了这种奇怪的原因here .

    原因很简单,该方法刚刚从旧的 AsciiExt 中移出。特征。现在不赞成使用此特性。可悲的是,所讨论的方法已经稳定为采用&self。 .将方法移动到 char很好,但是添加签名会破坏代码。所以 &self签名被保留。简短回答:向后兼容性。我还找到了this issue有人尝试了与您几乎相同的尝试。

关于string - 与 `char::is_ascii_alphanumeric` 匹配的字符串失败,而 `char::is_alphanumeric` 编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57835391/

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