Calling the to_string
method on a value that is already of type String
in Rust is redundant and unnecessary. This
redundant call does not change the value and only adds unnecessary complexity to the code. It can also be confusing to readers and may lead to
misunderstandings about the code’s intent.
If the intent was to clone the string, it is better to use the clone
method instead.
Avoiding such redundant calls helps keep the code clean, concise, and more readable.
Noncompliant code example
let message = String::from("hello world");
println!("{}", message.to_string()); // Noncompliant: 'message' is already a String
Compliant solution
let message = String::from("hello world");
println!("{}", message);
// or
let cloned_message = message.clone(); // Use 'clone' if you need a duplicate
println!("{}", cloned_message);