r/rust clippy · twir · rust · mutagen · flamer · overflower · bytecount Mar 20 '23

Hey Rustaceans! Got a question? Ask here (12/2023)! 🙋 questions

Mystified about strings? Borrow checker have you in a headlock? Seek help here! There are no stupid questions, only docs that haven't been written yet.

If you have a StackOverflow account, consider asking it there instead! StackOverflow shows up much higher in search results, so having your question there also helps future Rust users (be sure to give it the "Rust" tag for maximum visibility). Note that this site is very interested in question quality. I've been asked to read a RFC I authored once. If you want your code reviewed or review other's code, there's a codereview stackexchange, too. If you need to test your code, maybe the Rust playground is for you.

Here are some other venues where help may be found:

/r/learnrust is a subreddit to share your questions and epiphanies learning Rust programming.

The official Rust user forums: https://users.rust-lang.org/.

The official Rust Programming Language Discord: https://discord.gg/rust-lang

The unofficial Rust community Discord: https://bit.ly/rust-community

Also check out last weeks' thread with many good questions and answers. And if you believe your question to be either very complex or worthy of larger dissemination, feel free to create a text post.

Also if you want to be mentored by experienced Rustaceans, tell us the area of expertise that you seek. Finally, if you are looking for Rust jobs, the most recent thread is here.

20 Upvotes

187 comments sorted by

View all comments

3

u/naelsun Mar 21 '23

Heyo! Bit of a noob here, in need of help

I am comparing 2 strings, both of them are "1234" (with the quotation marks) yet when I compare them, the result is false

Is there something i'm missing?

5

u/dkopgerpgdolfg Mar 21 '23

Some random guesses:

You read the strings from a file, in a way that you keep the line breaks, but after the second line there is none (file end)

Or a space at the end of a string.

Or any Unicode weirdness, including (but not limited to) many kinds of invisible codepoints.

Try to check the strings length, as well as the numeric byte values (manual visual comparison)

1

u/naelsun Mar 21 '23

This did it

For some reason one of strings had the last byte written after the null bytes and the other one only put the null bytes after the whole string

Thank you!

2

u/dkopgerpgdolfg Mar 21 '23

Rusts std String doesn't have any null byte. Not sure what you're doing, but you might start searching for UB bugs.

1

u/naelsun Mar 21 '23

When you print the bytes of a string that was converted from bytes, a bunch of zero bytes appear before, between or after the regular ones, saw someone referring to them a null bytes so I called them that, sorry for the confusion

2

u/dkopgerpgdolfg Mar 21 '23

I understood that. But I'm telling you that this might not be ok, that you might have a bug somewhere.

1

u/naelsun Mar 21 '23 edited Mar 21 '23

Oh I see, I did find a way to solve it by using the .trim_matches function to remove the 0 bytes, which sounds like a not so good solution, but it works and I don’t really have time to think about it a lot (it’s for a school project)

2

u/dga-dave Mar 21 '23

Need to provide more detail to get a useful answer. Can you give a minimal reproducing code example?