7 ms·
Rust by Example
- ne9xt 10y agoas a noob interested in rust, what is its primary application? why do people use rust vs another language?
- mmiliauskas 10y agoits primary application is to bring more fun into life.
- stevedonovan 10y agoYes - and frustration. Breaking cycles with weak references and so forth. But on the plus side, once you know this stuff well modern C++ makes so much more sense.
- bogomipz 10y ago>"But on the plus side, once you know this stuff" Can you give some examples of "this stuff"? As a recent entrant to the world of C++ I would be curious to hear those areas I should investigate to get to that point. You mentioned breaking cycles with weak references so it is it mostly understanding how C object model is implemented and/or anything else? Any resources or book you found helpful to find that enlightenment? Cheers.
- baq 10y agomut & being kinda like unique_ptr<> is one example.
- xorxornop 10y agoWhen you want to go as fast as C or C++, but you don't want memory or data race errors, and you want a package manager and integrated, very easy to use build tool set.
- idobai 10y agoIt's a system's programming language with safe memory and thread management as it enforces safety at compile time. Rust concentrates on zero-cost abstractions too.
- nercury 10y agoI use Rust primarily because it has very precise mutability semantics: things are either immutable or uniquely mutable. It is much easier to understand what can or can not happen to structures when they are passed between functions, as opposed to, say, Java, where object's lifetime is undefined. Destruction after last use (RAII) ensures that resources such as files, sockets, db connections are closed. Also unavailable in majority of languages. Also the language is expressive (type inference, generics, pattern matching, traits). These three things are the main reasons, although I wish other languages (old or new) would pick up the first idea too.
- viraptor 10y agoFor me it's basically the language you choose when you want to write new project, but don't want it to explode with type error / null deref at runtime and on the other hand you don't want to deal with C segfaulting and writing all the manual memory management yourself. Also potentially if you want speed closer to C than interpreted script, and don't mind spending a bit more time up front. (offset by the time you don't spend debugging an inexplicable crash later on)
- IshKebab 10y agoI think the best way to put it is that Rust has no overhead. You'll never be in a situations with Rust where you say "Oh damn I wish I had written this in X because Rust has this annoying limitation". Some examples of limitations in other languages: * Python has a GIL (Global Interpreter Lock) so writing multi-threaded Python code is more or less impossible (there are some workarounds but it still sucks). * Most garbage collected languages (e.g. Java, C#) suffer from 'stop the world' pauses which make doing low-latency realtime stuff a pain in the arse. For example on Android they recommend you don't create any objects in your draw functions to avoid invoking the GC. It's kind of like "Java is great, no need to worry about memory, but oh... you really have to be careful about memory here." Not all GC languages are like this, e.g. Go now has sub-millisecond pauses. And aside from that it is safer than C/C++. So basically it will be used wherever C++ was before. The big downside is that it is definitely one of the hardest languages to understand and write. That can make you less productive. If I were on a varied team that included less skilled people I would definitely choose Go over Rust if it were suitable - it is a lot simpler.
- nine_k 10y agoIf one just wants simplicity, why not pick C, or even Basic-80? They are simpler than Go. For the increase in complexity, Go gives you concurrency, and Rust, safety, on top of other things.
- ekidd 10y ago> If one just wants simplicity, why not pick C, or even Basic-80? They are simpler than Go. It's very simple to write bad C. Writing safe, correct C with no security vulnerabilities is surprisingly hard, which is a major reason why most OSes need to apply regular security patches. Rust gives you speed, concurrency and memory safety at the same time. This is not free: You need to know about values, references, stacks and heaps. And you need to write your code so that objects have a single, clear owner. It also helps to be somewhat familiar with generic types and functions like `map`—some old-school C programmers might have issues with parts of Rust, but C++ programmers (or C programmers who know JavaScript) should be fine. If you're willing to pay that price, and spend a week or two making friends with the borrow checker, Rust is a fantastic and versatile tool. But for many programmers and applications, that price may be too high. Rust will hopefully remain simpler than C++. But it's always going to be more complex than Go or Ruby. I have no idea whether Rust will wind up simpler or more complex than modern JavaScript, though. :-)
- ekidd 10y agoI work at a startup with large amounts of JavaScript and Ruby, but no existing investment in a compiled language prior to Rust. Adding C++ to our toolchain would result in a nasty culture shock. Ruby and JavaScript have modern dependency managers (bundler and npm/yarn). They're easy to get working cross-platform. They almost never segfault. C++ would bring a whole host of issues (build systems, dependency management, pointers, segfaults, etc.). I didn't want to go there, and neither did my management. But Rust feels more like a modern scripting language (except with a steeper learning curve): It has cargo to handle dependencies and builds, it works cross-platform with minimal fuss, and it provides excellent protection against shooting yourself in the foot. It turns out that I can use Rust for CLI tools, server applications and Node.js add-ons, and it's great at all of these things. Plus, every time I write something in Rust, my coworkers are happy, because it's fast, it's easy to install, and it's "solid." It does take a little while to bring people up to speed on Rust. It really helps to pair program and explain what's going on when they hit a speed bump.
- copx 10y ago>Ruby and JavaScript have modern dependency managers (bundler and npm/yarn). They're easy to get working cross-platform. Ruby is easy to get working cross-platform? The only fully supported platforms are Linux and OS X (and really only the Linux-like parts of OS X). Ruby on Windows is a trainwreck, and other platforms are not supported at all. Meanwhile C++ runs on everything and the toaster. Ruby might have many strengths but cross-platform support is not among them.
- steveklabnik 10y agohttps://bugs.ruby-lang.org/projects/ruby-trunk/wiki/SupportedPlatforms https://bugs.ruby-lang.org/projects/ruby-trunk/wiki/Supporte... has many more than just Linux and OS X. (I still agree with you that it's not a super strong point.)
- ekidd 10y ago> Ruby is easy to get working cross-platform? Compared to a C++ project which relies on a half-dozen external libraries each with their own build system? Yes, in my experience. I've spent too much of my life converting autoconf scripts to Visual C++ project files. But Rust is definitely quite reasonable to get working on Windows, especially pure Rust, but small C extensions are OK as long as OpenSSL isn't involved. Usually I can just cross-compile or build using Travis CI.
- deleted 10y ago[deleted]
- Wonnk13 10y agogreat website. I think i'm going to start with C++ just so i get a foundation of what a systems language is and appreciate the innovation that Rust brings to the table. I'd be really curious to know if website format exists for other languages.
- anuragsoni 10y agoThere is a website with a similar format for the Go language: https://gobyexample.com https://gobyexample.com
- MrBuddyCasino 10y agoI'm doing the same, but with microcontrollers and C instead. To me C++ just seemed to big of a mountain to climb when its just an intermediary step. All the best! There is so much to learn.
- epynonymous 10y agowhen i taught myself golang, gobyexample.com was the best resource for learning golang! so i'm hoping this is on par with that. what gobyexample lacks is more complicated examples, but it gets you started with just enough. rust seems like a great language, but i'd like to hear about some advantages versus golang.
- giis 10y agoIf I'm not wrong, I think golang and rust target different group of developers. Rust portrayed as low-level system level lang. potential replacement for c/c++. Where golang is touted as potential replacement for high-level application which are currently being written in Python/Ruby/PHP
- Manishearth 10y agoGo is still intended to replace C/C++ in many domains, e.g. networky things. It's original goal was basically a better C.
- nicoburns 10y agoThis is partly true, but there's a lot of crossover. I've been happily using Rust as a node.js replacement for the last few weeks.
- throwaway7645 10y agoGo has a garbage collector and therefore pauses which may or may not be acceptable to you. Rust does not (maybe you can enable GC though idk). Rust also encourages immutability. I'm gonna guess Go is more traditional there. Rust uses the LLVM compiler...not sure about Go. Rust is meant to be used where speed is absolutely critical.
- steveklabnik 10y agoThere's no tracing GC in Rust, not even enable-able. The reference Go compiler doesn't use LLVM, there is https://llvm.org/svn/llvm-project/llgo/trunk/README.TXT https://llvm.org/svn/llvm-project/llgo/trunk/README.TXT though.
- giis 10y agoCan anyone share some insights on how time much did it took to get comfortable with Rust? I'm getting started and crawling with concepts like ownership, mutability
- jackyinger 10y agoIf you want to do the basic things that rust wants you to do it doesn't take that long. However if you want to create any graph like data structures you need to go into unsafe rust. For example, the std doubly linked list uses unsafe with error handling to do inserts because the compiler cannot be sure that an insert is safe. In my opinion Rust's safety is overhyped and entirely dependent on good implementation of unsafe code. And judging from the liked list example there is no way for all rust code to be intrinsically safe.
- coldtea 10y ago>In my opinion Rust's safety is overhyped and entirely dependent on good implementation of unsafe code. That's short-sighted. Of course it's "entirely dependent on good implementation of unsafe code". But that's inconsequential, as unsafe code in a library, especially one used by everybody, is much easier to make safe than unsafe code all around. And after you trust an unsafe lib you use to be sound, your own safe-only code can never breach that safety. You'll be guaranteed that any issue will be on the unsafe part. It's all about compartmentalization.
- Rusky 10y ago> In my opinion Rust's safety is overhyped and entirely dependent on good implementation of unsafe code. ...as is true of any safe language, which is dependent on a good implementation of its compiler or interpreter. Rust just happens to move some of that into the language where it can be extended.
- caconym_ 10y agoShow me a programming language that lets you manipulate raw pointers while providing the same safety guarantees as safe Rust code (i.e. have your cake and eat it too) and I'll accept this as a valid criticism of Rust. Until then, I"m going to keep thinking that Rust provides a reasonable, practical compromise. For about a year, I've been regularly using Rust to write real software that I actually use, and I've never had to write unsafe code. I'm trusting other people to do it, which is never not the case, no matter what programming language I'm using.
- kkirsche 10y agoAs a heavy ruby, go and Python user what kind of scripts or programs should I consider migrating to rust to help with learning it? Or is it really just low level stuff that I can't justify the additional complexity
- moron4hire 10y agoWhen you say "low level stuff", what do you picture?
- kkirsche 10y agoI picture things like drivers, kernel extensions, etc. I do a lot of network programming with raw sockets and custom packets, parsing Netflow / IPFIX, etc.
- Manishearth 10y agoNah, it doesn't have to be that low level. Servo, for example, doesn't deal with this; it's a web browser. Think of the use cases where you'd use C or C++, and Rust should work there. You probably can rewrite Ruby scripts in Rust if you want, but the productivity benefit depends on the kind of script. But for the purposes of learning the language it doesn't really matter.
- fjrieiekd 10y agoAnyone here using Rust on mobile? That's the potential usecase that interests me the most, but given that the tooling still has a lot to be desired, and a lot of mobile code is vendor-specific anyways, I'm not sure it makes that much more sense than just duplicating those shared parts between Swift and Java. I could see it for small modular parts that are better suited for native code and when one doesn't want to use C, perhaps, or, of course, for infrastructure-level projects like Servo. I just wonder if I'm barking up the wrong tree because much of the Rust interest I see seems more heavily weighted in infrastructure and backend, not in user-facing code.