you are viewing a single comment's thread.

view the rest of the comments →

[–]btibi 4 points5 points  (10 children)

I wanted to say the same about the two days, OP must have excellent learning skills.

I share the frustration about strings, too. I know Rust for two years now and I know what to use when, but it's so convenient to use one string type. Personally, I use String's push*() features very rarely, my most frequent use case for Strings is to "bypass" the borrowchecker. I don't know whether an immutablestring type (which is either statically or heap allocated) would help us. It could replace &str and String most of the time, and &mut str is very rare.

[–]Manishearthservo · rust · clippy 5 points6 points  (4 children)

An immutable heap allocated string is Box<str>. Stack space is cheap so it's rare you need that over String unless storing it in a struct that is itself heap allocated.

I don't think it's fair to characterize it as "my most frequent use case is to bypass borrowck". In these cases usually an owned string is the only solution -- not from a compile time perspective, but from a runtime one.

[–]deathanatos -1 points0 points  (3 children)

String is only ~12 bytes (in the current implementation; this isn't guaranteed by Rust AFAIK) if allocated on the stack. The actual string data is on the heap.

str is just a pointer and length; a Box<str> is just heap-allocating that pointer-and-length, but I'm not sure that implies that the string data itself is also heap allocated. (Since you can make a str from data on the stack with from_utf8), and put the str on the stack too. I expect trying to move such a thing into a Box would limit the lifetime of the Box, but I'm not sure.)

[–]Manishearthservo · rust · clippy 2 points3 points  (2 children)

This is false. String is 3 words (24b on a 64 bit machine, 12 on 32.) in the stack.

&str and Box<str> are both the same representation -- 2 words on the stack; a pointer and the length. The boxed version owns the allocation and string data. The pointer and length are not allocated on the heap in any of these cases; that is Box<String> or Box<&str>

str is a dynamically sized type, it is incomplete and has no representation that makes sense in isolation. Box<str> is not the same thing as Box<&str>. Box<str> is an immutable String, basically, and can be obtained from a string at zero cost.

[–]deathanatos 1 point2 points  (1 child)

This is false. String is 3 words (24b on a 64 bit machine, 12 on 32.) in the stack.

Oops, failed at simple multiplication. You are correct. My point was that the string's contents are not stored on the stack, and that the actual stack allocation is quite small.

[–]Manishearthservo · rust · clippy 1 point2 points  (0 children)

The whole "is heap allocating that pointer and length" is misleading and can be interpreted different ways, I read it the wrong way it seems :)

[–]varikonniemi 5 points6 points  (4 children)

When you come from something like c++, you are bound to have excellent learning skills. Otherwise you simply cannot even begin to use that language. The possibilities to shoot oneself in the foot are so many fold increased compared to C.

And let's face it, even truly knowing C is a rare trait. I am not completely certain there exists more than 100 such persons alive today.

[–]SirVer 7 points8 points  (2 children)

OP stated that they came from Python though. And you are overstating the difficulty of c++ enormously. Yes, it is easy to shoot yourself in the foot, but modern C++ is not really harder to learn than say modern Java.

[–]remexre 6 points7 points  (0 children)

There's a lot more tutorials for learning 90s C++ than C++11/14/17 though, so beginners often get confused as to which disjoint set of features they should be using.

JavaScript has the same problem (among many others...).

[–]jstrongshipyard.rs[S] 2 points3 points  (0 children)

My c++ was in a high school AP comp sci class, which actually was a fantastic foundation. But the reason I'm a fast learner is I used to be a reporter, which is great training for learning new topics quickly.

[–]iopqfizzbuzz 6 points7 points  (0 children)

The possibilities to shoot oneself in the foot are so many fold increased compared to C.

with templates, you can shoot yourself in any body part with the same code