6 ms·
Rust Cookbook
- kungito 6y agoWhat a coincidence. I've just used it 3 times in the past 2 hours to find code for some uuid and statistical cases. I have to admit, it's amazing for a quick lookup of common use cases when you don't want to think too much or remember things.
- pixel_tracing 6y agoWas hoping I could find something on changing a string in place (and efficiently) why is this such a difficult thing to do in rust? I am trying to build a parser combinator library in rust
- deleted 6y ago[deleted]
- tupshin 6y agoRust strings are UTF-8, and UTF-8 characters vary in byte length, therefore replacing same length character sequences don't guarantee that they will fit in the same number of bytes.
- jessegrosjean 6y agoRust uses very similar API to other languages for this: https://doc.rust-lang.org/std/string/struct.String.html#method.replace_range https://doc.rust-lang.org/std/string/struct.String.html#meth... The problems you are likely running into are higher level mutability issues. The above linked function requires &mut self, which means you need a mutable string. The above linked documentation has an example of using the `mut` keyword to declare a mutable string that you can replace_range on.
- bluejekyll 6y agoYou can modify strings in place, but not their length, which makes what your talking about complex if you need to support utf8. Here’s the str method: https://doc.rust-lang.org/std/primitive.str.html#method.as_bytes_mut https://doc.rust-lang.org/std/primitive.str.html#method.as_b... If you want an string that is mutable, can be extended, etc. You want to use String, and that has plenty of methods for mutation. Remember, Rust strings are all utf8, and part of the safety contract is that they’re always valid.
- the8472 6y agoclear() + push_str() to reuse the storage to assemble a new string. replace_range() to splice something in the middle. This may require the tail to be moved around when lengths don't match. Note that Strings are stored as utf8, so a substring with the same number of characters can still result in a different number of bytes and thus require memmoves.
- atbpaca 6y agoI love Rust, I just wish it could get rid of semicolons. Quite a lot of discussions around this.
- kthxb 6y agoof all the complaints to have...
- Tade0 6y agoAny such move will eventually result in automatic semicolon insertion in some shape or form. What's wrong with semicolons anyway?
- matesz 6y agoNothing wrong with semicolons. IMO they make code look a little bit less readable - you need to look at semicolon while you could be just looking at the empty line. One of the reasons people like python.
- colejohnson66 6y agoThis is a valid point. Without the context, the “everything is an expression” forces one to look for a line without a semicolon to see the expression result. In a 5 line function, that’s easy, but in a nested match, it can be hard.
- unanswered 6y agoIt can only possibly be at the end, so it's not really hard?
- deetsb 6y agoI wasn't a fan at first (coming from Python), but I really like that it allows you to make expressions into statements (I'm sure there's more going on then just that, but it was pretty cool to see that if/match are generally expressions but if I don't want them to be, ";" can change that!).
- protoman3000 6y agoI like this book a lot, but the writers use an external package to handle errors. Why don’t you just return a Result<T, Box<dyn Error>> instead?
- swsieber 6y agoDisclaimer: takes this with a grain of salt as I do use Box<dyn Error>. But I have a couple of things to note. 1. error chain is not the recommended way, despite being in the book. It needs updating! 2. The error trait in the std has come a long way since the book was originally written 3. You still might want some nicities that that Box<dun Error> doesn't provide you, and so you'd define an enum. You can do it manually, or use a library like anyhow or thiserror depending on if you're an app or library. 4. Rust error handling has been in a lot of flux, but generally improving. It probably does deserve a dedicated section in the book. I recommend this blog post: https://nick.groenen.me/posts/rust-error-handling/ https://nick.groenen.me/posts/rust-error-handling/
- michael_j_ward 6y agoJust adding this video [0] that both gives a mental framework for designing errors and a "lay-of-the-land" for current libraries. Jane, the speaker, also heads up Rust's error handling project group [1], and gives her thoughts on future design / work. [0] https://www.youtube.com/watch?v=rAF8mLI0naQ https://www.youtube.com/watch?v=rAF8mLI0naQ [1] https://blog.rust-lang.org/inside-rust/2020/09/18/error-handling-wg-announcement.html https://blog.rust-lang.org/inside-rust/2020/09/18/error-hand...
- gsserge 6y ago> I recommend this blog post: https://nick.groenen.me/posts/rust-error-handling/ https://nick.groenen.me/posts/rust-error-handling/ This blog post is good indeed. The discussion on /r/rust linked at the end of the post is also worth reading. Another good and light introduction is http://www.sheshbabu.com/posts/rust-error-handling/ http://www.sheshbabu.com/posts/rust-error-handling/ And a more in-depth look https://blog.burntsushi.net/rust-error-handling/ https://blog.burntsushi.net/rust-error-handling/
- 6y ago
- carols10cents 6y agoNote: The Rust Cookbook has existed for 4 years now, but it's still unofficial and somewhat out of date because we don't have enough people maintaining it.
- liquidify 6y agoIf this is already out of date, what will prevent it from complete rot over time?
- steveklabnik 6y agoMore people interested in contributing to bring it back up to date.
- mwww2 6y agoThank you for this resource!