We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 0db7abe commit 19aa101Copy full SHA for 19aa101
src/liballoc/string.rs
@@ -618,7 +618,15 @@ impl String {
618
/// ```
619
#[stable(feature = "rust1", since = "1.0.0")]
620
pub fn from_utf16(v: &[u16]) -> Result<String, FromUtf16Error> {
621
- decode_utf16(v.iter().cloned()).collect::<Result<_, _>>().map_err(|_| FromUtf16Error(()))
+ let mut ret = String::with_capacity(v.len());
622
+ for c in decode_utf16(v.iter().cloned()) {
623
+ if let Ok(c) = c {
624
+ ret.push(c);
625
+ } else {
626
+ return Err(FromUtf16Error(()));
627
+ }
628
629
+ Ok(ret)
630
}
631
632
/// Decode a UTF-16 encoded slice `v` into a `String`, replacing
0 commit comments