From 35aa79d14b2f4032efd79cda2c25b11540768107 Mon Sep 17 00:00:00 2001 From: Profpatsch Date: Tue, 4 May 2021 00:11:14 +0200 Subject: feat(users/Profpatsch/blog): add rust-string-conversions note Change-Id: I7bee585935e65660f6b25b88ed33f09775eb01a0 Reviewed-on: https://cl.tvl.fyi/c/depot/+/3088 Tested-by: BuildkiteCI Reviewed-by: Profpatsch --- users/Profpatsch/blog/default.nix | 10 +++-- .../blog/notes/rust-string-conversions.md | 52 ++++++++++++++++++++++ 2 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 users/Profpatsch/blog/notes/rust-string-conversions.md diff --git a/users/Profpatsch/blog/default.nix b/users/Profpatsch/blog/default.nix index 0da57f482a..584c12d8d7 100644 --- a/users/Profpatsch/blog/default.nix +++ b/users/Profpatsch/blog/default.nix @@ -16,13 +16,16 @@ let bins.lowdown "-s" "-Thtml" "-o" "$out" note ]; - preventing-oom = renderNote "preventing-oom" ./notes/preventing-oom.md; - notes = [ { route = [ "notes" "preventing-oom" ]; name = "Preventing OOM"; - page = preventing-oom; + page = renderNote "preventing-oom" ./notes/preventing-oom.md; + } + { + route = [ "notes" "rust-string-conversions" ]; + name = "Converting between different String types in Rust"; + page = renderNote "rust-string-conversions" ./notes/rust-string-conversions.md; } ]; @@ -308,7 +311,6 @@ let in depot.nix.utils.drvTargets { inherit - preventing-oom router notes-server split-stdin diff --git a/users/Profpatsch/blog/notes/rust-string-conversions.md b/users/Profpatsch/blog/notes/rust-string-conversions.md new file mode 100644 index 0000000000..ac8c8f8925 --- /dev/null +++ b/users/Profpatsch/blog/notes/rust-string-conversions.md @@ -0,0 +1,52 @@ +# Converting between different String types in Rust + +``` +let s: String = ... +let st: &str = ... +let u: &[u8] = ... +let b: [u8; 3] = b"foo" +let v: Vec = ... +let os: OsString = ... +let ost: OsStr = ... + +From To Use Comment +---- -- --- ------- +&str -> String String::from(st) +&str -> &[u8] st.as_bytes() +&str -> Vec st.as_bytes().to_owned() via &[u8] + +String -> &str &s alt. s.as_str() +String -> &[u8] s.as_bytes() +String -> Vec s.into_bytes() +String -> OsString OsString::from(s) + +&[u8] -> &str str::from_utf8(u).unwrap() +&[u8] -> String String::from_utf8(u).unwrap() +&[u8] -> Vec 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 -> &str str::from_utf8(&v).unwrap() via &[u8] +Vec -> String String::from_utf8(v) +Vec -> &[u8] &v +Vec -> 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 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 os.into_vec() use std::os::unix::ffi::OsStringExt; +``` + + +## Source + +Original source is [this document on Pastebin](https://web.archive.org/web/20190710121935/https://pastebin.com/Mhfc6b9i) -- cgit 1.4.1