3 ms·
Salespeople qualify leads by determining if you're ready to buy or not. If you're not, they stop wasting time on you. The general idea for a quick introduction
by steveklabnik 13y ago
Salespeople qualify leads by determining if you're ready to buy or not. If you're not, they stop wasting time on you. The general idea for a quick introduction is to qualify your lead. So this isn't a "introduction to Rust's syntax" it's "an introduction to why you should (or should not) care about Rust."
> since every competent C/C++ programmer knows not to do it.
Everyone knows, yet programs still segfault. The point is that the language helps you be competent. Static analysis is very useful.
> Does Rust really call its threads "green threads"? Green threads have a weird reputation.
I agree. Rust has N:M mapped threads by default, but recently added 1:1 as well.
> it's an attempt at conversational style that doesn't add much.
I happen to write like I talk, it gets good and bad reviews. A more neutral style would be more appropriate if/when this gets pulled into Rust itself, thanks, that's a great point.
> Does the whole concept of Rust concurrency and memory protection boil down to "the language provides an 'unsafe', and then people write libraries to do things with it"?
I think this is an unfair characterization, but I gave it to you, so that's a criticism of me, not you. I tried to point out that unsafe exists for exceptional cases only: it's not something that you need unless you're doing something dangerous for a specific reason. I personally have only ever written unsafe when wrapping something with FFI.
Introductions are hard because you never know how much depth to go into; maybe I should go into these bits a little more in depth.
Thanks for the great feedback. :)
- tptacek 13y agoOh, don't get me wrong, I'm sold on memory protection as a type system feature. I'm just suggesting that the example you're using might make it sound less valuable, because returning stack variable references isn't the most common kind of error made by C programmers; when you do that, more often than not your program doesn't work at all.
- steveklabnik 13y agoAbsolutely, I don't want people to think I'm attacking a straw man. Maybe a heap allocated example would be better?
- masklinn 13y agoProbably, with a slightly tricky ownership issue leading to use after free (a well known source of exploits http://cwe.mitre.org/data/definitions/416.html http://cwe.mitre.org/data/definitions/416.html) e.g. allocate to the heap, pass to a function which deallocates it (assuming that it has ownership) and use it after the call, e.g. #include <stdlib.h> #include <stdio.h> void destroyer(int* val) { printf("%d\n", *val); free(val); } int main(int argc, char** argv) { int* v = malloc(sizeof(int)); *v = 3; destroyer(v); printf("%d\n", *v); return 0 } Which compiles without warnings using Clang (unless -Weverything, and even then the warnings are not related to use-after-free), works "correctly" in O0 and O1 (prints "3" twice) then breaks starting at O2 (prints "3" then "0"). (note: it always prints "3" twice with GCC 4.8, showing how fun these things are) meanwhile the equivalent fn main() { let v = ~3; destroyer(v); println!("{}", *v) } fn destroyer(val: ~int) { println!("{}", *val) } refuses to compile and explains why: test.rs:4:20: 4:21 error: use of moved value: `v` test.rs:4 println!("{}", *v) ^ note: in expansion of format_args! <std-macros>:224:8: 224:50 note: expansion site <std-macros>:223:4: 225:6 note: in expansion of format! <std-macros>:241:45: 241:63 note: expansion site <std-macros>:240:4: 242:5 note: in expansion of println! test.rs:4:4: 5:1 note: expansion site test.rs:3:14: 3:15 note: `v` moved here because it has type `~int`, which is non-copyable (perhaps you meant to use clone()?) test.rs:3 destroyer(v); ^ error: aborting due to previous error which can be fixed either by explicitly cloning the value, or by altering the sub-function to not consider it owns the pointer (or by removing the `println!` call in `main()`, thus transferring the ownership of the pointer to the sub-function safely, of course)
- steveklabnik 13y agoThanks so much. I'll certainly have to use this if this makes it to Rust proper.
- dherman 13y agoThe downside of picking subtler, more intricate examples is that you waste your reader's time trying to understand the subtleties of the example, which isn't teaching them anything about Rust. Another option is to say something like, "For the sake of brevity, this is a very simple and arguably obvious violation of safety. In practice, there are many subtle and hard-to-diagnose sources of unsafety in C++, even when you use safer abstractions like shared_ptr." This allows you to avoid getting sidetracked and losing your reader, while heading off skepticism of readers with more knowledge about C++.
- crusso 13y ago> I happen to write like I talk, it gets good and bad reviews. There's nothing wrong with writing casually for a web page that is meant to be inviting to newcomers. I've ignored Rust links on HN until the subject of your link got me to take a look. The style was fine and you did a good job in at least showing a few snippets of code while advocating the benefits of the language. Contrary to tptacek's criticisms, I wasn't looking for a dry or comprehensive manual - just something to read in a couple of minutes that would give me an indicator of whether or not I should look further into the language. Thanks for the article.
- scott_s 13y agoA conversational style is fine, but those particular sentences actually get in the way. The style comes from the overall tone of the writing, not just sentences which talk to the reader directly.
- politician 13y ago> I personally have only ever written unsafe when wrapping something with FFI. Is discussion of unsafe in an introduction article esoterica? By the way, loved the proposal video [1] you did on docs for rust: it's a pretty solid proposal for any language. [1] https://air.mozilla.org/rust-meetup-december-2013/ https://air.mozilla.org/rust-meetup-december-2013/
- steveklabnik 13y agoUnsure. I think it's really important that people know the rules _can_ be broken if you think you know better than the compiler. Thanks. :)
- kybernetikos 13y ago> I happen to write like I talk, it gets good and bad reviews. I found it unusually clear, which I liked a lot. There was a bit at the end to do with unsafe that was a bit difficult to follow, but a sentence like 'but wait, RWArc is just a library, how come it was able to do the thing that I said the compiler wouldn't let you do?' would have cleared it up just fine.
- steveklabnik 13y ago> but a sentence like 'but wait, RWArc is just a library, how come it was able to do the thing that I said the compiler wouldn't let you do?' would have cleared it up just fine. Hmmm. Isn't that what this says? > So, the Rust language does not allow for shared mutable state, yet I just showed you some code that has it. How’s this possible? The answer: unsafe.
- kybernetikos 13y agoWell for me at least, you're introducing the language so it seems like RWArc is part of that. I have no idea as to whether or not there are any little cheats put into things like RWArc that are not available to other code, so emphasizing that RWArc is the kind of thing I could have written myself would have helped me understand. It's that jump from being introduced to RWArc to considering how it might have been implemented that wasn't obvious to me.
- steveklabnik 13y agoGotcha. It seems a few other people had this issue too, I'll take care of it in the next iteration. Thank you!
- kybernetikos 13y agoThanks for doing this, I've got much more appetite for reading about rust than can easily be sated, so it's very gratifying to have someone explain the key concepts so clearly and well.
- coldtea 13y ago>I happen to write like I talk, it gets good and bad reviews. A more neutral style would be more appropriate if/when this gets pulled into Rust itself, thanks, that's a great point I think the style's alright. I also like the tone of the tutorial on the Rust website, don't know if you were involved with that too. >Thanks for the great feedback. :) FWIW, I found the feedback to be a little too condescending for what is a volunter effort to help people understand a new language.