diff --git a/src/liballoc/string.rs b/src/liballoc/string.rs index ff3587d5d873..8690f9017c40 100644 --- a/src/liballoc/string.rs +++ b/src/liballoc/string.rs @@ -618,7 +618,15 @@ impl String { /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn from_utf16(v: &[u16]) -> Result { - decode_utf16(v.iter().cloned()).collect::>().map_err(|_| FromUtf16Error(())) + let mut ret = String::with_capacity(v.len()); + for c in decode_utf16(v.iter().cloned()) { + if let Ok(c) = c { + ret.push(c); + } else { + return Err(FromUtf16Error(())); + } + } + Ok(ret) } /// Decode a UTF-16 encoded slice `v` into a `String`, replacing