I have to be honest that I cannot easily understand or explain something as simple as string processing in rust, but I have figured out how to work around some of the errors I get from the compiler. I think that Rust: error[E0369]: cannot add `String` to `&str` is likely one of the first that most developers new to rust may see, as string processing patterns learned from other languages do not seem to apply in rust. Unfortunately, this leads to a poor initial developer experience, especially for someone like me that would rather write blog posts than read documentation.
Assume that I have defined a function to show help information for my program, with the following signature that accepts a message and a list of the arguments passed on the command line.
fn help(msg: &str, args: Vec<String>)
I can call the help method with a static string value for the message.
let args: Vec<String> = env::args().collect(); help("Help requested or no command specified", args);
I naively try to construct the message argument using a variable.
help("Invalid command: " + compare, args);
This code will generate a compiler error such as the following.
error[E0369]: cannot add `&str` to `&str`…`+` cannot be used to concatenate two `&str` strings… help: `to_owned()` can be used to create an owned `String` from a string reference…
Unfortunately, this error message seems to point me in the wrong direction. There seems to be some kind of type mismatch, but the real issue seems to have something to do with static strings (str) and memory allocation (String). Rust tries to help my code be efficient. If I take the compiler’s advice, it gives me back a different error.
error[E0369]: cannot add `String` to `&str`..`+` cannot be used to concatenate a `&str` with a `String`…help: `to_owned()` can be used to create an owned `String` from a string reference…
It does not feel that this is going in the right direction.
One solution is apparently to allocate a new variable and format the string.
let msg = format!("invalid command: {0}", compare); help(&msg.to_string(), args);
Which can be shortened, which requires converting the str to a String, or the String to a str, or whatever.
help(&format!("invalid command: {0}", compare), args);
Coming from numerous other languages, especially those that manage memory with garbage collection, and even including things as intentionally obfuscated as Perl, Rust seems like a weird language at first. It took some time, and I have much to learn, but after figuring out some of the patterns, details, and intentions, I have a better understanding of some of certain obscurities about rust, which seems to be about the compiler rather than the developer and sometimes feels almost like touching the CPU. Honestly, I think the only way programs could be more efficient is if the developer lays out the binary by hand.
Like any language, there is a learning curve and there are quirks, but I seem to prefer rust to C, C++, Perl, anything significant in shell scripting, and JavaScript already, though my one true heart will always be with C#.
hi, thank you very much
LikeLike