10 ms·
Things I hate about Rust
- adamnemecek 7y agoThe strings actually make sense. The difference between String and &str is same as between Vecs and slices. Don't use OsStr unless you have to.
- woodruffw 7y agoAuthor here: I agree that they make sense! I think the nuance is between "makes sense" and "fits into the mental model Rust otherwise encourages," i.e. thinking about `T` and `&T` as the fundamental building blocks for ownership semantics. In that context, being unable to directly instantiate a `str` (or having `&String` be a thing that you can occasionally produce when you mean `&str) is deeply confusing to newcomers.
- jkoudys 7y agoI'd even made your same comment about Strings over on reddit last week - it's not like the ways it's doing it are wrong at all. It's more that newbies can suffer decision paralysis when you are obsessed with learning the "right way" to do something, but there's no clear answer. Do I `.map(String::from)`, do I `.map(From::from)` and let things infer? Do I `.map(|s| s.into())`? `.map(|s| s.to_owned())`? `.map(|s| s.to_string())`? You can drive yourself crazy over the most meaningless, pointless questions in your code. The surprising thing is, I find rust an incredibly effective language at sparing me from worrying about trivialities. Once you learn the slight differences in what all those things mean, you can be pretty direct. Overall I find rust one of the harder languages to write, but one of the easiest to read, and reading code is generally where you'll spend more time.
- ajross 7y agoThe problem is naming though. Calling something a "slice" connotes the existence of a whole somewhere from which the slice was taken. The string types just call themselves same thing with some funny syntax and oddly inconsistent capitalization and abbreviation conventions. This non-behavioral stuff matters. Almost literally no one who comes to rust understands the string types with any intuition initially. And strings are really important! Much the same thing can be said about the evolution of other areas of rust syntax (macros and attributes come immediately to mind). The final result is a collection of syntactic soup with (mostly) well-defined and internally consistent behavior, but with a syntactic expression that bears the archaeological scars of its evolution.
- adamnemecek 7y ago> The problem is naming though. Calling something a "slice" connotes the existence of a whole somewhere from which the slice was taken. The string types just call themselves same thing with some funny syntax and oddly inconsistent capitalization and abbreviation conventions. That is not the case.
- twic 7y agoThis diagram may help: https://docs.google.com/presentation/d/1q-c7UAyrUlM-eZyTo1pd8SZ0qwA_wYxmPZVOQkoDmH4/edit https://docs.google.com/presentation/d/1q-c7UAyrUlM-eZyTo1pd... Although it could really do with some more expository text!
- DrBazza 7y agoIf you think Rust strings are bad, you're obviously never developed on Windows. From my failing memory, MBCStr, CComBStr, BStr, CString, XLString, and then all the unicode versions. Then you're developing in C++, so you also have std::string and std::wstring, oh and u16string, and u32string. Plus char, and wchar.
- woodruffw 7y agoAuthor here: I've spent about half a decade writing Win32 applications, including things deep in NTFS. I agree completely that everything about string typing on Windows is worse, especially once you stray off of the happy path. But the post isn't intended to equivocate against a ecosystem with 40 years of legacy APIs in it; it's to nitpick a new ecosystem that I otherwise love ;)
- DrBazza 7y agoAgreed. Abstracting the OS and its different string versions is hard. Even more so when your language has to work on multiple OSes. And then add into the mix non-mutable by default. This blog post [1] may interest you (if you haven't already read it). [1] https://fasterthanli.me/blog/2020/i-want-off-mr-golangs-wild-ride/ https://fasterthanli.me/blog/2020/i-want-off-mr-golangs-wild...
- eska 7y agoI don't see an alternative to interfacing with that though. If we were to just use UTF-8 strings for everything then there would be files we couldn't handle in various OSs for example. So while it's a bit unwiedly to use filepath.to_lossy_str() etc. I prefer it to a leaky abstraction that hides necessary complexity.
- jcranmer 7y agoIt wouldn't be an issue if OSes forbade non-UTF-8 paths... It's the unfortunate case that we have OS environments that are usually, but not always, UTF-8, and there's no clear, reliable indication of whether or not UTF-8 is to be expected.
- dijit 7y agoFor getting a users homedir I fall back to POSIX[0] (which windows follows in this instance fwiw). There are some things that I'm bitten by as a rust newbie though; strings are definitely one of those, since it's more difficult to reason about strings as they're heap allocated, where as most "normal" types are not, thus to use Strings you have to learn the borrow checker.. Another issue I have is crate versions, and compiling and including _multiple versions_ of the same crate, because that crate is also a dependency on one of the crates I'm using. Yet, another is crates that only support the nightly compiler, which you're not supposed to use for production, there needs to be some way of delineating the "stable" version of a crate with the "potential future" version of a crate. Even if you stick to a version and the nightly compiler supercedes then you end up with the same error: ------8<------ Caused by: process didn't exit successfully: `/Users/dijit/projects/rust/Rocket/target/debug/build/rocket_codegen-6474f79f6391da32/build-script-build` (exit code: 101) --- stderr Error: Rocket (codegen) requires a 'dev' or 'nightly' version of rustc. Installed version: 1.43.1 (2020-05-04) Minimum required: 1.33.0-nightly (2019-01-13) ------>8------ [0]: in POSIX the OS _must_ set $HOME: https://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap08.html https://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd...
- gfxgirl 7y agoI'm not sure about home dir but a thing I've needed is a standard place to store app stuff. On Windows it's a mess. There's AppData/Local, AppData/LocalLow, AppData/Remote and many apps store a ton of stuff in their app folder in c:\programs etc... On Mac I guess it's Library/Application Data but there's a bunch of other folders under Library. On Linux I read somewhere it's .config but there was no .config on my Linux installs and it seems strange to me that apps should be required to make this folder vs the OS and that the OS doesn't tell me where it is it's just convention. But in any case, I think I wish some standard library handled this.
- dijit 7y agoYes, I understand, and I feel your plight, as it stands this is what I would use: $APPDATA on windows[0] or $LOCALAPPDATA $XDG_CONFIG_HOME on linux[1], which is where you're getting the ~/.config from. For MacOS, it's a mess[2], you should probably use ~/Library/Application Support/script_name/ for most things though. [0]: https://ss64.com/nt/syntax-variables.html https://ss64.com/nt/syntax-variables.html [1]: https://wiki.archlinux.org/index.php/XDG_Base_Directory https://wiki.archlinux.org/index.php/XDG_Base_Directory [2]: https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html#//apple_ref/doc/uid/TP40010672-CH2-SW1 https://developer.apple.com/library/archive/documentation/Fi...
- http-teapot 7y agoI have the same problem with strings as well. I think I understand the difference between String and &str but I wish it’d automatically convert String into &str when a function expects it. Example: `std::env::var(“SOME_PARAM”).unwrap_or_else(|_| “localhost”.to_string())` It just seems odd to me, something tells me there is a better way to do this but I couldn’t find it.
- woodruffw 7y ago> I have the same problem with strings as well. I think I understand the difference between String and &str but I wish it’d automatically convert String into &str when a function expects it. Yep! My understanding is that `AsRef<str>` is the right way to do this automatic conversion but I learned that by reading library code, not from the standard documentation. It'd be awfully nice if the compiler could do those sorts of conversions automatically; injecting `AsRef` everywhere adds a lot of visual clutter.
- Ciantic 7y ago> injecting `AsRef` everywhere adds a lot of visual clutter I agree! `fn foo<T: AsRef<str>>(s: T)` This is so difficult to read, one has to jump back and forth with eyes to make any sense of these. Why couldn't there be easier syntax for "AsReffing"? E.g. `fn foo(s: @str)` I invented @-sign there.
- stuhood 7y agoCan use `impl Trait` I think? ``` fn foo(s: impl AsRef<str>) ```
- steveklabnik 7y agoSyntax like @T was actually a type in ancient Rust. People really, really hated having even more sigils in the language, so we ended up removing them. There was also ~T.
- jcranmer 7y ago
- kris-s 7y agoMy biggest gripe with Rust is that is doesn't spark joy. I intellectually agree with a lot of its goals but every time I write some Rust I just find it dreary. Maybe it's the ugly syntax, maybe it's the "code feel" of interacting with a grumpy compiler. That said I know a lot of folks really dig it and think it's a breath of fresh air; more power to em'.
- raphlinus 7y agoAdding my voice to the "it sparks joy" crowd. I think it depends a lot on who I am and what I'm trying to do; I'm really motivated by performance, and when I'm writing in Rust I feel I can do that and also maintain higher level structure.
- dijit 7y agoThat's a shame, I find Rust (and, ruby) spark incredible amounts joy to code in. But I find that Go does not spark joy for me. ('.Dial'?!, first character case denotes public/private D:). I guess that it is very subjective.
- leaveyou 7y ago'.Dial' ?? What else would you expect instead of Dial ? If you get demoralized from an imperfect function name you are in the wrong domain of activity.
- dijit 7y ago.connect() or connection.new() like everything else. Python: https://wiki.python.org/moin/TcpCommunication#CA-b22bc303d6d0b4c7af1a18623aacd1d0900b6ff3_12 https://wiki.python.org/moin/TcpCommunication#CA-b22bc303d6d... Ruby: https://ruby-doc.org/stdlib-2.5.1/libdoc/socket/rdoc/TCPSocket.html https://ruby-doc.org/stdlib-2.5.1/libdoc/socket/rdoc/TCPSock... Rust: https://doc.rust-lang.org/std/net/struct.TcpStream.html https://doc.rust-lang.org/std/net/struct.TcpStream.html Julia: https://docs.julialang.org/en/v1/manual/networking-and-streams/#A-simple-TCP-example-1 https://docs.julialang.org/en/v1/manual/networking-and-strea... C++: https://github.com/KMakowsky/Socket.cpp/blob/master/Socket.cpp#L73 https://github.com/KMakowsky/Socket.cpp/blob/master/Socket.c... Win32 C: https://docs.microsoft.com/en-us/windows/win32/api/Winsock2/nf-winsock2-connect https://docs.microsoft.com/en-us/windows/win32/api/Winsock2/... Linux C: http://man7.org/linux/man-pages/man2/connect.2.html http://man7.org/linux/man-pages/man2/connect.2.html Swift: https://github.com/apple/swift-nio/blob/master/Sources/NIOEchoClient/main.swift#L96 https://github.com/apple/swift-nio/blob/master/Sources/NIOEc... Java: https://docs.oracle.com/javase/7/docs/api/java/net/Socket.html#connect(java.net.SocketAddress) https://docs.oracle.com/javase/7/docs/api/java/net/Socket.ht... Javascript: https://gist.github.com/tedmiston/5935757#file-nodejs-tcp-example-js-L37 https://gist.github.com/tedmiston/5935757#file-nodejs-tcp-ex... Anyway, it's not about being demoralised, it just doesn't "spark joy". :)
- jen20 7y agoThe `dirs` crate has been recently forked and revived - the new home is here: https://github.com/xdg-rs/dirs https://github.com/xdg-rs/dirs (and the crate is named `dirs-next`). Given the relatively sparse nature of the standard library, and the cost of making mistakes (std::sync::mpsc, anyone?!) I can see why this is omitted. IMO it’s a fair criticism that pulling in lots of crates is less than ideal, though.
- rukittenme 7y ago> std::sync::mpsc, anyone?! Can anyone expand on this? I'm not well versed in Rust lore and have just used that module in a recent learning project.
- jankassens 7y agoFound this post https://stjepang.github.io/2019/03/02/new-channels.html https://stjepang.github.io/2019/03/02/new-channels.html with a link to a comment on GitHub: https://github.com/rust-lang/rust/pull/42397#issuecomment-315867774 https://github.com/rust-lang/rust/pull/42397#issuecomment-31...
- jen20 7y agoYes - sorry, this is the comment I was referring to. I'm not aware of _bugs_ in `std::sync::mpsc`, more concerns around things which could have been done better which are now not possible to retrofit because of the guarantees of the standard library with respect to backwards compatibility.
- cormacrelf 7y agoIt's cool, and was included probably because Go has channels and that user-base is valuable. But now there's crossbeam-channel, which is significantly faster, has handles that are more shareable, and it removes the `Result` wrapper around all the operations. So a drop-in replacement is technically possible, but doesn't get rid of `unwrap` everywhere, and there's no steam behind replacing the guts of it because the lesson that was learned says 'just deprecate it, let it live in a crate'.
- sergiotapia 7y agoThat string situation in Rust sounds like a lot of cognitive load. I wonder why can a language like Nim (which competes with Rust) have such wonderful dev ux where you can just go `string` and Rust has this thing?
- xrisk 7y agoJust a guess but probably because Nim uses a garbage collector and probably doesn't make the same memory safety guarantees that Rust does.
- nimmer 6y agoThe garbage collector is optional and Nim is memory safe. Just like Rust, you can unsafe structures when using libraries in C/C++.
- wchar_t 7y agoEach string in Rust has a specific purpose. OsString, for instance, is for storing data which might be in, say, UTF-16 instead of UTF-8.
- dpbriggs 7y agoRust prefers explicit, deal-with-the-complexity types. The OP lumped ownership-related types (String/&str) and the nightmare that is filepath encodings (OsString/OsStr). The first case is related to ownership/borrowing as these concepts are fundamental in systems languages. The second case is a hint that filepaths are more complex than a single string type can represent.
- jcranmer 7y agoThere's two separate issues in play here: The first is the distinction between "owned" (i.e., heap-allocated) and "borrowed" (types). Arguably, there could have been better language support for this to fix this issue. The second is the fact that the underlying OS rules for strings and paths do not conform to any sane string requirements on most systems. If you collapse this into normal strings, then you end up with situations where you can't interact with your OS properly.
- ChrisSD 7y agoPerhaps Rust should make clearer that `String` is a string buffer and not just a string type. Other than that I think Rust does need different string types for different situations. As to the standard library, I've actually started to think it's too big. I know this may sound like heresy for someone coming from a language with a big standard library. However, I think it should do whatever needs compiler support and have some traits for easier interop and that's about it. EDIT: Perhaps also some functions to help handle the more fiddly UB risks. Rust has a great, easy to use ecosystem. More needs to be done to point out the trusted and well tested crates and to point people in that direction. The standard library itself is much harder to contribute to and has stability guarantees which can potentially make mistakes costly in the long term.
- twic 7y agoThere's definitely a theory that the pairs of view and buffer (or borrowing and owning) types should have been more consistently named. I think steveklabnik usually manifests in these threads to express it. But it's something like: str is to String as &[] is to Vec as Path is to PathBuf ... and the names should reflect that.
- steveklabnik 7y agoYep, you nailed it. I wouldn't change Vec though. I would make the string types more consistent with the path types. The reason is that slices are more general than just vectors, whereas str/String and Path/PathBuf are virtually always only used together.
- twic 7y agoFair enough. Although i'd rename Vec to List, because variable-length sequences are not vectors, and there's no need to propagate that mistake any further!
- steveklabnik 7y agoThe problem is, some people think List means some form of linked list, not what a vector is. Names are hard.
- twic 7y ago> No current way to get the user’s home directory. std::env::home_dir() is explicitly marked as deprecated, and the documentation encourages users to rely on the dirs crate (which is currently archived on GitHub). Yeah, this is a bit of a farce. On the one hand, it seems fair to me to not have this in the standard library - it's not something that needs special cooperation with the compiler or runtime, it's not something universal (what would it do on an embedded microcontroller?), and it doesn't matter if different crates use different implementations. On the other hand, deprecating a standard library function in favour of a crate owned by a random user seems like an unforced error. There's discussion here: https://www.reddit.com/r/rust/comments/ga7f56/why_dirs_and_directories_repositories_have_been/ https://www.reddit.com/r/rust/comments/ga7f56/why_dirs_and_d... Including the fact that there is an actively maintained alternative: https://github.com/brson/home https://github.com/brson/home And a clue to why those repos are archived: > I was wondering the same thing and contacted the author. He said that some people had "lost their shit" over some of the things he mentioned about rust and that to help them calm down, they will be archived until 2022. !
- msla 7y ago> it's not something that needs special cooperation with the compiler or runtime, it's not something universal (what would it do on an embedded microcontroller?) It's filesystem access, like the open file and read file functionality, so it needs some runtime support, and all filesystem access would fail on embedded microcontrollers, so I'm not seeing why it shouldn't be in the standard library.
- AlotOfReading 7y agoPlenty of μC's have filesystems, probably the majority nowadays. They aren't necessarily running an operating system with a concept of users though. Besides, in C the file commands can be used to do non-file IO, so those functions still have a use even without a FS.
- finnthehuman 7y ago>probably the majority nowadays I'd dispute that on the grounds that a microcontroller with a filesytem is likely doing something complicated enough that it lives on a board with at least one other microcontroller that doesn't. I'll even go as far to say it's more likely that the majority of microcontrollers don't write to mutable storage other than RAM (if they have it at all) once they leave the factory.
- thurn 7y agoSkipped over my personal biggest annoyance, the inability to abstract over mutability (example: https://www.reddit.com/r/rust/comments/2a721y/ https://www.reddit.com/r/rust/comments/2a721y/)
- vmchale 7y agoI quite like the way Rust handles strings. Better than Haskell.
- nilkn 7y agoI think it's pretty bad in both languages. In Haskell it's trivial to understand the difference between a ByteString and Text. The names give it all away. You do need to know the difference between lazy and strict values, but that's universal to the language and not hard to understand. I'd say that strings in Haskell are more annoying than hard to understand. In Rust, even figuring out why some of the string types exist in the first place and what they mean is hard, and you still have the interconversion problem, which now is not only annoying but also difficult to understand.
- cryptonector 7y agoHaskell implementing strings as lists of characters is a problem. Basically, you can't use them for anything performant. A proper string library should be a Functor, naturally, but it should also be much more like a rope and much less like a list under the covers and in its API.
- nilkn 7y agoI agree, but that's why you use Text and ByteString. Annoying? Yes. Hard to understand? Not at all. Rust strings to me are both annoying and hard to understand and explain.
- api 7y agoReading this confirms that I was right to pick Go for a couple things. My very strong opinion is that the language should impose as little cognitive load as possible. Rust seems like it makes you think a lot about Rust, which means you are not thinking about the problem you are trying to solve. Brain cycles are a finite resource. I don't want to diss Rust too much though. I see it as a potential future replacement for C/C++ for bare metal stuff where you really do want to hand craft the code for maximal performance or directly interface with hardware. That is a different niche than building higher level stuff, and I don't think anyone has managed to make a language that is good for both (yet?).
- eximius 7y agoCan't agree. Rust's goal, if you can call it that, is to make you think about things you should be thinking about anyway. Coming from C, you should be thinking about lifetimes and mutability and sharing. If you dont, your program may work but be buggy. Rust forces you to think about that. Go does the same thing in different ways at different costs. It trades memory/performance so that you don't have to worry about allocations and simply doesnt use standard shared mutability patterns (not exactly, but channels take up most of the same space).
- mwindow 7y agoI don't understand why the parent post of yours is getting this much downvotes. What's wrong with using a GC'ed language when performance requirements are permissive?
- eximius 7y agoThat's not precisely their statement even if it is their intention. I don't think anyone could argue that statement (though some may deny your axiom of lax performance requirements). I think people are taking issue with the claims that _Rust_ is imposing the cognitive burden. As I said, these burdens already exist, languages just weren't forcing people to deal with them.
- thsowers 7y ago
- nagarjun 7y agoAs a Rust beginner, the point about strings really resonated with me.
- dpbriggs 7y agoOver time I found that I appreciated it more and more. There's a lot of weird and wonderful edge cases and it's a joy to see them encoded in the type system. You don't have to resort to stackoverflow questions like this: https://stackoverflow.com/questions/2050973/what-encoding-are-filenames-in-ntfs-stored-as/2051018#2051018 https://stackoverflow.com/questions/2050973/what-encoding-ar...
- beckler 7y agoNot being able to execute in the current shell has been a bit of a pain... Want to write a environment variable that lasts the lifetime of the current shell session? No way to do it.
- eximius 7y agoIs that a Rust problem? How would you normally do it that fails in Rust?
- deleted 7y ago[deleted]
- deleted 7y ago[deleted]
- fgnewsom 7y agoEND THE LOCKDOWN! FUCKING NAZIS!
- efiecho 7y agoThere are also things to hate about Rust, that are not technical at all. I saw a comment on another site in a post about why so few developers are using Rust. I think it hit the head on the nail: > You left out the part about not wanting to be in a project that spends more of its time debating what a man should call his vagina than working on actual work.
- StillBored 7y agoA few of my own: Declaring/initializing a medium sized array of structs, is simply too verbose if you have meaningful structure names. What is a few lines in C explodes into pages in rust. Type inference on declarations but, not function parameters, WAT! The scoped constructor syntax is designed to miss the concept of RAII. Array's by themselves are nearly useless, might have just made Vec<> the default array type. Variables end up being &mut even when its not necessary because the compiler forces the attribute to be carried along in a number of cases when its not rightfully needed. As I've complained about before, many of the 3rd party cargo libraries need additional traits before they can be mixed with threads/mutexes unless your willing to use unsafe. Then there are all the bad style choices, starting with the javascript like nested calling (aka obj1.call().call2().call3().call4().call5();) chains spread over whole pages of code. Mismatch braces (yah I know this one is everywhere in C/javascript/java too, doesn't make it right). Trait declarations that are scattered everywhere rather than being centralized like a C++ class.
- estebank 7y ago> Type inference on declarations but, not function parameters, WAT! This is a hard restriction in place on purpose. The compiler could perform inference for the input and output types as part of the language but that means that the actual low level signature of your function could change by changing either the implementation or the callers. This is a compromise we must have to avoid surprises in real programs that have public APIs. On the flip side the compiler doesn't have this restriction for inferring the return type. If you write a function signature `fn foo() -> _` with a body that can resolve to an unambiguous type, the compiler will give you a structured suggestion for the correct type. This way you get the benefit of inference when developing and the benefit of type safety and unambiguous documentation of your code at the cost of mild inconvenience. > Array's by themselves are nearly useless, might have just made Vec<> the default array type. I'm not sure what the actionable recommendation is here. Is it to have made [] the syntax for Vec? If so, that would have made Vec special from the language's point of view, when it doesn't need to. Today you can reimplement all of Vec in your own Rust code, but you can't for arrays, because the compiler and language need specific memory layout information about them. > Mismatch braces Could you expand? I'm intrigued what you mean by this. > Trait declarations that are scattered everywhere rather than being centralized like a C++ class. This enables very flexible design and composition of behavior. It feels very weird when coming from an OOP background, but I've come to prefer it after a while.
- the8472 6y ago> No way to invoke a command through a system shell. Yes, I know that system(3) is bad. I think a crate that offers more shell-like convenience functions/macros for scripting would be more powerful than just adding a system()-equivalent to the standard library.