10 ms·
Announcing Rust 1.22
- mkl 9y agolet mut x = 2; let y = &8; // this didn't work, but now does x += y; Why is it desirable that that work? It seems like it will mask mistakes and make types more confusing.
- mark_edward 9y agoI agree. Some of the things done in the name of ergonomics just strike me as wrong and/or possibly misleading for learners. People who say Rust is hard to learn have always confused me. I learned programming in this order: scheme, C, C++, Haskell, Rust. I believe that if you go in another order it may make things more difficult, but some of these difficulties are inherent. To me, a reference to a thing is inherently different from the thing, and a systems language should make that clear. I didn't even like auto-deref, I felt like there should be an operator similar to thing->method() in C++ that can be chained so that references and indirection would always be syntactically clear and distinct, but the ergonomics on that won out.
- kibwen 9y agoThe change in question wasn't done because of ergonomics, it was done for consistency with the rest of the language. Read about it in the originating PR (which was not linked directly from the blog post): https://github.com/rust-lang/rust/pull/41336 https://github.com/rust-lang/rust/pull/41336
- coldtea 9y ago"People who say Rust is hard to learn have always confused me. I learned programming in this order: (...)" (proceeds to list a quite rare and outlier-ish learning sequence, and all in non-scripting languages to boot). Well, of course those people confused you. What you, who learned Scheme, C++ and Haskell before going to Rust, have in common with someone who only knows C or Java or only got his start in some scripting languages, so as to be able to relate with their "Rust is hard" experience?
- mmstick 9y agoI personally went from Go (slightly mingling with it) -> Rust 1.0 (wrote my first applications with it) and found it to be incredibly easy to learn in comparison to C, C++, Java, and even Python and JavaScript. Everything's pretty explicit and straightforward, and the API documentation and associated resources are stellar.
- Manishearth 9y ago> a systems language should make that clear Folks always say this but never really clarify why this should be the case. When doing low lever unsafe stuff Rust does indeed make it very clear if something is a pointer or not, and avoids weird coercions (even C doesn't do well here!) But for normal code, does it really matter if something is a pointer or not? In C++ already with by-ref and move values and stuff you have implicit pointers in many places that don't behave as pointers. Rust never goes the way of adding implicit allocations or implicit indirection; it only goes in the way of stripping it, which is rarely if ever a problem except when dealing with unsafe code (where Rust is more explicit on pointers anyway).
- kosinus 9y agoConsidering pointer arithmetic is unsafe, there are no other interpretations, I think? I can’t think of an example where this would mask a mistake in Rust. As for confusion, both are accepted. I imagine clippy will eventually have advice on this situation. For example, clippy also warns you about taking a reference that the compiler immediatly dereferences. I’m not sure what the long term plans are for clippy, but personally hope it’ll eventually be warnings in the official compiler.
- DSMan195276 9y ago> Considering pointer arithmetic is unsafe I just wanted to point out that this is largely a misconception. Rust, or the Rust creators, do not consider pointer arithmetic to be fundamentally `unsafe`, and the only reason `.offset()` is `unsafe` to begin with is due to optimization concerns[0]. `.wrapping_offset()` exists and is marked safe, despite achieving the exactly same thing as `.offset()` for the majority of scenarios[1], and more-over you can cast any pointer to an integer using only safe code, at which point you can perform all of your arithmetic on the integer and then cast it back also using only safe code[2]. The bottom line is that things like pointer arithmetic are generally not considered unsafe because the operation has defined semantics for what happens. It is only the point where you attempt to use the value, the dereference, that is `unsafe`, as that may blow-up if the value is an invalid memory location. [0] https://github.com/rust-lang/rust/issues/33813 https://github.com/rust-lang/rust/issues/33813 [1] https://play.rust-lang.org/?gist=c9c11545fac3a45550e6810cb579e4e9&version=stable https://play.rust-lang.org/?gist=c9c11545fac3a45550e6810cb57... An example that uses `.wrapping_offset` without `unsafe` to produce a segfault. [2] https://doc.rust-lang.org/book/first-edition/casting-between-types.html#pointer-casts https://doc.rust-lang.org/book/first-edition/casting-between...
- nicoburns 9y agoBut generally there's no point in doing pointer arithmetic unless you are going to deference the pointer at some point, right?
- DSMan195276 9y ago
- kibwen 9y agoIt's desirable from the standpoint of consistency, given that the following equivalent code has always worked: let mut x = 2; let y = &8; x = x + y; What mistakes could this be masking? I can't think of any.
- mkl 9y agoI didn't realise it already worked in other contexts. Mistakes in terms of understanding the code and writing what you think you're writing. I've only just begun learning Rust, and one of the most difficult things has been figuring out what types my variables have when everything is implicit and based on type inference. Variables that act both as pointers and non-pointers seem pretty confusing. It's like temporarily and invisibly turning off parts of the type-checker. ETA: To me it feels a bit like the icky type coercion magic some languages have that lets you write code that kind of works even though you don't understand what you're doing. I don't know Rust though, so this may be totally different.
- Jare 9y agoDon't worry, by the time the borrow checker is done with you, you will understand what you are doing like you've never before. This is a good thing.
- kibwen 9y agoIf you happen to be coming from C, keep in mind that integers in Rust don't coerce into anything else like they do in C; e.g. adding an 8-bit integer to a 32-bit integer (without an explicit cast) is a type error, `if 0` does not work, arithmetic ops on references never do pointer arithmetic, etc.
- fredmorcos 9y agoOkay, so I came from C and this part really put me off. Why? Because I like saying what I mean, and meaning what I say. Nothing hidden, nothing implicit. And C, as opposed to C++, does not allow you to say something you don't mean. Here is one C++ example, without looking for the signature of f(), there's no way to tell the answer. So essentially two pieces of identical code with identical input values and no external side effects can still give you different output. Ah C++, the garbage that you are. int x = 5; f(x); // WHAT IS THE VALUE OF X HERE? int x = 5; f(x); // WHAT IS THE VALUE OF X HERE? So why is Rust any better in this regard? Because at the end of the day, Rust deals with actual concrete types and values, the only thing references are good for is being references: they sometimes save you some copying and let you refer to stuff and that's pretty much it. So the "ref1 == ref2" operation is a deep equality check and not a "does ref1 point to the same object as ref2" check. Because sometimes objects in different places in memory can be semantically equal. So it's okay for obj1 to mean obj1, &obj1 to mean obj1, &&obj1 to mean obj1, etc... If you need raw pointers like you need in C, because you know the objects you want to compare are singletons, then you can always cast using &obj as *const Obj or &mut obj as *mut Obj But that's an escape hatch. BTW, this auto-dereferencing you're experiencing is part of the Deref trait if you ever want to overload it. But that's, in my own opinion, an escape hatch as well. All in all, unless you're doing very specific things, try to write your code in high-level semantics (meaning using these deep-equality rather than pointer-equality semantics), and then benchmark and find out which parts are hurting your performance. Rust allows you to do that and still get between very-reasonable-and-very-good speed.
- drewm1980 9y agoI have no idea what that ? operator does from the summary or staring at the usage example... Was more boilerplate necessary in the example before?
- drewm1980 9y agoOk so I guess it's a trinary operator with two arguments combined that is able to use Option and Result for the test... I hope autoformatters keep stuff like that on one line!
- GolDDranks 9y agoIt's an error handling operator that does an early exit in case of None and unwraps the value in case of Some. It used to work only with Result: early exit when Err, unwrapping the value when Ok.
- Manishearth 9y agoNah, in that example there should be a `return` before the `None` -- because it's the only block in the function it doesn't matter. But it's not a ternary operator, it's unary. let val = foo.bar()?; will call `bar()`, return if it is None, and if it is `Some(foo)`, set `val` to `foo`.
- viraptor 9y agoIt skips a block you'd need otherwise. Without it, you'd have to do something like: fn try_option_some() -> Option<u8> { if let val = Some(1) { Some(val) } else { None } } It's an early exit if the expression before is None/Error
- Sharlin 9y agoThere should really be a more real-world example, the current code (and your alternative version) is just redundant and equivalent to fn try_option_some() -> Option<u8> { Some(1) }
- kibwen 9y agoReminder that Rust's year-end sprint has one more month to go, and even though we've gotten tons done there's still plenty of work to go around to anyone with motivation and idle hands. :) See here for an overview: https://blog.rust-lang.org/2017/09/18/impl-future-for-rust.html https://blog.rust-lang.org/2017/09/18/impl-future-for-rust.h... and here for the most recent checkpoint summary: https://internals.rust-lang.org/t/impl-period-newsletter-3/6185 https://internals.rust-lang.org/t/impl-period-newsletter-3/6... We'd love your help!
- akmittal 9y agoThere was a PR recently which support wasm compilation without emcc. Does anyone know when it is expected in stable? https://github.com/rust-lang/rust/pull/45905 https://github.com/rust-lang/rust/pull/45905
- killercup 9y agoGood question! It was merged quite close to the release, so it's hard to guess if it landed in beta already. So, let's have a look at [1]! And yep, it's in the beta branch. This means it's in 1.23, which'll be released in 6 weeks. [1]: https://github.com/rust-lang/rust/commits/beta https://github.com/rust-lang/rust/commits/beta
- MrBuddyCasino 9y agoDoes that mean I will be able to compile to wasm via a simple cargo parameter? If so - wow!
- killercup 9y agoYes, once [1] lands, you can add it with `rustup target add wasm32-unknown-unknown` and then you can `cargo build --target=wasm32-unknown-unknown`. You should note that it's not as simple as that, though. Depending on the context you want to run WASM you might need to do additional steps. E.g., when running this in a browser, you might not be able to use the std lib to spawn threads just yet. [1]: https://github.com/rust-lang/rust/pull/46115 https://github.com/rust-lang/rust/pull/46115
- margorczynski 9y agoHey, is there any plan to add FP programming (functors, mondads, free monad, etc.) to Rust as part of the language or some officialy supported library? Also - any plans for Streaming? I'm a big Scala user and fan (Haskell of course too) and would like to try out Rust but I really don't see myself going back to structural/object oriented-programming
- killercup 9y agoYou can write Rust in a quite functional matter, but as a language it works quite differently from Haskell or Scala. There's been a lot of discussion [1] around extending the type system to support something akin to higher-kinded types. The current result is the RFC you can find in [2], as well as a work-in-progress implementation of a new trait-resolution system for use in the compiler [3]. [1]: http://smallcultfollowing.com/babysteps/blog/2016/11/02/associated-type-constructors-part-1-basic-concepts-and-introduction/ http://smallcultfollowing.com/babysteps/blog/2016/11/02/asso... [2]: https://github.com/rust-lang/rfcs/pull/1598 https://github.com/rust-lang/rfcs/pull/1598 [3]: http://smallcultfollowing.com/babysteps/blog/2017/01/26/lowering-rust-traits-to-logic/ http://smallcultfollowing.com/babysteps/blog/2017/01/26/lowe...
- whyever 9y agoThere are already FP features and monads in Rust. What exactly do you want? It is unlikely you will ever see non-zero cost abstractions in idiomatic Rust.
- fmap 9y agoCan you link to some documentation about how monads work in rust? Based on the sibling comment rust doesn't support higher-kinded types yet, so I'm interested in how you would encode this in rust.
- steveklabnik 9y agoRust does not have HKT, and so we cannot write a generic Monad. Rust has many instances of Monad, though. That said, at some point, we'll be getting ATC, which is equivalent to HKT in many cases, but feels more Rusty.
- jayflux 9y agoAs these announcements get a lot of attention from non-rust devs it might be worth linking to what ? Actually does somewhere in that.
- wscott 9y ago"About a year ago, in Rust 1.13, we introduced the ?" And that includes a link to the 1.13 announcement with a detailed explanation.
- skywhopper 9y ago"Two recent compiler changes should speed up compiles in debug mode. We don’t have any specific numbers to commit to with these changes, but as always, compile times are very important to us, and we’re continuing to work on improving them." Can't imagine why compile times haven't been good in the past with this level of detailed analysis.
- killercup 9y agoThere's a difference between having numbers and having specific numbers to commit to… If you want raw numbers, some are tracked at http://perf.rust-lang.org http://perf.rust-lang.org Also, incremental compilation is making very good progress. More fuzzy numbers: I'm currently working on a mid-size Rust project, where the app crate has around 10k lines. `env CARGO_INCREMENTAL=1 cargo run --release` takes 2-3 seconds depending on what I changed.
- Manishearth 9y agoNot all improvements give you specific numbers. We track a lot of performance data, but some changes may give an extremely broad range of improvements so you can't say much about the numbers.
- steveklabnik 9y agoTo be clear, I wrote the announcement, but don't work on compiler optimizations. Those that do tend to have a better idea than me. But beyond that, it's ultimately about what you promise. Say something sped up compilation times 2x for most people, but slowed them 10x in a corner case. If you promise the 2x, the people for who it slowed down will be (rightfully) pretty mad. None of these changes are as drastic as that, but still.
- alfiedotwtf 9y agoDoes anyone know why the last two lines in main() fail? AFAICS, v has the same type as both functions. fn try_option_some() -> Option<u8> { let val = Some(1)?; Some(val) } fn try_option_none() -> Option<u8> { let val = None?; Some(val) } fn main() { assert_eq!(try_option_some(), Some(1)); assert_eq!(try_option_none(), None); let v: Option<u8> = None; assert_eq!(v, Some(None)); } One other thing troubling me here - shouldn't try_option_none() return `None` and not `Some(None)`? Looking at the diff for the commit, there's now a NoneError? Is there any documentation updates to look at how this works? Because AFAIK, now all matches will have to explicitly be updated to check for NoneError, otherwise they won't compile (since matches are not complete)?
- steveklabnik 9y agoAs the error says, = note: expected type `std::option::Option<u8>` found type `std::option::Option<std::option::Option<_>>` The return types of those functions are Option<u8>, but Some(None) is an Option<Option<_>>. > there's now a NoneError? It's not stable yet. > now all matches will have to explicitly be updated to check for NoneError NoneError only comes into play when you have a function that returns Result, but uses ? on an Option in the body. That code never compiled (and still doesn't on stable until Try is stabilized, and only if you have Result<T, NoneError> as your return type or some Err type that implements From<NoneError>, which you also can't write until it's stable. Does that help?
- alfiedotwtf 9y agoAh, awesome. Makes total sense now. Thanks!