From 6034fed057f9e28bf4fcefd99bca6dce873cae40 Mon Sep 17 00:00:00 2001 From: Husamettin ARABACI Date: Fri, 18 Jul 2025 21:59:15 +0300 Subject: [PATCH] docs: add disadvantages section to accepting-strings idiom --- src/idioms/ffi/accepting-strings.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/idioms/ffi/accepting-strings.md b/src/idioms/ffi/accepting-strings.md index 81aa8586..6f279169 100644 --- a/src/idioms/ffi/accepting-strings.md +++ b/src/idioms/ffi/accepting-strings.md @@ -135,4 +135,11 @@ character at the end of the string, sometimes it would just completely crash. ## Disadvantages -None? +While working with borrowed C strings (`&CStr`) minimizes copying and unsafe operations, there are still a few drawbacks: + +- **Lifetime Complexity:** Managing lifetimes correctly can be tricky when dealing with borrowed data from C, especially if the lifetime is not clearly documented or known. +- **Null-Terminated Assumptions:** Rust’s string handling relies on known lengths, whereas C strings rely on null termination. If a C string lacks a proper null terminator, it can lead to undefined behavior. +- **UTF-8 Enforcement:** Rust strings (`&str`) require valid UTF-8. C strings may contain arbitrary bytes, so converting `&CStr` to `&str` is fallible and may fail at runtime. +- **Thread Safety:** Raw pointers from C may not be thread-safe, and `&CStr` does not guarantee safety across threads unless explicitly handled. + +Despite these, using `&CStr` is often the best trade-off between safety and performance in FFI contexts.