src/unsafe-rust/solution.md
# // Copyright 2023 Google LLC
# // SPDX-License-Identifier: Apache-2.0
#
{{#include exercise.rs:solution}}
unsafe block is preceded by a // SAFETY: comment
explaining why the operation is safe. This is standard practice in Rust to aid
auditing.&str -> CString: To create a null-terminated string for C.CString -> *const c_char: To pass the pointer to C.*const c_char -> &CStr: To wrap the returned C string.&CStr -> &[u8] -> &OsStr -> OsString: To convert the bytes back to a
Rust OS string.Drop): We implement Drop to call closedir automatically when
the iterator goes out of scope. This ensures we don't leak file descriptors.Iterator, providing a
safe and idiomatic interface (next returns Option<OsString>) to the
underlying unsafe C functions.CString owns the data (like String), while CStr is a
borrowed reference (like &str).OsStrExt trait is needed on Unix systems to convert bytes directly to
OsStr.