22 ms·
Rust Language Cheat Sheet
- alpaca128 5y agoMy favorite is still the Rust Container Cheat Sheet[0] from the Rust Performance Book[1]. Great overview on just one page - that's what I call a cheat sheet. Edit: Also recommended: The Little Book of Rust Macros[2] [0] https://docs.google.com/presentation/d/1q-c7UAyrUlM-eZyTo1pd8SZ0qwA_wYxmPZVOQkoDmH4/edit#slide=id.p https://docs.google.com/presentation/d/1q-c7UAyrUlM-eZyTo1pd... [1] https://nnethercote.github.io/perf-book/title-page.html https://nnethercote.github.io/perf-book/title-page.html [2] https://veykril.github.io/tlborm/introduction.html https://veykril.github.io/tlborm/introduction.html
- est31 5y agoAnother container cheat sheet: https://i.redd.it/220xo2f6wci51.png https://i.redd.it/220xo2f6wci51.png https://oldreddit.com/r/rust/comments/idwlqu/rust_memory_container_cheatsheet_publish_on_github/ https://oldreddit.com/r/rust/comments/idwlqu/rust_memory_con...
- alpaca128 5y agoThanks, that looks helpful too. Note your second link seems to be missing a dot between "old" and "reddit".
- est31 5y ago> Note your second link seems to be missing a dot between "old" and "reddit". Oh indeed. I visited the link while logged in, but to share links I make the pre-redesign explicit. I guess a mistake occured during that. Can't edit the post any more, but the fixed link is: https://old.reddit.com/r/rust/comments/idwlqu/rust_memory_container_cheatsheet_publish_on_github/ https://old.reddit.com/r/rust/comments/idwlqu/rust_memory_co...
- raphlinus 5y agoThanks, appreciate the shout-out. I'll give a little background how that came about. At the time, I was promoting the use of Rust within Fuchsia. The team was largely composed of badass C++ programmers, who of course were very familiar with reasoning about programs in terms of exactly how things were laid out in memory. The official Rust documentation was at a more abstract level with respect to a lot of this stuff, in many case careful not to overspecify because a lot of the details of memory layout are not normative and may in fact change. In explaining the most common and important Rust data types, I found these kinds of diagrams useful, and it grew into the page you see there.
- gilnaa 5y agoGreat work! "Allocate T bytes on stack bound as x" may sound a bit like T is a size instead of a type.
- golergka 5y agoSimilar cheatsheet for most of modern languages: http://learnxinyminutes.com http://learnxinyminutes.com
- rob74 5y agoCool, but doesn't really make Rust less intimidating for novices I'm afraid. I mean, it's been some time since I last saw a language that needed a cheat-sheet entry for concatenating strings: format!("{}{}", x, y)
- gourlaysama 5y agoTo be fair, this is more a Rust Syntax Reference Sheet than any kind of cheat-sheet. It basically has everything. It's useful to quickly lookup the syntax for something you never use, for example. Or to browse and discover things you didn't know were even possible.
- yakubin 5y agoA simple "+" works as well, as long as "x" is a String, and not a &str. Also, concatenating strings for any other purpose than printing them is a rarely done thing in my experience, no matter the language. So I don't see why we should optimize the ergonomics of something like that.
- the_mitsuhiko 5y agoString concatenation is an antipattern in general. Making it too convenient is not a great idea anyways.
- moldavi 5y agoWhy would concatenating strings be an antipattern?
- burntsushi 5y agoBecause it's very easy for it to become accidentally quadratic in a loop.
- talideon 5y agoThere are lots of reasons it can be. Here's an example: in many languages, concatenation requires reallocation to generate a new string object. This is especially the case where strings are immutable. That can get expensive quickly as you're producing lots of garbage for little benefit, and each concatenation requires progressively larger and larger reallocations. You end up seeing quadratic runtime costs in terms of both memory and speed. There are a few different approaches taken to this. In Python, the popular option is to append everything to a list, and use `''.join(lst)` at the end to concatenate everything. Java and C#, on the other hand, have StringBuffer/StringBuilder types to accomplish something similar. Generally, you want to avoid simple string concatenation in loops because of this, and it's not always obvious when string concatenation is being done in a loop, such as where the string concatenation is being done in a method that's being called by some other code.
- ProNeo 5y agoDoes something like this exist for Kotlin?
- da39a3ee 5y agoThis is amazing! But there seems to be something missing: error handling. After using Rust on and off for a couple of years, a thing I still don’t understand is how to write idiomatic code which deals with multiple different error types.
- mesaframe 5y agoEnums?
- papaf 5y agoI sill don’t understand is how to write idiomatic code which deals with multiple different error types. I am not sure if it is idiomatic but I have been following the guidelines int this article for general error handling: https://nick.groenen.me/posts/rust-error-handling/ https://nick.groenen.me/posts/rust-error-handling/ And this article for having context: https://kazlauskas.me/entries/errors.html https://kazlauskas.me/entries/errors.html
- da39a3ee 5y agoThank you. (I'd come across the first but not the second). It looks like there is nothing blocking me apart from the need to find a clear morning to which to devote myself to the task of doing error handling properly in my project.
- davidhyde 5y agoIf you are writing your own library then your best bet is to define your own Error enum in each module. Below each Error enum write a bunch of From trait implementations to convert errors into a custom variant in your Error enum. This allows you to use the "?" operator to "bubble" errors up through your library and build some sort of useful printable error tree. Don't used boxed errors and don't use std:io:Error for your own custom purposes. Doing things this way will mean that consumers of your library will not be bound to use some error handling library that they may not want to use. It is also very fast. If this is not a library then feel free to use any number of error handling helper libraries out there. It would be unfair to single one of them out. However, once I got the hang of writing my on custom errors I ended up using them for my binary applications too.
- truth_seeker 5y agoThanks. One of the best cheatsheets I have even seen.
- HugoDaniel 5y agoPretty cool! Is there something like this for other languages? I know that Khronos publishes this pdf for WebGL* * https://www.khronos.org/files/webgl20-reference-guide.pdf https://www.khronos.org/files/webgl20-reference-guide.pdf
- dotslashroot 5y agoyou can look at this: https://overapi.com/#more https://overapi.com/#more
- ajross 5y ago"Cheat Sheet" prints as a 64 page PDF. Yeah, that sounds about right.
- KingOfCoders 5y ago<3 Rust The only thing I find difficult in Rust is lifetimes of e.g. "String"s in data structures, e.g. in Scala I have: case class CustomerSale(name:String, item:String) and because of GC I don't have to think about life times, copying etc. - data gets removed after a request or when nothing is used any longer. In Rust it is struct CustomerSale{ name: String, item: String, } which rarely works that simple. I need to think of the life times of name and item, e.g. when I get them from a database, and I don't want to own it, or if I want to copy them or have &str etc. And in web frameworks in Rust there is no notion of request life times which would make things much easier.
- pitaj 5y agoIf you use String which is heap allocated, you don't really need to worry about lifetimes. &str on the other hand
- KingOfCoders 5y agoYes, String was an oversimplification, String means I either own or copy it, both things I usually don't want to do, so I end up - as you've said - with &str.
- phaylon 5y agoYou might want to look into using `Arc<str>`. It's an immutable string slice that can have many owners and will be dropped once the last one is done with it.
- KingOfCoders 5y agoThanks, I will try that - fearing my code is littered with Arc ;-) - but sarcasm aside, thanks.
- phaylon 5y agoI usually just have a `type Text = std::sync::Arc<str>` at the root, specifically during prototyping :)
- optimalsolver 5y agoAny recommendations for a good C++ cheat sheet?
- dotslashroot 5y agoSome people have asked the cheatsheet for other languages. you can look at this: https://overapi.com/#more https://overapi.com/#more
- deleted 5y ago[deleted]