Open
Description
Given the following code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=ee83cc45a558eccd6b784e17065e71a5
fn takes_str(_: &str) {}
fn main() {
takes_str(String::from("e"));
takes_str(format!("e"));
}
The current output is:
error[[E0308]](https://doc.rust-lang.org/stable/error-index.html#E0308): mismatched types
--> src/main.rs:4:15
|
4 | takes_str(String::from("e"));
| --------- ^^^^^^^^^^^^^^^^^
| | |
| | expected `&str`, found struct `String`
| | help: consider borrowing here: `&String::from("e")`
| arguments to this function are incorrect
|
note: function defined here
--> src/main.rs:1:4
|
1 | fn takes_str(_: &str) {}
| ^^^^^^^^^ -------
error[[E0308]](https://doc.rust-lang.org/stable/error-index.html#E0308): mismatched types
--> src/main.rs:5:15
|
5 | takes_str(format!("e"));
| ^^^^^^^^^^^^ expected `&str`, found struct `String`
|
= note: this error originates in the macro `format` (in Nightly builds, run with -Z macro-backtrace for more info)
Using String::from
directly tells us we can borrow it with &
. Using format!
does not.