Ich habe eine &[u8]
und ich muss überprüfen, ob es mit einem Muster übereinstimmt. Es gibt Beispiele für die Verwendung von Regexen unter &[u8]
in der Regex
documentation und in the module documentation. Ich habe den Code aus the examples section und legte es in einem main()
und fügte ein paar Erklärungen:Wie verwende ich Rust Regex für Bytes (Vec <u8> oder & [u8])?
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)");
}
Ich verstehe nicht, wie das Beispiel-Code unterscheidet sich von regulären String-Matching, und in der Tat der Compiler beschwert sich über eine &str
erwartet. Im Allgemeinen deutet der Code nicht darauf hin, wie er sich vom üblichen String Matching unterscheidet, mit dem ich keine Probleme habe.
Ich nehme an, dass ich etwas grundlegend falsch gemacht habe, wie einen fehlenden oder präziseren Import. Ich bin hier in einem Ratespiel, da die Docs keine Arbeitsbeispiele liefern (wie sie es regelmäßig tun), und dieses Mal bringt mich der Compiler auch nicht in die richtige Richtung.
Hier sind die Compiler-Meldungen:
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
Danke. Es ist das erste Mal, dass ich diesen "verschachtelten Import" sehe, ich dachte, Regex :: Regex importiert alles in diesem Modul. Ich benutze sowohl & [u8] als auch Strings, deshalb musste ich die u8-Version mit "regex :: bytes :: Regex als Regex_u8;" umbenennen. – user103185
@ user103185 'verwendung regex :: Regex' bringt nur den Typ' regex :: Regex' ein. 'use regex :: *' würde alle Typen/Eigenschaften im 'regex' Modul (nicht-rekursiv) enthalten. Du könntest auch sagen 'benutze regex :: bytes; bytes :: Regex :: new() ', etc. – Shepmaster
Ich befürchte, dass die Docs, als ein Standard-Feature, die Klauseln" extern "und" use "weglassen, so dass sie nicht kompiliert werden. Das Vorschlagen, dass ein Beispiel funktioniert, weil alle Beispiele kompiliert und verifiziert sind (wie ich bereits wusste), hilft Neulingen noch mehr zu verwirren. Da Module und Importe für die Verwendung von Rust entscheidend sind, wäre es sehr nützlich, wenn jede Probe diese Klauseln für die tägliche Praxis enthält. Aber wenn nicht, würde zumindest ein Link zu einer Seite, der zeigt, wie man ein Beispiel kompiliert und ausführt, sehr geschätzt. Einmal einen Dank für die Klarstellung des Imports. – user103185