我有一个&[u8]
,我需要验证它是否符合某种模式。您可以在Regex
documentation和the module documentation中的&[u8]
上使用正则表达式。我从the examples section获取代码并将其放在main()
中并添加了一些声明:
extern crate regex;
use regex::Regex;
fn main() {
let re = Regex::new(r"'([^']+)'\s+\((\d{4})\)").unwrap();
let text = b"Not my favorite movie: 'Citizen Kane' (1941).";
let caps = re.captures(text).unwrap();
assert_eq!(&caps[1], &b"Citizen Kane"[..]);
assert_eq!(&caps[2], &b"1941"[..]);
assert_eq!(&caps[0], &b"'Citizen Kane' (1941)"[..]);
// You can also access the groups by index using the Index notation.
// Note that this will panic on an invalid index.
assert_eq!(&caps[1], b"Citizen Kane");
assert_eq!(&caps[2], b"1941");
assert_eq!(&caps[0], b"'Citizen Kane' (1941)");
}
我不明白这个示例代码与常规字符串匹配的区别,实际上编译器抱怨期望&str
。一般来说,代码没有暗示它与通常的字符串匹配有什么不同,我没有遇到任何问题。
我认为我做了一些基本的错误,比如丢失或更精确的导入。我在这里猜测游戏,因为文档无法提供工作示例(正如他们经常做的那样),而这次编译器也无法在正确的方向上推动我。
以下是编译器消息:
error[E0308]: mismatched types
--> src/main.rs:7:28
|
7 | let caps = re.captures(text).unwrap();
| ^^^^ expected str, found array of 45 elements
|
= note: expected type `&str`
found type `&[u8; 45]`
error[E0277]: the trait bound `str: std::cmp::PartialEq<[u8]>` is not satisfied
--> src/main.rs:8:5
|
8 | assert_eq!(&caps[1], &b"Citizen Kane"[..]);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't compare `str` with `[u8]`
|
= help: the trait `std::cmp::PartialEq<[u8]>` is not implemented for `str`
= note: required because of the requirements on the impl of `std::cmp::PartialEq<&[u8]>` for `&str`
= note: this error originates in a macro outside of the current crate
error[E0277]: the trait bound `str: std::cmp::PartialEq<[u8]>` is not satisfied
--> src/main.rs:9:5
|
9 | assert_eq!(&caps[2], &b"1941"[..]);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't compare `str` with `[u8]`
|
= help: the trait `std::cmp::PartialEq<[u8]>` is not implemented for `str`
= note: required because of the requirements on the impl of `std::cmp::PartialEq<&[u8]>` for `&str`
= note: this error originates in a macro outside of the current crate
error[E0277]: the trait bound `str: std::cmp::PartialEq<[u8]>` is not satisfied
--> src/main.rs:10:5
|
10 | assert_eq!(&caps[0], &b"'Citizen Kane' (1941)"[..]);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't compare `str` with `[u8]`
|
= help: the trait `std::cmp::PartialEq<[u8]>` is not implemented for `str`
= note: required because of the requirements on the impl of `std::cmp::PartialEq<&[u8]>` for `&str`
= note: this error originates in a macro outside of the current crate
error[E0277]: the trait bound `str: std::cmp::PartialEq<[u8; 12]>` is not satisfied
--> src/main.rs:13:5
|
13 | assert_eq!(&caps[1], b"Citizen Kane");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't compare `str` with `[u8; 12]`
|
= help: the trait `std::cmp::PartialEq<[u8; 12]>` is not implemented for `str`
= note: required because of the requirements on the impl of `std::cmp::PartialEq<&[u8; 12]>` for `&str`
= note: this error originates in a macro outside of the current crate
error[E0277]: the trait bound `str: std::cmp::PartialEq<[u8; 4]>` is not satisfied
--> src/main.rs:14:5
|
14 | assert_eq!(&caps[2], b"1941");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't compare `str` with `[u8; 4]`
|
= help: the trait `std::cmp::PartialEq<[u8; 4]>` is not implemented for `str`
= note: required because of the requirements on the impl of `std::cmp::PartialEq<&[u8; 4]>` for `&str`
= note: this error originates in a macro outside of the current crate
error[E0277]: the trait bound `str: std::cmp::PartialEq<[u8; 21]>` is not satisfied
--> src/main.rs:15:5
|
15 | assert_eq!(&caps[0], b"'Citizen Kane' (1941)");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't compare `str` with `[u8; 21]`
|
= help: the trait `std::cmp::PartialEq<[u8; 21]>` is not implemented for `str`
= note: required because of the requirements on the impl of `std::cmp::PartialEq<&[u8; 21]>` for `&str`
= note: this error originates in a macro outside of the current crate
答案 0 :(得分:1)
并添加了一些声明
不幸的是,你添加了错误的。请注意,您链接的文档如何用于结构 regex::bytes::Regex
,而不是regex::Regex
- 它们是两种不同的类型!
extern crate regex;
use regex::bytes::Regex;
// ^^^^^
fn main() {
let re = Regex::new(r"'([^']+)'\s+\((\d{4})\)").unwrap();
let text = b"Not my favorite movie: 'Citizen Kane' (1941).";
let caps = re.captures(text).unwrap();
assert_eq!(&caps[1], &b"Citizen Kane"[..]);
assert_eq!(&caps[2], &b"1941"[..]);
assert_eq!(&caps[0], &b"'Citizen Kane' (1941)"[..]);
assert_eq!(&caps[1], b"Citizen Kane");
assert_eq!(&caps[2], b"1941");
assert_eq!(&caps[0], b"'Citizen Kane' (1941)");
}
因为文档未能提供工作示例(正如他们经常做的那样)
请注意,文档中的代码块默认编译和执行,所以我的经验是,这些示例很少有用。