6 ms·
> An example for this is the lengthy page on Strings: https://doc.rust-lang.org/book/second-edition/ch08-02-string.. https://doc.rust-lang.org/book/second-editi
by jhoechtl 9y ago
> An example for this is the lengthy page on Strings: https://doc.rust-lang.org/book/second-edition/ch08-02-string.. https://doc.rust-lang.org/book/second-edition/ch08-02-string....
I don't know how the 2nd edition of the book deviates from the first one, but as you mention the strings section: I had installed rust with rustup, configured my editor environment, bookmarked and read tutorials and was serious about learning rust, dabbling with code snippets.
It was the exact chapter you mentioned, strings, which drew me away from rust in disgust. Four string types ... for the beginner? Seriously?
- baq 9y agoRust in general isn't really a beginner's language. You have to understand computers at a very low level to appreciate the design choices. Given some dedication, it'll help you at that but it's not easy, especially if you already know some higher level languages. It's probably easier to teach it as a first language than a second after Python or Java.
- squiggleblaz 9y agoCan you clarify your meaning? I'm a little confused. It sounds like you say "Rust isn't really a beginner's language... it's probably easier to teach [Rust] as a first language than a second after Python or Java". At first it sounds like a contradiction but when I inspect closer I realise there's alternative interpretations. Do you mean "learning a system's programming language is not helped and may even be hindered by experience with general purpose languages"? Are there any good beginners' systems languages, which help Rust?
- steveklabnik 9y agoI think your parent is saying "Rust isn't a great language to learn programming with", that is, if you've never programmed, Rust is not a great choice. I agree, but I don't think it's inherent; I think it's a lack of resources targeted at this demographic.
- syeaj 9y agoWhile the book is really top notch (I'm usually not a fan of programming language books), there's something to be said for not introducing all concepts at once. A scripting language has many of the same concepts, but not lifetimes, etc. Not to say objects are intuitive!
- steveklabnik 9y agoThanks! While this is true, those languages have stuff Rust doesn't as well. Take Ruby, for example: Rust doesn't have method_missing, or eigenclasses, or inheritance. "How do I know which method gets invoked" is much more complex.
- syeaj 9y agoOkay, I yield. I think you may be right. I've been thinking a lot about how I'll teach my children STEM topics and I've grappled with the idea of teaching Rust. One of the things I like about it is that you know the behavior at compile time, unlike Ruby or even C.
- curun1r 9y agoI do think a lot of it is inherent in the design choices that Rust has made. Rust, as a language, has a fairly large surface area. There's a lot of different syntactical and conceptual lessons that need to be learned by new Rust programmers. Compare that to, say, Lisp. My first programming class at Berkeley was taught in Scheme, which is basically Lisp. Within the first 20 minutes of the first day, the professor had not only gone over a bunch of introductory announcements, but he'd also introduced us to every bit of syntax we would learn in the course. The second lecture covered car/cdr and recursion. Beyond those two lessons, everything we learned was about the practice of programming, not the language we were programming in. Rust occupies a fairly unique position of being high level, with lots of abstractions to make experienced programmers more productive, and low level with fairly precise control over lower-level hardware concerns. That's really impressive, but I do believe that impressive power comes at the cost of an unavoidable learning curve. You'll never get a backhoe to be as simple to operate as a shovel. Which isn't to say that there isn't ample opportunity for the work that you and Carol are doing to make learning Rust easier--beginners now have it much easier than people like me that jumped in around 0.8. But it is to say that I don't think it's possible to ever make Rust as easy to learn as conceptually simpler languages. If I were designing a CS curriculum, I'd start new students with a Lisp dialect, then move on to Assembly (which, while hard, provides what is, to me, an essential view of the actual hardware that you're using) and then move on to the more practical languages. I'd still like to teach Rust, but it would come later to help introduce advanced concepts like compilers and operating systems.
- baq 9y agoSteve got it right. I'll clarify nevertheless: Rust and computer architecture (caveats apply) are (IMHO) very good subjects to be taught in tandem, as Rust tries very hard to provide safety features and high level abstractions that are impossible to implement in general, but quite possible in special, if very common, cases. Seeing where it is possible and where it's not and why should be an eye-opening experience. The operative word here is actually 'taught' - I think it's just too much stuff to internalize to be able to read a few tutorials and get it. It's a topic for multiple semesters of structured teaching going together with lots (lots!) of practical excercises. In that sense, it's easier to be taught if you don't know anything about programming than if you have a wrong mental model of how programming languages work, because you have to unlearn it first or open your mind in some other way for a different way of doing things. (The post that I was replying to asked why would you want four different string types - and you actually need at least six to interact with C libs and the OS...)
- neurotrace 9y agoWhere are you seeing four string types? As far as I know, there's only `String` and `str`. Heap allocated vs. stack/statically allocated. There's also things like `CString` and `OsString` but I don't think I've had to deal with those. If so, it was so similar to working with any other string that I didn't even notice. I'm still relatively new to Rust but this is my understanding. Please correct me if I'm wrong.
- Rusky 9y ago> Heap allocated vs. stack/statically allocated. `str` is very rarely stack allocated, and often points into a heap-allocated `String`. Further, there's an optimization on the table that will let `String`s point to static allocations. The distinction is rather owned vs borrowed. A `String` is in charge of the memory it uses, so it can grow it by reallocation, free it when it goes out of scope, etc. A `str` is not in charge of the memory it uses- it's only a pointer into some memory that something else owns, whether that's a static allocation or a `String` or some other data structure.
- neurotrace 9y agoAhhh, that makes a lot of sense. Thanks for the correction.
- eeereerews 9y agoA `str` is just an unsized blob of UTF-8 data. It's `&str` that is the (fat) pointer.
- Rusky 9y agoYou're right; I kind of left that out since it's not (currently) possible to have a bare `str` anyway and there are more ways to work with one than just `&str` (`Cow<str>`, `Box<str>`, `Rc<str>`, etc).
- kibwen 9y agoThere are two string types in Rust: String and &str. The CString and OsString types only exist for FFI and platform-dependent interop (because unlike Rust's string types, there's no guarantee that the rest of the world is UTF8-encoded). The only time you'd use CString and OsString (or see them get used) is at the very edges of your system, and you'd convert them into String or &str as soon as possible (or Vec<u8>, but that's hardly a "string type").
- m0meni 9y agoYour comment is confusing because they only mention two common string types, and then clearly explain the difference between the two (coming from someone who doesn't write any rust): > We’ll first define what we mean by the term string. Rust has only one string type in the core language, which is the string slice str that is usually seen in its borrowed form &str. In Chapter 4, we talked about string slices, which are references to some UTF-8 encoded string data stored elsewhere. String literals, for example, are stored in the binary output of the program and are therefore string slices. > The String type is provided in Rust’s standard library rather than coded into the core language and is a growable, mutable, owned, UTF-8 encoded string type. When Rustaceans refer to “strings” in Rust, they usually mean the String and the string slice &str types, not just one of those types. Although this section is largely about String, both types are used heavily in Rust’s standard library and both String and string slices are UTF-8 encoded.
- Rusky 9y agoIt's a systems language. C++ offers the same choices- std::string, std::string_view/char*, etc. Higher level languages can get away with simplifying this stuff; systems languages can't.
- shmerl 9y agoDo you mean ffi strings? At the basic level, there are two types, same as let's say C++ offers std::string, and char*. Strings are really a hard thing to begin with, many languages simply obscure their complexity.
- hsivonen 9y agoThere are way more than 4, but each one makes sense and has a purpose. Trying to conflate them would make things conceptually less clear. Also, somehow people seem to object less to Vec<T> vs. &[T] than String vs. &str. It's great that owned string with contents on the heap is clearly distinguished from a borrowed view into a string. In C, when you see a char*, do you own the string or are you borrowing a string? The type doesn't say. In C++17, however, the distinction is present: std::string vs. std::string_view. It's also great that Rust, unlike C or C++, distinguishes between strings that are valid UTF-8 and strings that came from a kernel that takes a GIGO position on encodings. Got a std::string in C++? Is it UTF-8 or garbage you got from the kernel? You don't know.
- comex 9y agoThe only thing that bothers me is that Rust’s standard library has no good solution for when you do want to take a GIGO position, such as if you need to interoperate over FFI with C/C++ code that does so. There’s Vec<u8>, but it’s missing a lot of the convenience methods and other functionality available for strings, and its Debug impl (i.e. string representation for debugging) gives you a list of numbers rather than, say, a best-effort UTF-8 interpretation. There’s also OsString on Unix, but that’s OS-specific and also is missing functionality. Unsurprisingly, there’s a third-party crate that has the functionality what I want, and thanks to Cargo that’s almost as good as it being in the standard library… but I still want there to be something in the standard library. :)
- LambdaComplex 9y agoOsString isn't Unix-specific. Or am I misunderstanding what you're saying?
- kibwen 9y agoI think what they're saying is that because Unix doesn't specify any string encoding at all, you can get GIGO by using OsStr on Unix.
- smoe 9y agoI'm coming mostly from Python for web development, so I haven't spent much time of my life thinking about strings. But I also usually have not really any clue what is going on under the hood when working with them. Which is fine, because most of the time it doesn't matter (as long as you don't do a+b+c... too much) in my work. What I like about the page is not that it makes strings easy to understand for a beginner, but that it doesn't hide difficulties about strings, that there are different ways to look at them and Rust doesn't presume a certain one. They say it isn't easy, then go on explaining why. To me this makes the steep learning curve feel justified. Where as some other language tutorials brush away any difficulty by saying "you don't need to understand that, just trust us." This is also a matter of perspective. I'm not looking at Rust for general web development (at least not as main language) but for more lower level stuff I want to get into. And least to me as a beginner in that domain it sounds very believable that those distinctions matter in that context
- steveklabnik 9y agoThe second edition is completely re-written. We had a discussion on the strings thing the other day: https://news.ycombinator.com/threads?id=steveklabnik&next=16550855 https://news.ycombinator.com/threads?id=steveklabnik&next=16... TL;DR, the language only has one string type. The stdlib has more. They’re all needed. Strings are hard.
- saghm 9y agoThe link you posted seems to show all of your recent comments, the most recent of which is not related to strings. Did you mean to link this? https://news.ycombinator.com/item?id=16546910 https://news.ycombinator.com/item?id=16546910
- steveklabnik 9y agoI did, sigh. I was on mobile, must have messed it up. Thank you.
- deleted 9y ago[deleted]
- deleted 9y ago[deleted]