# Impl TryFrom\<&str\> for CString

**URL:** <https://internals.rust-lang.org/t/impl-tryfrom-str-for-cstring/21407>\
**Category:** language design\
**Created:** [August 20, 2024, 2:55pm UTC](https://internals.rust-lang.org/t/impl-tryfrom-str-for-cstring/21407 "2024-08-20T14:55:30Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mlowicki](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mlowicki/32/12468_2.png) [@mlowicki](https://internals.rust-lang.org/u/mlowicki)\
**Post date:** [August 20, 2024, 2:55pm UTC](https://internals.rust-lang.org/t/impl-tryfrom-str-for-cstring/21407/1 "2024-08-20T14:55:30Z")

</div>

Hi there!

Saw it across codebase I'm working on that ppl do:

```rust
CString::new(key.to_owned());

```

where key is &str. It leads to realloc as CString::new tries to add then \0. Ended up with utility function doing:

```rust
let mut s = String::with_capacity(key.len() + 1);
s.push_str(key);
CString::new(s)

```

Opinions on impl TryFrom so avoid such inefficiency?

---

<div class="post-metadata">

**Author:** ![Ddystopia](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ddystopia/32/10915_2.png) [@Ddystopia](https://internals.rust-lang.org/u/Ddystopia)\
**Post date:** [August 20, 2024, 3:09pm UTC](https://internals.rust-lang.org/t/impl-tryfrom-str-for-cstring/21407/2 "2024-08-20T15:09:53Z")

</div>

This would also be suitable for no\_std folks, as we don't like allocations at all

---

<div class="post-metadata">

**Author:** ![bjorn3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bjorn3/32/2736_2.png) [@bjorn3](https://internals.rust-lang.org/u/bjorn3)\
**Post date:** [August 20, 2024, 3:12pm UTC](https://internals.rust-lang.org/t/impl-tryfrom-str-for-cstring/21407/3 "2024-08-20T15:12:57Z")

</div>

When would the `TryFrom` return an error under your proposal?

---

<div class="post-metadata">

**Author:** ![mlowicki](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mlowicki/32/12468_2.png) [@mlowicki](https://internals.rust-lang.org/u/mlowicki)\
**Post date:** [August 20, 2024, 3:17pm UTC](https://internals.rust-lang.org/t/impl-tryfrom-str-for-cstring/21407/4 "2024-08-20T15:17:01Z")

</div>

In the same case like `CString::new` returns `NulError` currently ([CString in std::ffi - Rust](https://doc.rust-lang.org/std/ffi/struct.CString.html#method.new)).

---

<div class="post-metadata">

**Author:** ![tguichaoua](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/tguichaoua/32/8952_2.png) [@tguichaoua](https://internals.rust-lang.org/u/tguichaoua)\
**Post date:** [August 20, 2024, 5:29pm UTC](https://internals.rust-lang.org/t/impl-tryfrom-str-for-cstring/21407/5 "2024-08-20T17:29:25Z")

</div>

`CString::new` can accepts a `&str`, see [playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=d2a7a95379ad9eebf0e21eb632916436).

---

<div class="post-metadata">

**Author:** ![bjorn3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bjorn3/32/2736_2.png) [@bjorn3](https://internals.rust-lang.org/u/bjorn3)\
**Post date:** [August 20, 2024, 5:30pm UTC](https://internals.rust-lang.org/t/impl-tryfrom-str-for-cstring/21407/6 "2024-08-20T17:30:21Z")

</div>

(Your playground link is broken. It doesn't link to any code.)

---

<div class="post-metadata">

**Author:** ![tguichaoua](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/tguichaoua/32/8952_2.png) [@tguichaoua](https://internals.rust-lang.org/u/tguichaoua)\
**Post date:** [August 20, 2024, 5:31pm UTC](https://internals.rust-lang.org/t/impl-tryfrom-str-for-cstring/21407/7 "2024-08-20T17:31:36Z")

</div>

Sorry, link fixed

---

<div class="post-metadata">

**Author:** ![mlowicki](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mlowicki/32/12468_2.png) [@mlowicki](https://internals.rust-lang.org/u/mlowicki)\
**Post date:** [August 20, 2024, 5:43pm UTC](https://internals.rust-lang.org/t/impl-tryfrom-str-for-cstring/21407/8 "2024-08-20T17:43:38Z")

</div>

Yup, seems there is already optimization inside if &str is passed to avoid realloc. So I need for extra TryFrom impl

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [February 11, 2026, 5:43pm UTC](https://internals.rust-lang.org/t/impl-tryfrom-str-for-cstring/21407/11 "2026-02-11T17:43:54Z")

</div>

This topic was automatically closed 540 days after the last reply. New replies are no longer allowed.
