Skip to main content

Fixed: "expected `&str`, found `char`" when you join a Vec of strings in Rust

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

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:

Terminal
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:

src/main.rs, which makes a Vec of the roles editor, viewer and admin and prints them joined with a comma written in single quotes.

src/main.rs
fn main() {
let roles = vec!["editor", "viewer", "admin"];
println!("{}", roles.join(','));
}

Run it:

Terminal
cargo run -q
Output
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

A zsh terminal where cargo run -q fails with error E0308, mismatched types, expected str, found char, a help that the return type of the call is char due to the type of the argument passed, and a last help that suggests double quotes.

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 join accepts. The standard library documents join on 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 &str for a slice of strings, which gives a String; a &OsStr for a slice of OsStr strings; and a &T or a &[T] for a slice of slices, which gives a Vec<T>. None of them is a char, 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 a char, 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 of join". But with a char, no Join implementation 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 this join case 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:

src/main.rs
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​

Terminal
cargo run -q
Output
editor,viewer,admin

A zsh terminal where cargo run -q prints editor,viewer,admin with no error.

The program compiled and printed editor,viewer,admin.

How to Avoid It​

  • Write the separator of join in 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​

SymptomLikely causeFix
expected `&str`, found `char` on join(',')The separator is a charWrite it in double quotes: join(",")
error: character literal may only contain one codepoint on join(', ')Two characters in single quotesWrite them in double quotes, as in this fix
no method named `join` found for struct `std::slice::Iter<'a, T>` in the current scopejoin was called on an iteratorCall 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​