14 ms·
Rust by Example
- Uehreka 13y agoThis looks great, I've bookmarked it for the next time I want to learn a new language. Speaking of which: I haven't been keeping up with the Rust news, is it stable yet? Or are they still introducing breaking changes?
- adamnemecek 13y agoNot quite stable (current version is 0.10) but they are trying to keep the breaking changes to a minimum.
- jfager 13y agoI wouldn't say "trying to keep breaking changes to a minimum"; breaking changes still happen a lot. More accurate would be that breaking changes are happening less frequently than they were and generally require less effort to accommodate.
- steveklabnik 13y agoLet's be a bit more precise: the _language_ has few breaking changes right now, but the _libraries_ are absolutely in a lot of flux. My "Rust for Rubyists" doesn't use a ton of library code, and here's all it took from 0.9 to 0.10: https://github.com/steveklabnik/rust_for_rubyists/commit/9e55c5702915d6f514c2ee493b174c891219a70c https://github.com/steveklabnik/rust_for_rubyists/commit/9e5...
- BrandonM 13y agoI looked through that, and it seemed that the vast majority of the changes were in prose, tool output, and println. There were some other smallish changes as well, but that doesn't seem so bad for a language that is being very clear that breaking changes are still occurring.
- steveklabnik 13y agoYup. To be clear, this is a ~50 page introduction to the language, so it's not as though I'm testing all of the features of Rust, but the language itself is not changing a whole lot at this point. And what does is often a find/replace.
- jcmoyer 13y agoBreaking changes still happen, but commits to the rust repo are tagged with [breaking-change] so you can easily do a `git log --grep breaking-change` and (hopefully) get a good idea of how to fix your code. For more gradual breaking changes, the developers have been good at adding lints to rustc so you're told how to fix your code via compile-time warnings. Vec migration and crate attribute syntax are two recent examples of this.
- steveklabnik 13y agoIt's worth mentioning that this policy is only a few weeks old, so it's more of a 'will be in the future' thing than it is 'all the changes so far' thing.
- fooyc 13y agoRust feels a lot like C++ with smart_ptr/unique_ptr by default, just with a different syntax. Bad things they imported from C++ : - Traits (a.k.a. I don't known where this method is implemented). - Over-complicated and compile-time-specialized generics (feel a lot like templates). - Copy traits feel a lot like C++ copy constructor (a.k.a. I never known what simple things like assignments or passing an object around will actually do) ( https://rustbyexample.github.io/examples/clone/README.html https://rustbyexample.github.io/examples/clone/README.html ) - Explicit allocations (stack vs heap) and ownership management. Yet they added some bug-provoking things like "The final expression in the function will be used as return value". And crazy syntax rules like "ho, only if the expression is not followed by a ;".
- adamnemecek 13y agoIf you actually believe that, you have not really looked. It adds e.g. algebraic data types, pattern matching, actor based concurrency, it's more functional, has pretty good macros, has the compiler do more heavy lifting etc. > Explicit allocations and ownership management. It has optional GC. And it's not like automatic memory management comes at no cost. The last thing makes sense if you think of a semi-colon as an operator turning an expression into a statement. And since it's typed, it won't let you do something dumb.
- fooyc 13y ago> If you actually believe that, you have not really looked. It adds e.g. algebraic data types, pattern matching, actor based concurrency, it's more functional, has pretty good macros, has the compiler do more heavy lifting etc. My point is that they have borrowed really bad features from C++. Pattern matching, Option, etc are cool; but the C++ features are ruining it, IMO.
- steveklabnik 13y agoIt sounds to me like you don't want a low-level programming language. That's totally fine! Rust isn't going to be amazing for everything.
- pooya72 13y agoThis is cool. I'm really excited about Rust, but as a programming hobbyist(noob) I haven't been able to get very far with it. The language is starting to become more googleable now, even though the name "rust" is a hinderance. That's one of the things I like about clojure. Very googleable name.
- kybernetyk 13y agoI guess/hope that "rustlang" (like with D and go before) will become the googleable term.
- encoderer 13y agoGolang was the original name of the language.
- asdfaoeu 13y agoTrying to find out what rustc's oxidise step was especially painful.
- chrismorgan 13y agoThere, you finally pushed me over the edge on it—https://github.com/mozilla/rust/issues/13781 https://github.com/mozilla/rust/issues/13781
- tomlu 13y agoAs it becomes more famous Google will learn that it's a programming language and that you're a programmer. The first search result for "View" is the Android documentation for me.
- encoderer 13y agoThis. Google has been talking about personalized search for a decade. And for years they would say that your search results were personalized but it wasn't really obvious. You had to just trust that what you're seeing is better because they say it is and that it's just for you. But sometime in the last few years this seems to have hit an inflection point. Domain and location specific results are expected now. Where a decade ago I would have to type in "Sycamore San Francisco" now I just type in "Sycamore" and I get what I want. This also has an effect on adwords campaign tuning. As people become trained to not include locale in their query, the keywords become fewer and more generic. You of course use location targeting but many people crafted thousands of location-specific keywords that get less and less traffic going forward.
- elinchrome 13y agoWell that's exciting. I had been waiting for precisely such a tutorial for rust.
- zura 13y agoNice. Similar thing for Golang would be appreciated. I mean more advanced than Go tour.
- dbaupp 13y agoThis was actually inspired by exactly that: http://gobyexample.com/ http://gobyexample.com/
- thikonom 13y agodoes anyone know the coloscheme used in the snippets ?
- caipre 13y agoIt's using highlight.js, but doesn't seem to be one of the default styles. [0] Maybe it's custom? [0]: http://highlightjs.org/static/test.html http://highlightjs.org/static/test.html
- kybernetyk 13y agoThis is really great and answers many questions I had about details of Rust but was too lazy to google. Thanks! Btw. the lifetimes example misses syntax coloring.
- JetSpiegel 13y agoRust looks like a marriage between Java and C. Very interesting. There's some error on this page. https://rustbyexample.github.io/examples/lifetime/README.html https://rustbyexample.github.io/examples/lifetime/README.htm... The syntax highlight doesn't work. EDIT: And https://rustbyexample.github.io/examples/constants/README.html https://rustbyexample.github.io/examples/constants/README.ht... too
- masklinn 13y ago> Rust looks like a marriage between Java and C. Very interesting. Where do you get Java from? It's more of a marriage between C and ML, but it has drawn inspiration from numerous other sources.
- JetSpiegel 13y agoGenerics mostly, the syntax is the same. Structs with associated methods are a rudimentary form of classes. I have to say I'm not a programmer nor I've studied Computer Science, more a Script Kiddie, and functional languages are something esoteric that I understand on a very shallow way.
- masklinn 13y ago> Generics mostly, the syntax is the same. That's the syntax for "generics in C-like languages" since C++, FWIW. So at its core, the inspiration there is probably C++ more than it is Java.
- pcwalton 13y agoWell, you're not wrong--Java's generics and Rust's generics were very influenced by ML's. So really you're seeing the ML influence :) (Though Rust's generics really borrow more from those of Haskell, since it has more fully-featured typeclasses as opposed to just eqtypes like SML.)
- JetSpiegel 13y agohttp://tvtropes.org/pmwiki/pmwiki.php/Main/OlderThanTheyThink http://tvtropes.org/pmwiki/pmwiki.php/Main/OlderThanTheyThin...
- estebanrules 13y agoI'm very interested in learning either Go or Rust. Of course I could learn both, but when I learn a new language (or anything really), I like to really immerse myself and focus. I'm leaning towards Go but Rust is very interesting.
- steveklabnik 13y agoGo is like a better Ruby or Python. Rust is like a better C++. Go is ready for production today. Rust will have 1.0 this year, and only has 3 production deployments so far. Hope that helps.
- pjmlp 13y ago> Go is like a better Ruby or Python. Not really as it lacks the expressive power of the said languages. And if one is looking for execution speed there are alternatives like Cython, PyPy, JRuby and RubyMotion. For the OP question, if he values a mordern 21st century language, Rust is the way to go, even at pre 1.0.
- steveklabnik 13y agoGo wins over Ruby and Python for many people due to it's speed, tooling, and trivial deployment. A language is more than just expressive power. I personally don't find Go to be that interesting, but the number of Ruby and Python people who are making re move and are happy with it indicates that I'm an oddball.
- pjmlp 13y agoWhat I think is that many moving to Go, do so without trying out other runtimes/compilers available for the said languages.
- mackwic 13y agoI don't even understand on which base you are comparing Go with Ruby or Python (ok, they are programming languages). Care to explain ? In my point of view, Go is system oriented, concurrency builtin, compiled, lots-of-braces, few syntax features. It's like the opposite of Python and Ruby. I would compare Go with Vala, Java and C#.
- Keats 13y agoIs it only me or the comments are completely unreadable due to their color? I need to highlight them to read properly
- sholanozie 13y agoWhat kind of application is Rust well-suited for?
- buster 13y agoPretty much everywhere where you see C/C++ i'd say. So not in your browser, but everywhere else.
- jnbiche 13y ago>So not in your browser, but everywhere else. Not in the browser now, but I think Rust will make a very nice compile-to-JS language with Emscripten. And this will likely be possible in the not-to-far future given how relative easy it will be to implement with LLVM. That said, Rust's primary uses will probably be "systems programming", game dev, embedded programming, and other real-time programming.
- eranation 13y agoThis is pretty cool. To my surprise, I found many language constructs similar to Scala. Is it just me? It's like go and Scala mashed together with the good parts from both syntaxes.
- steveklabnik 13y agoRust takes more from OCaml and Haskell than Scala, but it's very inspired by functional languages, it's not just you.
- eranation 13y agoI don't know OCaml much but the syntax for pattern matching, Tuples, Traits, the fact that map, filter etc are methods on the collection (unlike F# or Haskell) feels more like in Scala than pure functional to me.
- pcwalton 13y agoYeah, Rust tries to integrate method syntax into the language more than OCaml (whose object system is kind of separate) and Haskell (which doesn't have an object system). It's a different approach from Scala, though: we don't have inheritance or virtual methods outside of traits (which are like interfaces).
- rm445 13y agoThere's tons of Python too, wouldn't you say? In a way, it's good - people are discovering what works and moving it forwards. Language fragmentation often seems like a shame - because most of the time, programming languages don't fit into different niches, we just have a multitude of general-purpose languages with a few special features to argue over. There's some hope that, over long periods of time (longer than our typical careers) languages will combine good features, genetic-algorithm style, and in the end we'll get even better languages.
- bjz_ 13y agoScala and Rust share common roots in ML and Haskell. I would say it's more like a mash between C++, ML and Haskell.
- deleted 13y ago[deleted]
- deleted 13y ago[deleted]
- deleted 13y ago[deleted]
- deleted 13y ago[deleted]
- deleted 13y ago[deleted]
- zackmorris 13y agoA few observations knowing nothing about Rust until I read this, perhaps someone can shed some light on these: THE BAD: * keywords like "let" are extra work for the programmer because the compiler can infer them automatically. * I’m not a fan of printf()-style parameters, because they break ordering (remarkably, php does this best with its “.” and “$” notations). * Mandatory curly brackets are also more work, although they (could) prevent subtle bugs later. * I’m not a fan of the generics syntax, borrowed from templates in c++ (I prefer the something like the “auto” style more, shifting work to the compiler). * Forcing the programmer to manually place values on the heap with “~” is annoying, when other languages can automatically optimize such things. * It seems that move semantics distinguish between objects and primitives, instead of everything being an object? * Borrow is confusing to me, because I would probably make an immutable object passed to a function as mutable create a copy (but maybe it avoids subtle bugs). * Lifetimes completely confuse me, as it seems like they would be handled by garbage collection? * I don’t have a problem with the “static” keyword, except that wouldn’t global immutable variables be static anyway? * I might have gone with traits (interfaces) everywhere instead of distinguishing them from structs, because that seems to be the direction the world is going, to avoid inheritance problems. * I’m not sure I would have distinguished closures from functions with special syntax (although maybe it prevents subtle bugs later). * #[bench] seems a little weird (putting profiling calls in the code), although I do find myself wanting this constantly, so if they conditionally compile they could be handy. THE GOOD: * Real preemptive threads it seems (as opposed to Go’s goroutines which are cooperative threads/coroutines). * UTF-8, non null-terminated strings are good (I wonder how they handle character boundaries though, especially in languages besides English). * Some argument could be made that borrowing c++ concepts makes Rust more approachable.
- kam 13y ago> I’m not a fan of printf()-style parameters, because they break ordering (remarkably, php does this best with its “.” and “$” notations). Rust's println! macro also supports named format parameters, which don't need to be in the same order as the format string. println!("{foo}: {bar}", bar=5, foo="abc"); > It seems that move semantics distinguish between objects and primitives, instead of everything being an object? Structs fulfilling the Copy trait (meaning they don't have destructors and can be duplicated with a simple memcopy) have the same copy semantics as primitive number types. > Forcing the programmer to manually place values on the heap with “~” is annoying, when other languages can automatically optimize such things. > Lifetimes completely confuse me, as it seems like they would be handled by garbage collection? Rust is a systems language. The language makes memory allocation and management explicit, by design. Lifetimes allow Rust to provide memory safety at zero run-time cost. They're how the programmer proves to the compiler that pointers are valid at the time of use. There is no garbage collection by default; only when you use the Rc or Gc types from the library.
- jganetsk 13y agoI try to describe Rust to lots of people. It's astonishing to me that most people have not imagined the possibility of a language having manual memory management with the compiler checking for mistakes and guaranteeing safety. So many programmers have such a low opinion and comprehension of type systems that it's very regrettable.
- jeffdavis 13y agoI am one of those people who didn't really consider the possibility. It's not that I thought it was impossible, but I dismissed it as probably impractical to use. Rust is showing that it's actually practical, in a somewhat-normal language with somewhat-normal libraries.
- twic 13y agoThey should use that on their publicity material: "Rust: it's somewhat normal".
- oscargrouch 13y agoWe can see a live example of that in some of the comments in this thread.. Two decades of garbage collected languages, with fat runtimes that does everything under the hood + lazy programmers with Moore's law mentality Note: its ok for script languages that have exacly this purpose, to be easy and ask less from the programmer.. like Javascript or Lua.. But its a shame that languages like Java have created such a bad mentality.. For system programming its not cool to abstract the machine too much.. we've learned that lesson the hard way (see the big projects like hadoop in manage languages for an example) Those languages asks almost nothing to use them.. but you end up paying a lot more latter, because of the abstractions layers you just cant change.. You are lucky if you project starts simple and remain simple over time, but the majority of the projects are not like that.. Yes: javascript for system programming like in nodejs is a bad idea (as in: "free entrance, pay with your blood later")
- bad_user 13y agoOh master, please teach an unworthy soul the path to the true enlightenment you must be experiencing everyday while being close to the metal, because this fool was blinded by the promises of fat runtimes that can deliver business value in a shorter amount of time without having to worry about the cost of virtual table dispatches, memory leaks, buffer overflows or differences in the memory models and libc versions of the underlying operating system. > For system programming its not cool to abstract the machine too much.. we've learned that lesson the hard way (see the big projects like hadoop in manage languages for an example) Oh master, please do say what's wrong with Hadoop, one of the best and most successful pieces of open-source infrastructure. Truly you must know of a better alternative that isn't burdened with the cancer that is the JVM. Also, you must mean something different when you're saying system programming, because in my dictionary, Hadoop ain't it. Not a native english speaker here, must take some lessons apparently.
- chrisdevereux 13y agoWhat do people use to write Rust? Seems like it would really make the most of good editor/IDE support.
- steveklabnik 13y agoWe have a lot of vim and emacs people, and an IDE crew too. I personally use vim. I think there's even a Kate file in the source tree? There's been lots of effort to make sure Rust's grammar stays simple enough to have great IDE support eventually, so as the language matures, hopefully we'll see it supported in a lot of them.
- gnuvince 13y agoEmacs for me.
- dubcanada 13y agoAll we need now is a GUI library and we'll be set.
- zephyrthenoble 13y agoI am a CS student who had to use Rust for my OS class. I have had several issues using Rust so far, mostly caused by a lack of verbose documentation and examples. The basic ideas are great, but it's hard to learn about some things, such as lifetimes, because it isn't covered in extensive detail in the official documentation, and not at all elsewhere. The compiler is no help, because the errors aren't online. I won't be using Rust again until well after its 1.0 release. I don't want to struggle with a language, I want to program in it. Rust just seems like it requires a much deeper knowledge of programming theory in general for one to use it effectively, and people like me would rather just write it in Python.
- adamnemecek 13y agoThe bigger issue is that your course professor picked a language that's still pretty new and under development. To quote the Rust website "Rust is a work-in-progress and may do anything it likes up to and including eating your laundry". > Rust just seems like it requires a much deeper knowledge of programming theory in general for one to use it effectively I mean it's on the lower end of language spectrum so yes, it requires more knowledge than say Python but it's not much more than say C. > people like me would rather just write it in Python Rust is not supposed to replace Python, it's supposed to replace C/C++/Ada.
- zephyrthenoble 13y agoI think what I should have said is that, since I had deadlines for project submissions, dealing with Rust in its current state was more difficult than writing it in Python would have been. I do understand that Rust is trying to become a better alternative to current lower-level languages, but my OS class is (supposed) to be about how things like kernels and schedulers work, not about college students struggling with a new under development language. So by "rather just write it in Python" I meant "rather just write it in something I already can use efficiently." I am obviously not who Rust is aimed at.
- adamnemecek 13y ago
- mamcx 13y agoBy the way, this was done with what? It look great.
- steveklabnik 13y agohttp://www.gitbook.io/ http://www.gitbook.io/
- gmrple 13y agoI feel like they should have named the compiler 'rustic'.
- JetSpiegel 13y agoEven better, 'crust'
- zobzu 13y agoone of the point people are trying to make and apparently get misundertood: 1) i need to code something quickly, easily, safely, and i don't care for perf too much: * i use python2 2) i need low level access, i want to manage object copy or the lack thereof, i want to fine tune memory allocation: * i use C (or C++) 3) I want something like python, but faster, AOT compiles, with more control: * i use Go but its not that fast and you can't manage everything * I use C# but outside windows APIs sucks and AOT doesn't work, and unsafe mode is unsafe * [...] * I use rust but the syntax is crazy and its far harder than python or Go So.. of course, rust may not be targeted at 3) - but instead as a real C++ replacement. Turns out many still want 3 regardless - even thus rust is closer to 3 than C++.
- megaman821 13y agoIt seems number 3 is all about have solid libraries with a nice interface. If I need an API server that can handle a fair bit of traffic, having a solid, easy-to-use HTTP lib and database lib is going to go a long way. Nothing I see in Rust precludes them from delivering these things.
- zobzu 13y agogood API is indeed a necessity - and in my opinion at least 50% of the value of a language is the API - or probably more. That said, rust is still complex IMO due to both specific idioms and weird/unusual syntax.
- thinkpad20 13y agoWhat are some things that have been written in rust, besides Servo? I think one of the reasons Go has been so successful is that they started right off the bat with something people could use, and focused on creating tools and other things which encouraged people to dive in. Julia had this too with their math libraries. Things like web servers / frameworks, game engines, parser libraries, command line tools... What is written in rust that I could use to start building actual stuff?
- DonGateley 13y agoReally naive question: might it be possible to bridge Rust to some older language's high content, well developed library?
- kam 13y agoRust can use C libraries pretty easily, and there's a tool to autogenerate Rust `extern` declarations from C headers: http://static.rust-lang.org/doc/master/guide-ffi.html http://static.rust-lang.org/doc/master/guide-ffi.html https://github.com/crabtw/rust-bindgen https://github.com/crabtw/rust-bindgen
- jganetsk 13y agoOne of the fascinating ambitions of Rust is to bring linear type systems to the mainstream. The programming world is so un-acquainted with linear types, yet the possibilities are staggering. To me, functional programming languages were always based around referential transparency: the property that a name and its value are interchangeable. But Rust takes another approach: a name is an owner of value, and owners are linear resources. This is very exciting, and makes me want to learn linear logic. It also makes me wonder if there is a smaller language dying to break out of this larger one.
- pcwalton 13y ago> It also makes me wonder if there is a smaller language dying to break out of this larger one. I don't think so, at least without sacrificing one of (a) memory safety; (b) no mandatory garbage collection; (c) race-free concurrency. Virtually every feature in Rust* is necessary to achieve one of those three goals. * OK, not methods, typeclasses, or macros. But methods and interfaces/typeclasses in some form are more or less an expected part of every programming language that is to go mainstream nowadays, and macros are necessary for really important conveniences like println and deriving to work.
- aaronblohowiak 13y agoYou could if you had only linear-typed mutable and refcounted immutable. Refcounted immutable obviously could not contain pointers to linear typed mutable things. That would be much simpler, just as safe and require no GC.
- pcwalton 13y agoWell, you'd have no multiply-owned mutable state then, which would prohibit a bunch of useful things (such as implementing the DOM in a browser). In any case, you just described basically how Rust the language works: the ref-counted mutable types are all just part of the standard library, implemented with unsafe code but exposing a safe interface. The only mutability in the language itself is through linear types.
- 13y ago