r/rust • u/steveklabnik1 rust • May 26 '16
Announcing Rust 1.9
http://blog.rust-lang.org/2016/05/26/Rust-1.9.html10
u/so_you_like_donuts May 26 '16
Typo: volitile => volatile
5
u/steveklabnik1 rust May 26 '16
Gah, I always do this. Will fix, thank you.
5
u/RustyProgrammer May 26 '16
Typo in the docs on the page UdpSocket. TCP should be changed to UDP.
3
u/steveklabnik1 rust May 26 '16 edited May 26 '16
Thanks, on it! EDIT: Actually, this has already been fixed in nightly.
4
u/RustMeUp May 26 '16
Can someone clarify what the purpose of the default syntax is in the context of specialization? It looks really out of place, why is it required in the first place?
8
u/steveklabnik1 rust May 26 '16
defaultallows a method to be specialized. https://github.com/rust-lang/rfcs/blob/master/text/1210-impl-specialization.md#the-default-keyword has the details.1
u/RustMeUp May 27 '16
Ah so it's only used as a signal to the developer reading the source code, not a requirement for technical reasons.
On that note, what is your opinion on
implementing traits in a private module that doesn't contain either the thing ortraitbeing implemented?Eg I like to put
fmt::Debug(if not derived) andfmt::Displayin its own privatestrings.rsfile. Documentation will show them just fine, everything works, but the implementations are 'hidden' away to reduce clutter.How is this different than the
defaulttag? I assume that all specializations will show up in the docs regardless.Ah well, all this sounds philosophical and sometimes I find myself on the other side.
7
u/Manishearth servo · rust · clippy May 27 '16
It is a technical requirement. By default, you can't have overlapping trait impls, since trait resolution breaks. With the
defaultkeyword, you opt in to this behavior, and accept that your own implementation may be overridden. That's not something you want to happen by default.Eg I like to put fmt::Debug (if not derived) and fmt::Display in its own private strings.rs file. Documentation will show them just fine, everything works, but the implementations are 'hidden' away to reduce clutter.
How is this different than the default tag? I assume that all specializations will show up in the docs regardless.
This has nothing to do with the default tag. Your situation is just a matter of code organization within a single crate, whereas specialization has cross-crate implications.
→ More replies (2)
10
u/AnachronGuy May 26 '16
Holy cow! We waited eternity for 1.0 and now they keep coming faster than I can understand what changed. Wohoo!
16
u/steveklabnik1 rust May 26 '16
Yup! Every six weeks, like clockwork.
11
u/AnachronGuy May 26 '16
That's impressive! Since the quality did not seem to downgrade even though the release cycle is so tight I am even more impressed by the Rust team. Hats up for you!
15
u/minno May 26 '16
It helps that they don't have any obligations as to what goes in each cycle, so there's no crunch.
30
u/palad1 May 26 '16
Raw pointers gained as_ref() and as_mut(), which returns an Option<&T>, translating null pointers into None.
Lifesaver, working with pointers and I am making tons of mistakes like:
if ret.is_null(){
let error = unsafe{CStr::from_ptr(ret)}
.to_string_lossy()
.into_owned();
println!("error:{}",error);
}and getting segfaults around my FFI code... Now I can write the more legible:
match ret.as_ref(){
Some(ref c_err)=> println!(...),
_=>()
}15
u/beefsack May 26 '16
I find destructuring for a single branch is a bit cleaner with
if let:if let Some(ref c_err) = ret.as_ref() { println!(...) }3
12
u/thirtythreeforty May 26 '16
This is effectively free at runtime, correct? Since reference types are
NonZero?17
1
6
u/DebuggingPanda [LukasKalbertodt] bunt · litrs · libtest-mimic · penguin May 27 '16
This feature also led to a record time in out-of-date-information on StackOverflow: click (read the comments on the answer)
2
5
u/masklinn May 26 '16
Don't these do completely different things? Seems to me the first one creates a String, the second one an &c_char
3
u/meh_or_maybe_not May 27 '16
That's the best part of this release for people who wrap C libraries in a safe Rusty API.
15
10
3
u/LordJZ May 26 '16
Is the panic::catch_unwind API somewhat similar to try-catch and exceptions?
I've been waiting on exception-like error handling to start some heavy Rust development, so that might be very good news for me.
22
u/steveklabnik1 rust May 26 '16
These are very emphatically not exceptions, though they are implemented in a similar way. Rust will pretty much never get real exceptions.
5
u/vrj May 27 '16
From someone that absolutely loves using Result for error handling, I'm a bit worried that people are going to mistakenly use panics like exceptions. I can't decide if it would be better to document the crap out of the use case for catching panics or to just bury it so people won't come across it unless they're doing FFI work.
1
u/steveklabnik1 rust May 27 '16
Many of us shared the same concern generally, but if you look at this thread, it seems most people are very much on board.
4
u/burntsushi May 27 '16
I personally am somewhat optimistic on this point. Go has a similarish split between idiomatic error handling (using return values) and a panic/recover mechanism. The details are of course very different than Rust, but the split exists in both languages. Arguably, using panic/recover in Go is more convenient than doing so in Rust and error handling in Go is probably less convenient than Rust. Nevertheless, folks seem to have stuck with using return values for error handling, so it gives me hope! (It is, however, true that some libraries use panic/recover as an error handling mechanism internally, but it's reasonably rare and typically because writing
if err != nil { ... }out can get a bit onerous in some circumstances.)1
u/LordJZ May 26 '16
As far as I understand from the Rust 1.9 docs the only difference between panics and exceptions is that panics do not contain stack trace information? Is this correct? (The docs even mention that this can be used as "a general try/catch mechanism")
10
u/steveklabnik1 rust May 26 '16
It's not just about the implementation, it's about what they should be used for, and how it fits into the language. You could use these to sorta-kinda emulate exceptions, but you shouldn't. This isn't a general error-handling mechanism.
3
u/LordJZ May 26 '16
That doesn't answer the question though. Also, what are the practical reasons why I shouldn't use this like exceptions, and what is a general error-handling mechanism in your mind? I am assuming you don't consider
Resulttype to be error-handling mechanism?19
u/steveklabnik1 rust May 26 '16
I am assuming you don't consider Result type to be error-handling mechanism?
The opposite;
Resultis absolutely the general error-handling mechanism for recoverable errors.panic!is the general error-handling mechanism for unrecoverable errors.what are the practical reasons why I shouldn't use this like exceptions,
Exceptions are usually a recoverable kind of error. It's exactly why you wanted this function: you expect to be able to catch the error. But panics are not generally recoverable, and even with this, panics can also abort, which will not unwind, and cannot be caught. If your crate relies on catching panics to work properly, you'll unnecessarily be cut out of part of the ecosystem.
3
u/LordJZ May 26 '16
panic!is the general error-handling mechanism for unrecoverable errors.But Rust 1.9 makes those errors recoverable? How is it different from
Resultat all then?Answering /u/staticassert:
Results are reasonable - you know when you may encounter one, they're expected errors like a webpage being down.
What if I don't want to consider webpage being down a reasonable error? Because if I do, I'd need to write code to handle that .01% case in the same code that does business logic, which is so bad for code quality.
Let's take a real-world example: I talk to a remote server which when queried for objects of type A, returns objects of type A. I certainly do not expect it to return objects of type B, and I absolutely certainly not willing to write code to handle that case. However, if that ever happens, I want my error-handling code to log the failed communication session, show my user an error message, and move on. I also do not want to employ any error handling means that involve multiple threads or processes etc. So what is the idiomatic way of solving this in Rust?
Using
Resulttype in this scenario would mean that I'd need to check for absolutely everything that may go wrong, and this amount of checks would turn my code into a complete mess that resembles Go or some unit test code.→ More replies (12)14
u/steveklabnik1 rust May 26 '16
But Rust 1.9 makes those errors recoverable? How is it different from Result at all then?
It makes them recoverable only because there are very specific situations in which they should be recovered, like what's covered in the post.
Using Result type in this scenario would mean that I'd need to check for absolutely everything that may go wrong, and this amount of checks would turn my code into a complete mess that resembles Go or some unit test code.
Well, with
try!, (and the upcoming?), I guess I just disagree that this is particularly onerous. You propogateResults up to the level that you want to handle the error, and then handle it.→ More replies (5)1
u/LordJZ May 26 '16 edited May 26 '16
As far as I remember,
try!panics when the argument is an error. So it won't help the scenario at all. I am not aware of the "upcoming?", would be nice of you to provide a link.Edit: I was wrong.
→ More replies (1)11
u/steveklabnik1 rust May 26 '16
Try does not panic, it returns a Result, specifically, the Err case.
https://github.com/rust-lang/rfcs/blob/4b4fd5146c04c9c284094aad8f54ca5c2093c7f2/text/0243-trait-based-exception-handling.md is the question mark, basically,
try!(foo)becomesfoo?→ More replies (0)4
u/staticassert May 26 '16
Not steve, but I'll weigh in. I would say that Result type is the way to handle errors.
Panics are not part of the type signature. You can not reason about them and if they are triggered it should be considered a bug in the program. Results are reasonable - you know when you may encounter one, they're expected errors like a webpage being down.
3
u/desiringmachines May 26 '16
I am assuming you don't consider
Resulttype to be error-handling mechanism?To take a different fork in this conversation - why do you assume this? Maybe if we understood why you thought
Resultwas insufficient we could better explain why we think exceptions are ill-advised.→ More replies (1)7
u/kibwen May 26 '16
The docs even mention that this can be used as "a general try/catch mechanism"
Where does it say this? It's certainly incorrect. Implying that what Rust has is "exceptions" is like saying that C has "exceptions" because of setjmp/longjmp.
8
u/LordJZ May 26 '16
Right here:
It is not recommended to use this function for a general try/catch mechanism.
"Not recommended" means "you can, but you should not".
8
u/kibwen May 26 '16
catch_unwindis not a general-purpose error-handling mechanism. A library that tries to pretend as such is going to make its users miserable with the deliberate lack of ergonomic support from the language. Furthermore, once support for turning panics into aborts lands, it will be impossible for library authors to assume that panics are catchable in any capacity whatsoever.Resultremains the mechanism for handling recoverable errors.2
u/LordJZ May 26 '16
I don't know anything about aborts, but that sounds like the only practical reason against using
catch_unwindfor handling recoverable errors mentioned in this thread. Thank you.6
u/desiringmachines May 26 '16
You should not try to use this like exceptions. You should use
ResultandOptioninstead.6
u/LordJZ May 26 '16
It would be nice if someone outlined practical reasons for this.
15
u/desiringmachines May 26 '16
Exceptions introduce control paths which are untyped and of limited visibility to the programmer.
ResultandOptionare fully typed and highly visible, forcing programmers to handle error cases at the boundaries to other programmers' systems. By placing limits on the use of unwinding, we eliminate the responsibility for most programmers to write transactional "exception safe" code.The RFC discussion around catch_unwind contains a lot of discussion of the downsides of using exceptions for control flow:
3
u/LordJZ May 26 '16
The RFC discussion around catch_unwind contains a lot of discussion of the downsides of using exceptions for control flow
Please see this answer. I am certainly not trying to use exceptions for control flow.
ResultandOptionare fully typed and highly visible, forcing programmers to handle error cases at the boundaries to other programmers' systems.Might just be bad wording, but to me this sounded as a disadvantage rather then an advantage.
17
u/desiringmachines May 26 '16
Might just be bad wording, but to me this sounded as a disadvantage rather then an advantage.
Not bad wording, we have an irresolvable axiological disagreement. I think forcing you to be robust to errors in other systems is a benefit of using Rust.
Exceptions are always a control flow construct, just for a path you hope will be uncommon. You certainly are using exceptions for control flow.
2
u/LordJZ May 26 '16
By your logic, exceptions are always bad. And so is
Result, because now it is also a control flow construct for a path you hope to be uncommon. Makes no sense to me.Instead, I think "using exceptions for control flow" means having code in
catchthat does something else rather than compensating for the exception. Which is totally not what I'm trying to achieve.I think forcing you to be robust to errors in other systems is a benefit of using Rust.
Sure, but that either means that your code is ugly (see the rest of the comment thread here), or is not "robust to errors". Taking it to extreme, it means Rust is encouraging to write ugly code, which I hate to say, but that's what I actually feel deep inside. It's good to see things change with the
?operator though.20
u/desiringmachines May 26 '16
By your logic, exceptions are always bad. And so is Result, because now it is also a control flow construct for a path you hope to be uncommon. Makes no sense to me.
Let me clarify: I think that exceptions are bad because they introduce invisible, implicit, and untyped control flow paths. Results are not bad because the control flow is explicit and the program well typed.
I can't do anything about what you feel deep inside, but even when working with a great many fallible functions (parsing data from a tcp stream, so the tcp stream's errors plus invalid data errors), I have not found results made my code too ugly. I agree that
?is a delightful addition.→ More replies (12)10
u/mapofcanada rust May 27 '16
FWIW I've always been on board with the "errors are values" style error handling that both Rust and Go have decided to take on, but this explanation really drove it home for me. Nice one.
2
u/gclichtenberg May 27 '16
The RFC discussion around catch_unwind contains a lot of discussion of the downsides of using exceptions for control flow
Please see this answer. I am certainly not trying to use exceptions for control flow.
I've never understood this attitude. Exceptions are a control-flow mechanism. They can be used to implement other patterns. Smarter folks than I have even argued that their untypability is essential and useful.
4
u/CrystalGamma May 27 '16
They are useful in a similar way that completely dynamically typed languages can be useful. Rust, however, tries to be a strongly statically typed language.
→ More replies (1)2
u/crusoe May 26 '16
It definitely would be easier if Rust had monadic bind and do notation like Scala / Haskell
Closest I can find
https://github.com/TeXitoi/rust-mdo
This way the result of a series of chained Option or Either/Result computations is the final result, or the first failure encountered. It is really freaking nice, and rust needs to offer this in some fashion.
3
u/LordJZ May 26 '16
I think the
?operator proposal discussed here includescatchstatement for this.2
u/iwinux May 28 '16
To handle
Result, there is only one thing to check: what error this function returns?To handle exceptions, you need to look all the way down (DFS?). For example, when coding in Python, I often struggle with: 1. do I mis-catch anything? 2. do I catch too much?
(typing on iPhone, would like to show some code later)
-1
u/shadowmint May 27 '16
Personally I think the key thing to understand here is, where does a panic come from?
Rust is safe; rust code shouldn't panic.
There should never be a need to wrap 'perform operation here' in a wrapper in case you need to recover from a panic.
...
...but that's not how things work in practice.
In practice, rust does does panic, because of badly used unwraps(), because of bad libraries, from out of bounds access or any number of other small problems.
However, the question here is, do you feel that your code path is sufficiently risky that you need the safety of using this feature?
I'm going safe, in most cases... probably no. If all you're doing is processing some data, don't bother.
I wouldn't recommend deliberately introducing panics deliberately; that's utterly bad. It'll cause anyone who uses your library grief. ...but if you're unavoidably using code that might panic, perhaps you can help your application maintain robustness without using pushing your logic into separate threads (which was previously how you'd do this).
7
u/Perceptes ruma May 26 '16
I updated my Docker image for 1.9: https://hub.docker.com/r/jimmycuadra/rust/
The tags "latest" and "1.9.0" are now both Rust 1.9.
1
u/Breaking-Away May 27 '16
Not sure how lightweight you want to keep your Docker image, but I think racer be an awesome addition to it. Although that would also involve requiring the rustlang/rust repository be cloned somewhere on the system and setting the proper environment variable to point to it.
Is this something that would be an appropriate part of the docker image? I know very little about docker. Or maybe since racer is a development tool, it would be more appropriate in a rust-dev image of some sort.
10
May 27 '16
Base images for languages are usually meant to be as minimal as possible, intended for running applications with as little overhead as possible, not as development environments.
Of course, people do use docker for development environments as well, but usually those images are based on full distributions like Ubuntu instead.
7
u/mmstick May 27 '16
Although the entire purpose of installing Rust is for development environments exclusively. Rust isn't needed by end users running software built with Rust.
3
May 27 '16
That's an interesting point that I hadn't considered, but the usual use case is to have a Dockerfile that depends on a minimal Rust image, build your software in that, and then deploy it.
→ More replies (2)4
u/Perceptes ruma May 27 '16
My image isn't meant to be a development environment, really. Anyone doing real Rust development is going to install it on their host system. The purpose of the image is to make it easy for people who aren't really Rust programmers (but are familiar with Docker and using it as a sort of package manager) to compile Rust programs.
2
u/yespunintended May 27 '16
Anyone doing real Rust development is going to install it on their host system.
I disagree. We use Docker images for all projects, for multiple reasons:
Isolate every project with their own dependencies.
It is much easier to share environments with coworkers.
You can keep a clean/minimal host.
2
u/Perceptes ruma May 27 '16
Those are all valuable benefits. I guess I should say "most people" rather than "anyone."
6
May 27 '16 edited May 27 '16
I'm so happy that we finally can have non-blocking reads and writes in the standard library! It would be nice to have a cleaner API though. It doesn't make sense to make a socket non-blocking and then set a read or write timeout, for example. The documentation is also very sparse.
3
2
u/handle0174 May 27 '16
Nice job Rust team! Thanks for making Cargo behave well when run concurrently, Alex.
2
1
u/krstoff May 27 '16
WOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOAAAAAAAAAAH.
I had NO IDEA catch-unwind was in the works. This is huge for me! Thank you, thank you, thank you!
1
23
u/Breaking-Away May 26 '16
Specialization hype!
Big thanks to the contributors, via code, general discussion, documentation, or any other way I'm forgetting to mention!
On the topic of specialization, is there a chapter in the book on it yet?