Beginner here. I'm trying to impl FromStr
for my Cedict
struct and FromStr
is giving me a lot of issues with lifetimes. I'd really like to understand why, but so far I've skated by on Clippy's recommendations to get things to compile. Here's the relevant code:
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct CedictEntry<'a> {
pub traditional: &'a str,
pub simplified: &'a str,
pub pinyin: Option<Vec<Syllable<'a>>>,
pub jyutping: Option<Vec<Syllable<'a>>>,
pub definitions: Option<Vec<&'a str>>,
}
#[derive(Debug, Clone)]
pub struct Cedict<'a> {
pub entries: Vec<CedictEntry<'a>>,
}
impl FromStr for Cedict {
type Err = CedictEntryError;
fn from_str(cedict_entries: &str) -> Result<Cedict, CedictEntryError> {
let entries: Vec<CedictEntry> = cedict_entries
.lines()
.filter_map(|line| CedictEntry::new(line).ok())
.collect();
Ok(Cedict { entries })
}
}
I get these errors:
Compiling cccedict v0.1.3 (.../cccedict)
error[E0726]: implicit elided lifetime not allowed here
--> src/cedict.rs:32:18
|
32 | impl FromStr for Cedict {
| ^^^^^^- help: indicate the anonymous lifetime: `<'_>`
|
= note: assuming a `'static` lifetime...
error: `impl` item signature doesn't match `trait` item signature
--> src/cedict.rs:35:5
|
35 | fn from_str(cedict_entries: &str) -> Result<Cedict, CedictEntryError> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ found `fn(&str) -> Result<Cedict<'_>, CedictEntryError>`
|
::: /Users/brian/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/src/rust/library/core/src/str/traits.rs:554:5
|
554 | fn from_str(s: &str) -> Result<Self, Self::Err>;
| ------------------------------------------------ expected `fn(&str) -> Result<Cedict<'static>, CedictEntryError>`
|
= note: expected `fn(&str) -> Result<Cedict<'static>, _>`
found `fn(&str) -> Result<Cedict<'_>, _>`
= help: the lifetime requirements from the `impl` do not correspond to the requirements in the `trait`
= help: verify the lifetime relationships in the `trait` and `impl` between the `self` argument, the other inputs and its output
error: could not compile `cccedict` due to 2 previous errors
When I add explicit lifetimes:
impl<'a> FromStr for Cedict<'a> {
type Err = CedictEntryError;
fn from_str(cedict_entries: &'a str) -> Result<Cedict<'a>, CedictEntryError> {
let entries: Vec<CedictEntry> = cedict_entries
.lines()
.filter_map(|line| CedictEntry::new(line).ok())
.collect();
Ok(Cedict { entries })
}
}
I get these errors:
Compiling cccedict v0.1.3 (.../cccedict)
error[E0308]: method not compatible with trait
--> src/cedict.rs:35:5
|
35 | fn from_str(cedict_entries: &'a str) -> Result<Cedict<'a>, CedictEntryError> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
|
= note: expected fn pointer `fn(&str) -> Result<_, _>`
found fn pointer `fn(&'a str) -> Result<_, _>`
note: the anonymous lifetime #1 defined on the method body at 35:5...
--> src/cedict.rs:35:5
|
35 | fn from_str(cedict_entries: &'a str) -> Result<Cedict<'a>, CedictEntryError> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: ...does not necessarily outlive the lifetime `'a` as defined on the impl at 32:6
--> src/cedict.rs:32:6
|
32 | impl<'a> FromStr for Cedict<'a> {
| ^^
For more information about this error, try `rustc --explain E0308`.
error: could not compile `cccedict` due to previous error
I wish I could ask a more specific question, but I'm just really at a loss Any help would be appreciated!