xxxxxxxxxx
use std::ffi::OsStr;
use std::os::unix::ffi::OsStrExt;
fn main() {
let os_str = OsStr::new("example");
let string: String = os_str.to_string_lossy().to_string();
println!("{}", string);
}
xxxxxxxxxx
// You can convert a literal string (&str) into a String using the to_string function
// Here s is a String where s_literal is a &'static str
let s_literal = "String literal";
let s = s_literal.to_string();
xxxxxxxxxx
pub trait Literal {
fn literal(&self) -> String;
}
// Using the .literal() method on a String or &str returns the String.
impl Literal for String { fn literal(&self) -> String { return self.clone() } }
impl Literal for &str { fn literal(&self) -> String { return String::from(*self); } }