'error while setting conditional value in rust using format in string
I have an issue related to conditional value to a string in rust, if I remove format() everything runs fine but I have no idea how to implement this with format.
let value =
if matcha.sugar {
format!("Hello {} your matcha is ready at {}ºC with sugar", self.client, matcha.temp);
}
else {
format!("Hello {} your matcha is ready at {}ºC without sugar", self.client, matcha.temp);
}.to_string();
Error Message:
"message": "the method `to_string` exists for unit type `()`, but its trait bounds were not satisfied\n\nmethod cannot be called on `()` due to unsatisfied trait bounds\n\nnote: the following trait bounds were not satisfied:\n `(): std::fmt::Display`\n which is required by `(): std::string::ToString`",
"source": "rustc",
"relatedInformation": [
{
"startLineNumber": 60,
"startColumn": 23,
"endLineNumber": 60,
"endColumn": 32,
"message": "method cannot be called on `()` due to unsatisfied trait bounds",
}
]
}]
Solution 1:[1]
Trailing semicolons make expressions evaluate to () You need to remove the ; after the format!(...)s Also, the to_string() is useless because format!(...) already give you Strings, you would just allocate a new one for nothing
by Giuschi#7708
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mentasuave01 |
