about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2024-06-28 08:34:09 +0200
committerGitHub <noreply@github.com>2024-06-28 08:34:09 +0200
commit2c228260dcf76238de2ac4427b6decd1c74a181b (patch)
treefcb94a62647e3d710ab6513b2411db0f54299f83
parentc4d0c0892532af122da046d6103f65255ac09690 (diff)
parentd5ff4f4f657766ca03d7b96553baae6aca053596 (diff)
downloadrust-2c228260dcf76238de2ac4427b6decd1c74a181b.tar.gz
rust-2c228260dcf76238de2ac4427b6decd1c74a181b.zip
Rollup merge of #126970 - DaniPopes:simplify-str-clone_into, r=cuviper
Simplify `str::clone_into`

Removes an `unsafe` in favor of just using `String` methods.
-rw-r--r--library/alloc/src/str.rs7
1 files changed, 4 insertions, 3 deletions
diff --git a/library/alloc/src/str.rs b/library/alloc/src/str.rs
index 3e23612d0c1..3bb808a6c73 100644
--- a/library/alloc/src/str.rs
+++ b/library/alloc/src/str.rs
@@ -206,15 +206,16 @@ impl BorrowMut<str> for String {
 #[stable(feature = "rust1", since = "1.0.0")]
 impl ToOwned for str {
     type Owned = String;
+
     #[inline]
     fn to_owned(&self) -> String {
         unsafe { String::from_utf8_unchecked(self.as_bytes().to_owned()) }
     }
 
+    #[inline]
     fn clone_into(&self, target: &mut String) {
-        let mut b = mem::take(target).into_bytes();
-        self.as_bytes().clone_into(&mut b);
-        *target = unsafe { String::from_utf8_unchecked(b) }
+        target.clear();
+        target.push_str(self);
     }
 }