My goal is to make the error messages in this Rust file DRYer (don't repeat yourself) https://github.com/Drops-of-Diamond/diamond_drops/blob/develop/src/cli/args.rs#L22.
I created a function with signature called show_message_with_available_args(message: &mut String)
to concatenate a post-fix to the end of the pre-fix of the error message that is passed into the function, since in the existing Rust file the post-fix of the error message is repeated multiple times:
", try cargo run -- -mode <argument>, where argument is proposer, p, notary, n, both, or b."
I want the error message returned in this example to be:
"Invalid configuration argument, try cargo run -- -mode <argument>, where argument is proposer, p, notary, n, both, or b."
I've created a stand-alone snippet of the code that you can copy/paste into https://play.rust-lang.org/ to replicate the same error that I encountered:
pub fn parse_arg(arg: &str) -> Result<(), &'static str> {
match arg {
"wrong arg" => { println!("Don't go here!"); Ok(()) },
_ => {
let mut message = String::from("Invalid configuration argument").to_owned();
show_message_with_available_args(&mut message);
let message_slice: &str = message.as_str();
return Err(message_slice.as_ref());
}
}
}
fn main() {
parse_arg(&String::from("right arg"));
}
fn show_message_with_available_args(message: &mut String) {
let available_args: &str = ", try cargo run -- -mode <argument>, \
where argument is proposer, p, notary, n, both, or b.";
message.push_str(available_args);
}
When you run the code you'll see the error that I encountered replicated as follows:
|
7 | let message_slice: &str = message.as_str();
| ^^^^^^^ borrowed value does not live long enough
8 | return Err(message_slice.as_ref());
9 | }
| - borrowed value only lives until here
|
= note: borrowed value must be valid for the static lifetime...
I believe the error is because Strings do not live for the entire life of the block, and that's what the return type of &'static
lifetime is expecting.
I understand from reading the The Rust Programming Language Book that since references are immutable I cannot modify it in the function show_message_with_available_args
that is called unless I create a mutable reference with &mut
and receive it with &mut
too.
I have tried using techniques mentioned here such as using let
to convert it first to a string slice, or by explicit reborrowing, but I still get the same error.
I have read through other posts in the forums that are also entitled Borrowed value does not live long enough
since they encountered the same error, but they haven't helped me resolved the issue unfortunately.