我试着将字符串的一个片段与&'static str
进行比较:
impl Tag for BoldTag {
fn is_this_tag(text_slice: &str) -> bool {
text_slice.len() >= 2 && text_slice[0..=1] == "**"
}
}
这将显示以下错误消息:
error[E0277]: can't compare `str` with `&str`
--> src/lib.rs:52:52
|
52 | text_slice.len() >= 2 && text_slice[0..=1] == "**"
| ^^ no implementation for `str == &str`
|
= help: the trait `PartialEq<&str>` is not implemented for `str`
= help: the following other types implement trait `PartialEq<Rhs>`:
<&'a str as PartialEq<OsString>>
<&'a str as PartialEq<String>>
<&'b str as PartialEq<Cow<'a, str>>>
<String as PartialEq<&'a str>>
<String as PartialEq<Cow<'a, str>>>
<String as PartialEq<str>>
<String as PartialEq>
<str as PartialEq<Cow<'a, str>>>
and 4 others
我知道不可能比较str
和&str
,但我不太明白&str
的切片如何变成str
(而不是&&str
),也不知道编写此函数的正确方法是什么。rustc --explain E0277
给出的解释是通用的,“您试图使用一个类型,该类型没有在预期该特性的地方实现该特性”,这并没有真正告诉我如何解决这个特定的问题。
1条答案
按热度按时间cgvd09ve1#
尝试在对字符串进行切片之前添加一个
&
现在您正在比较
&str
和&str