String::into_raw_parts¶
Level: reference · for working programmers
One line: Consumes the String into (pointer, length, capacity) and stops running its destructor — the memory is now yours to account for.
Stable since 1.93.0.
Nothing is copied and nothing is freed. The three values are exactly what the String held, and the String itself is forgotten, so the buffer will leak unless you give it back to Rust with from_raw_parts.
The point is FFI: three plain integers cross a language boundary where a String cannot, and the same three come back to reconstruct it. All three must be preserved exactly — reconstructing with a different capacity corrupts the allocator, because deallocation is sized.
This is not a pointer to lend out. as_ptr borrows; this transfers ownership. And it is not NUL-terminated, so a C API expecting char * still needs a CString.
Stable since 1.93.
Example¶
string_into_raw_parts.rs in full — pasted here by tools/run_examples.py from the file CI compiles and runs.
fn main() {
let s = String::from("héllo");
let (len, cap) = (s.len(), s.capacity());
let (ptr, l, c) = s.into_raw_parts();
println!("len {l} capacity {c} -- the same values the String held: {}", (l, c) == (len, cap));
// Nothing is freed until it is given back.
let back = unsafe { String::from_raw_parts(ptr, l, c) };
println!("{back:?}");
// Round trip preserves the capacity exactly.
let mut roomy = String::with_capacity(32);
roomy.push_str("x");
let (p2, l2, c2) = roomy.into_raw_parts();
let restored = unsafe { String::from_raw_parts(p2, l2, c2) };
println!("len {} capacity {}", restored.len(), restored.capacity());
// Borrowing, for comparison: as_ptr transfers nothing.
let borrowed = String::from("kept");
let _p = borrowed.as_ptr();
println!("{borrowed:?} is still owned by its binding");
}
Verified output of string_into_raw_parts.rs — regenerated by tools/run_examples.py, never hand-typed.
len 6 capacity 6 -- the same values the String held: true
"héllo"
len 1 capacity 32
"kept" is still owned by its binding
See also¶
String::from_raw_parts— the way back, and the only way to free itString::leak— the same abdication, without the pointer arithmeticstr::as_ptr— borrowing rather than transferringString::into_boxed_str— an owned handoff that still frees itself
String::into_raw_parts in the standard library ↗
Po polsku¶
Ta metoda rozbiera String na trzy liczby — surowy wskaźnik (raw pointer), długość i pojemność — i, co najważniejsze, nie uruchamia destruktora: bufor na stercie dalej istnieje, ale nikt już za niego nie odpowiada, więc wycieknie, dopóki nie oddasz go Rustowi przez from_raw_parts. Sensem jej istnienia jest FFI: trzy zwykłe liczby przechodzą przez granicę języków, przez którą String przejść nie potrafi. Dwie pułapki warto zapamiętać osobno, bo żadna z nich nie kończy się błędem kompilacji, tylko uszkodzeniem sterty w czasie działania: wszystkie trzy wartości muszą wrócić dokładnie te same (Rust zwalnia pamięć, podając alokatorowi jej rozmiar — dlatego przykład wypisuje len 1 capacity 32, a nie 1 i 1), a otrzymany wskaźnik nie jest zakończony zerem, więc API w C oczekujące char * i tak wymaga CString. Jeżeli chciałeś tylko podejrzeć adres, właściwe jest as_ptr — ono pożycza i nie przekazuje własności nikomu.
Szukaj po polsku: surowy wskaźnik · wyciek pamięci · wywoływanie kodu C z Rusta · rust String into_raw_parts · rust from_raw_parts FFI