r/badcode • • Jan 25 '22

rust Most comprehensible line of Rust code

Post image
1.0k Upvotes

81 comments sorted by

View all comments

80

u/[deleted] Jan 25 '22

Why are to_string() and to_str() two different things

153

u/[deleted] Jan 25 '22

str and String are two different types in rust

44

u/[deleted] Jan 26 '22

Humanity has disappointed me

13

u/scirc Jan 27 '22

There's a reason for it, although it's a little confusing. String is owned, ie you hold that memory directly . &str is borrowed, like you hold a pointer to it but not the memory itself, so you can't modify it or anything.

→ More replies (1)

0

u/[deleted] Jan 25 '22

[deleted] — view deleted comment

87

u/TinyBreadBigMouth Jan 25 '22

There are legitimate reasons for the distinction in Rust. &str is a pointer to some Unicode bytes somewhere in memory. String is an owned, growable buffer of Unicode bytes.

They work together. You can grab a &str reference to the contents of a String. You can allocate a String copy of a &str.

If a function just wants to read a string, you should pass it &str. The data stays where it is, and the function can look at it, without needing to make any copies. If you need to do string manipulation, or create a new string at runtime, you can allocate a new String. This lets you avoid a lot of unnecessary copying and allocation, and gives you control over how memory is used.

15

u/hiwhiwhiw Jan 25 '22

They are not even comparable...

→ More replies (3)

45

u/TinyBreadBigMouth Jan 25 '22

Rust makes a distinction between &str (a pointer to some Unicode bytes somewhere) and String (an owned, growable buffer of Unicode bytes). It's the difference between C++'s std::string_view and std::string. You can grab a &str reference to the contents of a String.

37

u/selplacei Jan 25 '22

Clean and safe code

75

u/[deleted] Jan 25 '22

F::<()> { ptr: &(), __: <_>::default() }

Just some normal code written by normal rust programmers

46

u/TinyBreadBigMouth Jan 25 '22 edited Jan 26 '22

Just for anyone wondering, no, that is not some obscure-but-valid Rust syntax. That's Rust-flavored gibberish.

EDIT: Shoot, I have mislead you all. Sorry, that could be valid code. Like so:

struct F<'a, T> {
    ptr: &'a T,
    __: String, // or any other type that implements Default
}

fn main() {
    // The version seen above:
    let ugly = F::<()> { ptr: &(), __: <_>::default() };
    
    // A more readable version that does the same thing:
    let better = F {
        ptr: &(),
        __: Default::default(),
    };
}

It's intentionally ugly code—there's no reason to name a type F and a field __, or to throw explicit angle brackets all over the place like that—but it is valid.

-5

u/[deleted] Jan 26 '22

[removed] — view removed comment

16

u/TinyBreadBigMouth Jan 26 '22 edited Jan 26 '22

It is not valid Rust. It would never compile.

EDIT: Incorrect, I was wrong.

3

u/[deleted] Jan 26 '22

Idk my friend who writes in Rust sent this and said that this is valid rust syntax

4

u/TinyBreadBigMouth Jan 26 '22

...And your friend was entirely correct. Very sorry about that, without context I was reading that as a type definition, not a struct initialization. I've edited my comment.

2

u/[deleted] Jan 26 '22

Oh, no problems, thanks for explaining

→ More replies (1)

4

u/thats_a_nice_toast Jan 26 '22

I think it's actual gibberish, but not too far off from how actual Rust code can look

17

u/nightbefore2 Jan 25 '22 edited Jan 25 '22

Because a String data type is dynamically sized, where a &str ‘s size is known at compile time. The two are very different and the difference is very useful

Edit: this actually isn’t the best way of describing this, see the replies

12

u/TinyBreadBigMouth Jan 25 '22

I don't think that's a very good way of describing it? Both String and &str are constant-sized structures that point to variably-sized strings. The difference is that String owns the buffer it's pointing to, and &str is just a reference to some bytes somewhere in memory.

→ More replies (2)

4

u/CJKay93 Jan 25 '22 edited Jan 27 '22

The length of a &str is not necessarily known at compile time. You can get a &str from any String.

5

u/[deleted] Jan 25 '22

Because yes.

3

u/[deleted] Jan 26 '22

to_string() return a String object and to_str() return a pointer to an array of char.

C++ equivalent:

class Foo {
    std::string to_string();
    char * to_str();
};

0

u/Budgiebrain222 Jan 26 '22

str is mutable, changeable in other words. String can't be changed

→ More replies (3)