1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Converting between different String types in Rust</title>
<style>
@font-face {
  font-family: 'Quattrocento';
  font-weight: 400;
  src: url(/fonts/quattrocento-latin.woff2) format('woff2');
  font-display: swap;
}
@font-face {
  font-family: 'Open Sans';
  font-weight: 300;
  src: url(/fonts/open-sans-latin.woff2) format('woff2');
  font-display: swap;
}

  body {
    font-family: 'Quattrocento', serif;
    font-size: 20px;
    line-height: 1.5em;
    margin: 0 auto;
    max-width: 80ch;
    padding: 1em 0.5em;
    background: #fff;
    color: #111;
  }
  @media (min-width: 40em) { body { padding: 1em 2em; } }
  @media (prefers-color-scheme: dark) {
    body { background: #1a1a1a; color: #ddd; }
    a { color: #adf; }
    a:visited { color: #c9a0ff; }
    hr { border-top-color: #444; }
  }
  h1, h2 { font-family: 'Open Sans', sans-serif; }
  h1 { font-weight: 300; }
  h2 { font-weight: 400; margin-top: 2em; }
  hr { border: none; border-top: 1px solid #ccc; margin: 2em 0; }
  pre {
    background: #f5f5f5;
    border: 1px solid #ddd;
    border-radius: 3px;
    padding: 0.75em 1em;
    overflow-x: auto;
    font-size: 0.75em;
    line-height: 1.4em;
  }
  code { font-size: 0.85em; }
  pre code { font-size: 1em; }
  @media (prefers-color-scheme: dark) {
    pre { background: #222; border-color: #444; }
  }
</style>
</head>
<body>

<h1>Converting between different String types in Rust</h1>

<hr>

<pre><code>let s: String = ...
let st: &str = ...
let u: &[u8] = ...
let b: [u8; 3] = b"foo"
let v: Vec&lt;u8&gt; = ...
let os: OsString = ...
let ost: OsStr = ...

From       To         Use                                    Comment
----       --         ---                                    -------
&str     -> String    String::from(st)
&str     -> &[u8]     st.as_bytes()
&str     -> Vec&lt;u8&gt;   st.as_bytes().to_owned()               via &[u8]
&str     -> &OsStr    OsStr::new(st)

String   -> &str      &s                                     alt. s.as_str()
String   -> &[u8]     s.as_bytes()
String   -> Vec&lt;u8&gt;   s.into_bytes()
String   -> OsString  OsString::from(s)

&[u8]    -> &str      str::from_utf8(u).unwrap()
&[u8]    -> String    String::from_utf8(u).unwrap()
&[u8]    -> Vec&lt;u8&gt;   u.to_owned()
&[u8]    -> &OsStr    OsStr::from_bytes(u)                   use std::os::unix::ffi::OsStrExt;

[u8; 3]  -> &[u8]     &b[..]                                 byte literal
[u8; 3]  -> &[u8]     "foo".as_bytes()                       alternative via utf8 literal

Vec&lt;u8&gt;  -> &str      str::from_utf8(&v).unwrap()            via &[u8]
Vec&lt;u8&gt;  -> String    String::from_utf8(v)
Vec&lt;u8&gt;  -> &[u8]     &v
Vec&lt;u8&gt;  -> OsString  OsString::from_vec(v)                  use std::os::unix::ffi::OsStringExt;

&OsStr   -> &str      ost.to_str().unwrap()
&OsStr   -> String    ost.to_os_string().into_string()       via OsString
                         .unwrap()
&OsStr   -> Cow&lt;str&gt;  ost.to_string_lossy()                  Unicode replacement characters
&OsStr   -> OsString  ost.to_os_string()
&OsStr   -> &[u8]     ost.as_bytes()                         use std::os::unix::ffi::OsStringExt;

OsString -> String    os.into_string().unwrap()              returns original OsString on failure
OsString -> &str      os.to_str().unwrap()
OsString -> &OsStr    os.as_os_str()
OsString -> Vec&lt;u8&gt;   os.into_vec()                          use std::os::unix::ffi::OsStringExt;</code></pre>

<h2>Source</h2>

<p>Original source is <a href="https://web.archive.org/web/20190710121935/https://pastebin.com/Mhfc6b9i">this document on Pastebin</a>.</p>

</body>
</html>