2

I'm trying to concatenate two strings (&str) or convert a byte array in a string in Rust without using std. I saw core::str::from_utf8 but that's not what I'm looking for.

I'm searching something like

let b: [u8; 2] = [97, 98];
let result: &str = core::str::array_to_string(b); // "ab"

or

let a: &str = "Hello ";
let b: &str = "world !";
let result: &str = core::str::concatenate(a, b);
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
centipede_
  • 79
  • 9
  • 2
    See also [How do I concatenate strings?](https://stackoverflow.com/q/30154541/155423). Notably, the first sentence: *When you concatenate strings, you need to allocate memory to store the result*. In a no_std environment, you don't have an allocator, so you need to do the allocation for yourself. This is often just a "big enough" `u8` array on the stack. – Shepmaster May 08 '19 at 16:51
  • 1
    See also [Is there any way to return a reference to a variable created in a function?](https://stackoverflow.com/q/32682876/155423) and [Return local String as a slice (&str)](https://stackoverflow.com/q/29428227/155423) for discussion why your proposed functions cannot make sense, even when you have access to the standard library. – Shepmaster May 08 '19 at 16:52
  • 2
    Also, `str::from_utf8` appears to be **exactly** what you are asking for in your first example, which doesn't seem to involve *concatenation* at all. It also has the same problem of nothing owning the underlying bytes. – Shepmaster May 08 '19 at 16:54

0 Answers0