Replies: 1 comment 5 replies
-
In general, few CLI definitions get reused in other ways as a lot of what is useful for a CLI doesn't always apply in other situations
My guess is that its because of fn parse_cow_str(string: &str) -> Result<Cow<'static, str>, std::io::Error>{
Ok(Cow::Owned(string.to_owned()))
} That function says it could return a You need to tell the compiler that you won't use the borrowed variant. fn parse_cow_str<'ignored>(string: &str) -> Result<Cow<'ignored, str>, std::io::Error>{
Ok(Cow::Owned(string.to_owned()))
} If that doesn't compile, then you likely need a feature called higher ranked trait bounds (HRTB) to tell the compiler you don't care about that lifetime. |
Beta Was this translation helpful? Give feedback.
-
Hi!
I have some
struct Cli
that contains text. I don't want to store an owned version of the text, mainly because I only need a&str
and requiring the user to provide ownedString
will eventually lead to unnecessary clones.For convenience, I also derive
clap::Parser
this struct, such that it is possible to create it from command-line arguments.Note
I guess this is a frequent issue, but I could not find any other post mentioning this.
In practice, my struct has many other fields, and creating two almost identical structures
just to have
clap
on the "owned" version, would required duplicating a lot of the code.So I would prefer not having to duplicate code :-)
I know
clap
requires the parsed value to be'static
, but I thought it would be possible to handle both owned and borrowed variants usingCow
, see my attempt below.Of course, this does not compile (see error traceback below), and I was wondering if there was a solution to this?
Beta Was this translation helpful? Give feedback.
All reactions