9 ms·
Learning Rust
- faitswulff 9y agoThis is very readable. Thanks!
- vvanders 9y agoLooks really nicely written and laid out. One note on borrowing/ownership that really made it click for me was that to move from &mut T to &T you have to transition through owning the value type, so: &mut T -> T <- &T Whichever scope holds the actual value-type is the only scope that can make that transition(absent cases like RefMut)
- red75prime 9y agoDowngrading &mut T to &T is perfectly valid in every context[0]. Upgrading from &T to &mut T is impossible in safe Rust and can easily cause undefined behavior when it's done in unsafe block. If you have T, you can create one &mut T reference or many &T references. [0] Except in a case when there's &mut U reference, which points at some part of the object referenced by &mut T.
- danieldk 9y agoCould you clarify you mean here? Something like this is perfectly valid: fn no_mut(a: &mut usize) { *a = 10; let b: &usize = a; println!("a: {}, b: {}", a, b); } fn main() { let mut foo = 42usize; no_mut(&mut foo); } If such pointer weakening was not possible, how would you be able to call a method that takes &self on binding of type &mut self? Of course, you can't use a mutably while b is in scope. So, something like the following results in a compiler error: fn no_mut(a: &mut usize) { let b: &usize = a; *a = 10; println!("a: {}, b: {}", a, b); }
- vvanders 9y agoActually you're right, I didn't realize that the compiler treated a single &mut references like that. I wonder if what I'm thinking of involved &mut self captures that were happening as part of an impl on a struct. There's definitely some cases there where I was getting tripped up on it.
- vvanders 9y agoAh, now I remember why I tend to use this model: use std::thread; fn no_mut(a: &mut usize) { *a = 10; { let b: &usize = a; println!("a: {}, b: {}", a, b); } let b: &usize = a; thread::spawn(|| { println!("b: {}", b); }); } fn main() { let mut foo = 42usize; no_mut(&mut foo); } Similar code but the move throws an error(as it should). If you don't have the value type then you're constrained by the parent mut lifetime here so it's not just as simple as a conversion that you can then pass around. Otherwise you need to consider all the lifetime scopes which can get a bit unwieldy.
- deleted 9y ago[deleted]
- danieldk 9y agoIf you don't have the value type then you're constrained by the parent mut lifetime here so it's not just as simple as a conversion that you can then pass around. You cannot move any closure to spawn that captures a non-static reference, since there is no guarantee that the value will live during the lifetime of the thread. (The closure is required to be Send + 'static.) E.g., you can't do (moving to a non-Copy type :)): let foo = String::new(); let t = thread::spawn(|| { println!("foo: {}", &foo); }); You could move the captures, including foo, but then foo cannot be used in its original scope anymore: let foo = String::new(); let t = thread::spawn(move || { println!("foo: {}", &foo); }); // This will give an error, because foo is moved. println!("{}", foo); Note that only moving values works, this does not compile (for the same reasons as the first example): let foo = String::new(); let bar = &foo; let t = thread::spawn(move || { println!("bar: {}", &bar); });
- deleted 9y ago[deleted]
- mswift42 9y agoExercises, I want Exercises, no I need Exercises. In order for me to learn anything, I need some form of "homework". If you want to teach me a new language, a new framework, I need to "get my hands dirty". Working through exercises makes sure I actually understand what I am reading, and sometimes it even shows me I don't understand what I thought I did.
- mattherman 9y agoYou could take a look at exercism.io. I just started the exercises on there yesterday so I don't have an informed opinion on whether they are good or not overall, but it might be what you're looking for. http://exercism.io/languages/rust/about http://exercism.io/languages/rust/about
- mswift42 9y agoNo, I mean that every chapter ends with exercises testing your knowledge about the chapter's contents. This is not specific to this github site. I've recently read the O'reilly Programming Rust Book, not a single exercise in it, and the official The Rust Programming Language Book also doesn't have any.
- shepmaster 9y ago> and the official The Rust Programming Language Book also doesn't have any Could you expand on bit on what you see as the difference between an "exercise" and the "projects" that the book includes? - Guessing game: https://doc.rust-lang.org/stable/book/second-edition/ch02-00-guessing-game-tutorial.html https://doc.rust-lang.org/stable/book/second-edition/ch02-00... - I/O: https://doc.rust-lang.org/stable/book/second-edition/ch12-00-an-io-project.html https://doc.rust-lang.org/stable/book/second-edition/ch12-00... - Web server: https://doc.rust-lang.org/stable/book/second-edition/ch20-00-final-project-a-web-server.html https://doc.rust-lang.org/stable/book/second-edition/ch20-00... There's also some exercises scattered at the end of various chapters, but not with the header "exercise" - Control flow: https://doc.rust-lang.org/stable/book/second-edition/ch03-05-control-flow.html#summary https://doc.rust-lang.org/stable/book/second-edition/ch03-05... - Collections: https://doc.rust-lang.org/stable/book/second-edition/ch08-03-hash-maps.html#summary https://doc.rust-lang.org/stable/book/second-edition/ch08-03... Sometimes they are even inlined with the text: > Try modifying Cacher to hold a hash map rather than a single value. > try introducing more generic parameters to increase the flexibility of the Cacher functionality. - https://doc.rust-lang.org/stable/book/second-edition/ch13-01-closures.html#limitations-of-the-cacher-implementation https://doc.rust-lang.org/stable/book/second-edition/ch13-01...
- talloaktrees 9y agoHow does this compare to the Rust Programming book?
- rayascott 9y agoProgramming Rust has 21 chapters and is 583 pages long. I’ve read it, and I’ve also read the online Rust Book (both several times). Most chapters are in the order of 20 to 30 pages long. Programming Rust is one of the best programming books I’ve ever read, and I’ve been coding professionally since 1997. If you want to get an A+ in Rust read Programming Rust a few times. You can see the entire Contents section of the book on Amazon. Below is a list of chapter titles from Programming Rust that get little to zero coverage in this Learning Rust website. Ownership. References. Expressions. Error Handling. Enums & Patterns. Operator Overloading. Closures. Iterators. Collections. Strings & Text. Input & Output. Concurrency. Macros. Unsafe Code/FFI - the coolest part, they show you how to create a safe wrapper around libgit2.
- AlexeyBrin 9y agoThere are two books with similar titles and I'm under the impression that you don't talk about the one the OP had in mind: https://www.amazon.com/Rust-Programming-Language-Steve-Klabnik/dp/1593278284/ https://www.amazon.com/Rust-Programming-Language-Steve-Klabn... https://www.amazon.com/Programming-Rust-Fast-Systems-Development/dp/1491927283/ https://www.amazon.com/Programming-Rust-Fast-Systems-Develop... I think both are good books, I already have the second one and preordered the first.
- deleted 9y ago[deleted]
- rayascott 9y agoI assumed he’d misnamed the book, switched the words around, as your first link points out (“The Rust Programming Language”), there isn’t actually a book called “Rust Programming”, and “Programming Rust” seemed like the closest approximation.
- 9y ago
- hokkos 9y agoI love those succinct introductions to languages, I find it easier to understand when you don't have to read lots of text and languages possibilities are written as a series of quick examples. What I miss from Rust learning material is a series of exercises that focus on reference, lifetime, ownership where you have to fix some code to make it compile.
- shepmaster 9y agoYou may be interested in Rustlings: > Small exercises to get you used to reading and writing Rust code. Includes practice reading and responding to compiler messages! https://github.com/carols10cents/rustlings https://github.com/carols10cents/rustlings
- Scarbutt 9y agoFor devs, equally familiar in rust and python and ignoring libraries, would you say development speed in both languages is almost the same?
- siscia 9y agoI really don't think that I could code any faster of what I code in rust. Especially if you consider the overall quality of the work. In my humble opinion python give the impression to move fast, but when you need to maintain the code base the rust compiler is simply too wonderful.
- chc 9y agoI suspect people probably can do it a bit faster in Python, but like you say, you wouldn't necessarily end up with the same quality of work. The Rust code is likely to be quite fast and very solid, whereas the Python code is likely to be "eh, fast enough" and "stable as long as nobody says the word 'thread' within a 10-block radius."
- mlindner 9y agoFor me writing Python is fast, until you need to rewrite it when you inevitably refactor. For Python that's an agony, for me.
- mquander 9y agoI'm more familiar with Python than Rust right now, but I feel like after adjusting for amount-I-have-to-look-up-standard-library-things my development speed is similar. The biggest advantage Python gets is IPython.
- staticassertion 9y agoI write python for most of the day, professionally. I write Rust at home. I'd say I spend similar time on the same problem with both, maybe a bit more upfront on Rust and a bit more on the tail with Python. This is, of course, disregarding libraries.
- always_good 9y ago
- piracykills 9y agoI really want to learn and use Rust, but just lack the motivation, especially when APIs turn out to be incomplete - I think the last one I saw was multicast not allowing me to set the send interface. I guess the main draw for me to any given language is great libraries with great APIs. Python has those, Javascript has those, C++ has those, Rust... not so much yet.
- childintime 9y ago> I guess the main draw for me to any given language is great libraries with great APIs. Python has those, Javascript has those, C++ has those, Rust... not so much yet. Well... what makes (especially) Rust libraries great is the relative absence of bugs. I can use a Rust library and have great confidence some ugly bug won't bite me later on. The developers of the libraries you (don't) mention continuously struggle to achieve the same property. So in a sense those libs are not at all great in the same sense that a Rust library is!
- earenndil 9y agoThis looks like a fairly nice website. For someone coming from a background of C and D, but familiar with quite a few others, I have to ask: what makes rust different and better? Aside from memory safety, what reason do I have to choose rust over another language?
- Paul-ish 9y agoDid you have another language in mind? Eg Rusts safety means basically nothing if you are comparing it to Python or C#.
- earenndil 9y agoNot really, I more meant "in what situations should I choose rust and what makes rust the best choice in that situation instead of _any_ other language."
- kod 9y agoPython stops you from accidentally sharing mutable state across threads?
- earenndil 9y agoYes. Look up the GIL (Global Interpreter Lock).
- kod 9y agoWhich is why Python doesn't have thread locking... Except, it does. Perhaps that's because GIL isn't really helpful for preventing logical races? Yet it encourages a false sense of security?
- dragonwriter 9y agoThe GIL doesn't stop you from sharing mutable state across threads, nor does it make it generally safe to do so.
- 9y ago
- Animats 9y agoI used to know Rust, but I got tired of the churn and went back to Go.
- steveklabnik 9y agoWhat churn, specifically? Anything you learned since May 2015 should still be very relevant. That's almost three years ago.
- deleted 9y ago[deleted]
- Dowwie 9y agoExamples or it didn't happen
- yusrilhs 9y agoI trying use rust, but I don't know why sometimes cargo is like hanging to retrieve from github.
- Wildgoose 9y agoIf you are going through a proxy server (e.g. at work) then you may find that the SSL interception proxy has not been correctly configured to provide revocation information.
- steveklabnik 9y agoPlease file bugs! We'd like to track it down.
- ishanjain28 9y agoIt's probably a git/libgit issue. I reported a similar issue(Cloning/Pulling commits from github took forever, Transfer speeds going as low as 1 kbps when my connection was 50mbps). They asked me to run some commands and log output to measure packet loss/speed etc. and then told me that my connection had 2-5% packet loss which was causing all the problem and I need to talk to my ISP or get a better connection. I switched my ISP after that and its not a problem anymore.
- _Chief 9y agoFrom the github readme > I am a Sri Lankan Web Developer who lives in Vietnam. So I am not a native English speaker and just learning Rust This guide is so well done, props to you. I haven't worked with Rust yet but your docs make it seem so easy to work with.
- dumindunuwan 9y agoThanks, glad it helped :)
- anon335dtzbvc 9y agoDuplicate? https://news.ycombinator.com/item?id=16048065 https://news.ycombinator.com/item?id=16048065
- dumindunuwan 9y agoAt that time search was not working, had bit ugly ui, no indicator for current page and etc. Even it's duplicate, this time people actually saw it correctly. So sometimes trying things again is a good thing :) Have a nice weekend!
- always_good 9y agoThat was three months ago... Do you expect people to start participating in that submission with zero comments now that it's three months from the top of the stack?