Fixed: "error: character literal may only contain one codepoint" in Rust
roles.join(', ') stops Rust with error: character literal may only contain one codepoint. Single quotes hold exactly one character, and ', ' has two: a comma and a space. The compiler's help calls the space a non-printing character and suggests removing it, which leads to a second error. 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. After a comma in single quotes, ',', I tried a comma and a space in single quotes: ', '.
What I Expected
An error saying that text of more than one character 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: character literal may only contain one codepoint
--> src/main.rs:3:31
|
3 | println!("{}", roles.join(', '));
| ^^^^
|
note: there are non-printing characters, the full sequence is `, `
--> src/main.rs:3:32
|
3 | println!("{}", roles.join(', '));
| ^^
help: consider removing the non-printing characters
|
3 | println!("{}", roles.join(','));
| ~
error: could not compile `demo` (bin "demo") due to 1 previous error

The build stopped at the literal itself. The note says "there are non-printing characters, the full sequence is , ", and the help suggests removing them, which leaves ','. I followed the help:
fn main() {
let roles = vec!["editor", "viewer", "admin"];
println!("{}", roles.join(','));
}
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

With the space removed, ',' is a valid char, and the build failed again, this time because join doesn't take a char: expected `&str`, found `char`. That's the problem in this fix. The space was never the problem: the single quotes were.
What I Checked
- What single quotes hold. The Rust Reference, Character literals: "A character literal is a single Unicode character enclosed within two U+0027 (single-quote) characters, with the exception of U+0027 itself, which must be escaped by a preceding U+005C character".
', 'holds two characters. - What double quotes hold. String literals, on the same page: "A string literal is a sequence of any Unicode characters enclosed within two U+0022 (double-quote) characters". The Rust Book, The Character Type, says the same in short: "Note that we specify char literals with single quotation marks, as opposed to string literals, which use double quotation marks."
- Why the help talks about non-printing characters. The note was added by pull request #90861, "Print escaped string if char literal has multiple characters, but only one printable character", for Rust issue #90857, which shows a zero-width space hidden after a character in single quotes. For a character like that, the note does its job: in my check, a comma followed by a zero-width space gave the same error, with the full sequence shown as
,\u{200b}, and removing the invisible character is the right fix. My space, though, was typed on purpose, and removing it changed the separator. - What the help says for letters. With two letters in single quotes,
'or', the same error came with a different help: "if you meant to write a string literal, use double quotes". That's the help', 'needed too, so I reported it as Rust issue #166993.
The Fix
Write the separator in double quotes:
fn main() {
let roles = vec!["editor", "viewer", "admin"];
println!("{}", roles.join(", "));
}
Only the quotes changed: ", " in double quotes is a &str that holds both the comma and the space.
The Result
cargo run -q
editor, viewer, admin

The program compiled and printed editor, viewer, admin, with a space after each comma.
How to Avoid It
- Use single quotes only for one character, a
char. Anything longer goes in double quotes. - Write the separator of
joinin double quotes, as in", ", whatever its length. - Before you remove characters because a help calls them non-printing, check whether you typed them on purpose.
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
error: character literal may only contain one codepoint | Two or more characters in single quotes | Write them in double quotes: ", " |
expected `&str`, found `char` after removing the space | ',' is a char, and join takes a &str | Write it in double quotes, as in this fix |
The same error on what looks like one character, with a note whose full sequence shows a code such as \u{200b} | An invisible character, here a zero-width space, is hidden in the literal | Remove it, as the help suggests |
FAQ
What is a codepoint?
The number Unicode gives a character. The Rust Book says that a char "represents a Unicode scalar value", which is one such number, so a character literal holds exactly one character. The comma and the space in ', ' are two.
Why didn't the compiler suggest double quotes?
Because it treated the space like an invisible character and suggested removing it. For two letters, 'or', it did suggest double quotes. Rust issue #166993 asks for the same help when the extra character is a space.
Conclusion
error: character literal may only contain one codepoint means there's more than one character between single quotes. Text goes in double quotes: roles.join(", "). Don't follow the help to remove the space unless you typed it by mistake.
Additional Resources
- Character literals and String literals, in The Rust Reference.
- The Character Type, in The Rust Programming Language.
- Rust issue #166993, about the help for a space in a character literal.
