diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs index 71b6b9b41f5c5..8ef25b86cdb4c 100644 --- a/library/alloc/src/string.rs +++ b/library/alloc/src/string.rs @@ -607,17 +607,7 @@ impl String { #[cfg(not(no_global_oom_handling))] #[stable(feature = "rust1", since = "1.0.0")] pub fn from_utf16(v: &[u16]) -> Result { - // This isn't done via collect::>() for performance reasons. - // FIXME: the function can be simplified again when #48994 is closed. - 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_utf16(v.iter().cloned()).collect::>().map_err(|_| FromUtf16Error(())) } /// Decode a UTF-16–encoded slice `v` into a `String`, replacing