Fixed: "expected `&str`, found `char`" when you join a Vec of strings in Rust
roles.join(',') doesn't compile in Rust. ',' in single quotes is a char, and join on a list of strings takes its separator as a &str, so the compiler says expected `&str`, found `char`, along with a confusing help about the return type. The fix is double quotes: roles.join(",").
What I Was Doing
I was trying out the mistakes people make with join, for a guide on joining a Vec of strings in Rust. One of them is writing a one-character separator in single quotes, as ','.
What I Expected
Either editor,viewer,admin, or an error saying that the separator has to be in double quotes.
What Happened Instead
Create a project:
cargo new demo
cd demo
cargo new demo creates a project in a folder named demo, with its code in src/main.rs, and cd demo moves into it.
Replace the contents of src/main.rs with:

fn main() {
let roles = vec!["editor", "viewer", "admin"];
println!("{}", roles.join(','));
}
Run it:
cargo run -q
error[E0308]: mismatched types
--> src/main.rs:3:31
|
3 | println!("{}", roles.join(','));
| ---- ^^^ expected `&str`, found `char`
| |
| arguments to this method are incorrect
|
help: the return type of this call is `char` due to the type of the argument passed
--> src/main.rs:3:20
|
3 | println!("{}", roles.join(','));
| ^^^^^^^^^^^---^
| |
| this argument influences the return type of `join`
note: method defined here
--> /rustc/20ddd01716a87c0fcf06ae4752d85a4abc15bcdf/library/alloc/src/slice.rs:615:11
help: if you meant to write a string literal, use double quotes
|
3 - println!("{}", roles.join(','));
3 + println!("{}", roles.join(","));
|
For more information about this error, try `rustc --explain E0308`.
error: could not compile `demo` (bin "demo") due to 1 previous error

The program didn't compile: join expected a &str and got a char. The last help gave the fix, but the one in between, the return type of this call is `char` due to the type of the argument passed, didn't make sense to me, because join doesn't return a char.
What I Checked
- What
joinaccepts. The standard library documentsjoinon slices: "Flattens a slice of T into a single value Self::Output, placing a given separator between each." Its own example uses a separator in double quotes:["hello", "world"].join(" "). The separators it accepts are listed on the page of its helper trait, Join: a&strfor a slice of strings, which gives aString; a&OsStrfor a slice ofOsStrstrings; and a&Tor a&[T]for a slice of slices, which gives aVec<T>. None of them is achar, and none of them returns one. - What
','is. The Rust Book, The Character Type: "Note that we specify char literals with single quotation marks, as opposed to string literals, which use double quotation marks." So','is achar, and","is a&str. - The help about the return type. In the signature of
join, the return type depends on the type of the separator, which is why the help says "this argument influences the return type ofjoin". But with achar, noJoinimplementation matches, so there's no return type to name. Rust issue #163443, "Confusing diagnostics about return type for input type mismatch", reports the same help for another method,MaybeUninit::write, and I added thisjoincase to it. - The last help. "if you meant to write a string literal, use double quotes", with the line changed to
roles.join(","). That's the fix.
The Fix
Write the separator in double quotes:
fn main() {
let roles = vec!["editor", "viewer", "admin"];
println!("{}", roles.join(","));
}
Only the separator changed: "," in double quotes is a &str.
If the separator is already a char in a variable, turn it into a string first. With let comma = ',';, roles.join(comma.to_string().as_str()) printed the same result in my checks.
The Result
cargo run -q
editor,viewer,admin

The program compiled and printed editor,viewer,admin.
How to Avoid It
- Write the separator of
joinin double quotes, even when it's a single character. - Single quotes make a
char, and double quotes make a&str. - When the compiler gives several helps, read them all: here the last one said what to change.
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
expected `&str`, found `char` on join(',') | The separator is a char | Write it in double quotes: join(",") |
error: character literal may only contain one codepoint on join(', ') | Two characters in single quotes | Write them in double quotes, as in this fix |
no method named `join` found for struct `std::slice::Iter<'a, T>` in the current scope | join was called on an iterator | Call it on the Vec, or collect the iterator into a Vec first |
FAQ
Can join take a char separator?
Not on a list of strings. The Join implementations take a &str, a &OsStr, a &T or a &[T]. A char in a variable has to become a string first, as with comma.to_string().as_str() above.
Does the same happen with a separator of several characters in single quotes?
No, that fails earlier, with a different error: ', ' gives error: character literal may only contain one codepoint, and following its help leads back to this error.
Conclusion
join on a list of strings needs a &str separator, so write it in double quotes, even for one character: roles.join(","). Single quotes make a char, which join doesn't accept.
Additional Resources
- join and the Join trait, in the Rust standard library documentation.
- The Character Type, in The Rust Programming Language.
- Rust issue #163443, about the help on the return type.
