Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I think your response is on point because Rust also seems to be a lot of what I want (GC not mandatory, perf, etc, etc, plus pattern matching). Private by default is literally a huge positive for me. But the terribleness of Rust strings is enough to put me right off of it.

I'm worried that all of us are waiting for something to replace C++ that can't happen.



When you say "terrible", are you complaining about having to write `.as_slice()` and `.to_string()` all the time?

For context for others who may not have a lot of Rust experience, Rust has two string types, the heap allocated `String` and the 'string slice' `&str`, which is a view of some string data anywhere in memory (possibly on the heap, possibly not). A little more info about these types: http://stackoverflow.com/a/24159933/1256624

At the moment, conversions between them are via explicit method calls, which can result in verbose code when handling them. An explicit conversion is required even for the super-cheap `String` -> `&str` conversion (which is just repackaging the 3 words (length, capacity, pointer) of String into the 2 words (length, pointer) of the &str).


That is part of the problem. The bigger part of the problem is comparing String's with constants. It simply doesn't work easily.

Also, start trying to do pattern matching on Vec<String> vs Vec<str> (and the wide variety of static, ref, etc versions there in) and you end up spending a lot of effort to do what is very basic in most languages that support generic pattern matching.

Try a simple use case, parse a command line argument array via pattern matching in a Rust program.


Comparing a constant:

  string.as_slice() == "foo"
"Parsing" command line args (it's not as slick as it could be, but it's not entirely horrible... for me as an experienced Rust user anyway):

  match std::os::args().as_slice() {
      [] => println!("need more arguments"),
      [ref x, ..] if x.as_slice() == "-h" => print_help(),
      [ref a, ref b] if a.as_slice() == "-f" => write_to_file(b.as_slice())
      args @ _ => println!("unrecognised arguments: {}", args)
  }
Of course, using a proper parser like getopts or docopt.rs would be better:

- http://doc.rust-lang.org/master/getopts/ - https://github.com/BurntSushi/docopt.rs

I think it gets smoother with practice (as in, with practice you're able to know intuitively when you need .as_slice and when a `ref` is required etc.), but yes, improving the ergonomics will be nice. Dynamically sized types will allow for String to be treated implicitly as a &str in many cases, so, e.g., my_string.trim() will work (it currently requires `my_string.as_slice().trim()`). And there is some loose talk about overloading pattern matching via the same mechanism, which would allow proper pattern matching on String with literals `"..."`.

> support generic pattern matching.

There's some subtlety here, e.g. Haskell doesn't support pattern matching on Data.Vector.Vector or Data.Text.Text (which are some of the closest equivalents to &[T], Vec<T> and String).


A) I understand how to make Rust do what I want with strings. B) Even with your examples, I think you've glossed over some of the even trivial use cases for dealing with Vec<String> vs Vec<~str> (or whatever the current nomenclature is). C) Even with your great comments you've pointed out that String manipulation in Rust is not "natural". Which is something that is a very high priority for me.

That said, easy string manipulation is a high priority feature for me. Non-private data access is a high priority feature for the original commenter. I worry that too many of us have expectations for Rust that are unlikely to be fulfilled.

That's not to say that the Rust team has set too high of barriers, only that we have elevated them to something they aren't. I'm less concerned with what the Rust team is delivering (which so far has been great) than with what people seem to be expecting (which is nothing less than a C++ replacement in all contexts).


The problem is that the string story is on two sides of a barrier erected in Rust very much by intention: To make memory allocation explicit, and to enable allocation-free operations as far as possible

This is why Rust will have you juggle String (a string with allocation) and &str (a "string slice"; a view into an allocation elsewhere) all the time.


Erlang does support pattern matching on binaries though, that that's really nice when splitting out and converting bits and bobs from binary data.


> I'm worried that all of us are waiting for something to replace C++ that can't happen.

From the early days of computing, history proves systems programming languages only get replaced when OS vendors push another language on their official tooling.

Apple is now pushing Swift and Microsoft, at very least, dumping C.

So will Mozilla push Rust as part of Firefox OS? If not, which OS vendor will pick it up?

Note this applies also to other "wannabe C++" replacements.


Rust looks very interesting for the embedded programmer. Imagine a leightweight OS with minimal runtime requirements, similar to Contiki[1], but done right.

[1] http://contiki-os.org/


Given your conclusion there, in what ways do you wish Rust strings were similar to C++ strings?


Sorry, that is a bad reading of my comment (which is my fault). Rust strings are in no way preferable to C++ strings (and in some ways worse). Which is a major failing of Rust FOR MY USE CASES.

That is to say, I had huge expectations for Rust, that it did not (yet) meet. It really made me think about what I wanted from a systems language, and what is possible.

Please don't read this as a screed against the future of Rust, just the present, which didn't really enthrall me enough to learn more of vs C++11


Rust strings are in no way preferable to C++ strings

I haven't been following Rust closely, so this is the first I've heard about the string/slice distinction. But I have spent a lot of time trying to remove unnecessary allocations/deallocations/copies from performance-critical, string-heavy C++ code. I use a combination of custom allocators and slice-like objects, but both of these approaches make it a hassle to interact with code using the natural std::string.

A pervasive distinction between strings that manage their own memory and those that don't, along with language and library support to make using them in combination straightforward would be a big win for me.


I think having felt pain with std::string, you would implicitly understand and enjoy working with the two string types in Rust. They do everything it sounds like you've been doing manually and inconveniently in your C++ code.


Agreed. I'd also make an analogy to C++ where you sometimes must hop between std::string and char*'s with ambiguous lifetimes. Not to mention various custom string types that any big app will have (ex. one that internally exploits jemalloc's allocator optimizations (i.e. exposing block overages), or does rope-style allocation, or intern's into a shared table, or whatever).


> this is the first I've heard about the string/slice distinction

Strings got re-done like ~2 months ago, so don't feel too bad ;)


I ask because I want to know what Rust's pain points are so that I can suggest ways for the devteam to do better. If you have any feedback, please make your voice heard while we still have time to consider it! :)


The history of gamedev (in particular) eschewing C++ for years until the consensus flipped gives me hope that something similar might happen here. The strings thing may take a similar path to so many aspects of C++ -- where it took years of folks like Scott Meyers and Josuttis and Herb Sutter and Alexandrescu talking through the details and rationale before enough people grew to like the decisions C++ made. Think of how many C++ programmers had to get their head around (say) RAII -- it probably first seemed horribly odd and weird, and now it's seen as a fundamental reason why it's a great language.


What "terribleness"?




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: