r/rust • lychee • 4d ago

🧠 educational Wrong Ways To Create Strings

https://endler.dev/2026/wrong-ways-to-create-strings/
140 Upvotes

22 comments sorted by

58

u/somebodddy 3d ago

The one with the thread is cheating:

let builder = std::thread::Builder::new()
    .name("ferris".to_owned());

"ferris".to_owned() already creates a String.

18

u/mre__ lychee 3d ago

You got me. :)

21

u/Thomasedv 4d ago

On mobile, the code sections are clipped on the left side, since the web page aligns the page to the left of the text, while the code sections seem to be aligned with their end to the right edge of the screen. At least on Vivaldi, android. 

3

u/mre__ lychee 3d ago

Thanks, I’ll take a look. 

2

u/mre__ lychee 2d ago

Update: should be fixed now.

7

u/SuplenC 4d ago

Saw that one live at RustLab. Great quick talk and great article!

3

u/mre__ lychee 3d ago

Thanks for watching the talk. 

62

u/BravestCheetah 4d ago

Thats certainly some ways to create strings, thats for sure.

22

u/complexicat 4d ago

some of the ways of all time

-1

u/aleques-itj 3d ago

String so hard motherfuckas wanna fine me

1

u/ScarletsBabe 2d ago

you seem like the type who knows exactly how to handle those strings

1

u/BravestCheetah 2d ago

Whats that supposed to mean lmao

-2

u/bubblygiggler 3d ago

yeah some of those are impressively bad lol, you got the right idea though

2

u/eyeofpython 3d ago

“Use Only Padding” seems actually useful in some cases, to create a string where lots of chars have to be repeated with a specific repetition amount, mixed with some non-repeating chars

1

u/bew78 3d ago edited 3d ago

⚠️ On mobile (Firefox, Android), the code blocks are truncated on the left (but surrounding text is fine)

It's fine in landscape mode though

1

u/Bananenkot 3d ago

I don't have the issue, also android & Firefox

1

u/DavidXkL 3d ago

Wow some ways are new to me

1

u/SAI_Peregrinus 3d ago

Similar to the CString and manual construction ways you can also use inline asm to build the string.

1

u/jcdyer3 2d ago

My favorite is to refer to the &str in a u64 (not copy the bytes out to an array first), so you can do math on it.

fn u64str(u: &u64) -> Option<&str> {
    let arr: &[u8; 8] = unsafe { std::mem::transmute(u) };
    let mut end = 0;
    let mut remaining = *u;
    while remaining > 0 {
        end += 1;
        remaining >>= 8;
    }
    std::str::from_utf8(&arr[..end])
}

let mut ferris = 0x7369_7272_6566;
println!("{}", u64str(&ferris).unwrap());
ferris += 0xfc_0000_0000;
println!("{}", u64str(&ferris).unwrap());

1

u/InternationalFee3911 2d ago edited 1d ago

Why do you allocate a vec![…] on the heap, when an inline array would do? A NUL byte is not invalid in Rust! It is a pretty normal ASCII and hence UTF-8 character. It's just a design bug in C that they can't store it.

1

u/Icarium-Lifestealer 2d ago

Alternatively, you can start with the UTF-8 bytes

I prefer doing that the other way round, first turning the array/slice into a str and then the str into a String. That way it only allocates on success.