&str -> String
has many equally valid methods:String::from(st)
,st.to_string()
,st.to_owned()
.- But I suggest you stick with one of them within a single project. The major advantage of
String::from
is that you can use it as an argument to amap
method. So instead ofx.map(|s| String::from(s))
you can often usex.map(String::from)
.
- But I suggest you stick with one of them within a single project. The major advantage of
&str
->&[u8]
is done byst.as_bytes()
&str
->Vec<u8>
is a combination of&str -> &[u8] -> Vec<u8>
, i.e.st.as_bytes().to_owned()
String -> &str
should just be&s
where coercion is available ors.as_str()
where it is not.String -> &[u8]
is the same as&str -> &[u8]
:s.as_bytes()
String -> Vec<u8>
has a custom method:s.into_bytes()
&[u8] -> Vec<u8>
is done byu.to_owned()
&[u8] -> &str
doesn't actually exist, that would be&[u8] -> Result<&str, Error>
, provided viastr::from_utf8(u)
str::from_utf8(u).unwrap()
works, but you should prefer better error handling (see Error handling - The Result type).
&[u8] -> String
is the combination of&[u8] -> Result<&str, Error> -> Result<String, Error>
String::from_utf8(u).unwrap()
, but prefer better error handling (see Error handling - The Result type and alsoResult::map
.
Vec<u8> -> &[u8]
should be just&v
where coercion is available, oras_slice
where it's not.Vec<u8> -> &str
is the same asVec<u8> -> &[u8] -> Result<&str, Error>
:str::from_utf8(&v)
str::from_utf8(&v).unwrap()
but prefer better error handling (see Error handling - The Result type)
Vec<u8> -> String
doesn't actually exist, that would beVec<u8> -> Result<String, Error>
viaString::from_utf8(v)
String::from_utf8(v).unwrap()
works, but prefer better error handling (see Error handling - The Result type).
Coercion is available whenever the target is not generic but explicitly typed as &str
or &[u8]
respectively