Skip to content

Commit aa47a8e

Browse files
authored
Rollup merge of rust-lang#126970 - DaniPopes:simplify-str-clone_into, r=cuviper
Simplify `str::clone_into` Removes an `unsafe` in favor of just using `String` methods.
2 parents edb0168 + d5ff4f4 commit aa47a8e

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

library/alloc/src/str.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -206,15 +206,16 @@ impl BorrowMut<str> for String {
206206
#[stable(feature = "rust1", since = "1.0.0")]
207207
impl ToOwned for str {
208208
type Owned = String;
209+
209210
#[inline]
210211
fn to_owned(&self) -> String {
211212
unsafe { String::from_utf8_unchecked(self.as_bytes().to_owned()) }
212213
}
213214

215+
#[inline]
214216
fn clone_into(&self, target: &mut String) {
215-
let mut b = mem::take(target).into_bytes();
216-
self.as_bytes().clone_into(&mut b);
217-
*target = unsafe { String::from_utf8_unchecked(b) }
217+
target.clear();
218+
target.push_str(self);
218219
}
219220
}
220221

0 commit comments

Comments
 (0)