Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert a char to a String?

Tags:

string

char

rust

Is there a succinct way to convert a char to a String in Rust other than:

let mut s = String::new();
s.push('c');
like image 995
Daniel Fath Avatar asked Jan 17 '15 19:01

Daniel Fath


People also ask

How do I convert a char to a string in R?

To convert elements of a Vector to Strings in R, use the toString() function. The toString() is an inbuilt R function used to produce a single character string describing an R object.

Can you use toString on char?

The toString() and valueOf() methods can both be used to convert a char to a string in Java.


2 Answers

You use the to_string() method:

'c'.to_string()
like image 196
Steve Klabnik Avatar answered Oct 05 '22 14:10

Steve Klabnik


Since Rust 1.46, String implements From<char>, so you can alternatively use:

String::from('c')

or, with type inference,

'c'.into()
like image 33
L. F. Avatar answered Oct 05 '22 15:10

L. F.