16 ms·
Announcing Rust 1.20
- copx 9y ago"Associated functions" = class methods "Associated constants" = limited version of class variables Rust keeps approaching C++'s feature set.. I am amazed that the above wasn't in Rust to begin with, though. Can't think of any other languages which has classes but no class methods/variables.
- jandrese 9y agoNow you have me wondering what use a class is without methods or variables. It's just a namespace?
- dbaupp 9y ago"class methods" are specifically static methods, meaning they don't have a self/this object. Rust's closest equivalent to classes has always allowed normal methods and variables (as well as class/static methods, just not class/static "variables").
- e12e 9y agoThreadsafe (no externally visible mutable data) singleton instance?
- jandrese 9y agoIsn't all data (outside of unsafe blocks) in Rust already threadsafe? Isn't that one of the big selling points of the language?
- wahern 9y agoRust isn't inherently thread-safe; not like in the way Java is thread-safe because of how the JVM's memory model is defined. Rust makes it really difficult to share mutable data. But once you do that, such as by smuggling a pointer _through_ an unsafe block, all bets are off, even for code outside an unsafe block. Also, as with Java there will always be bugs in the compiler and runtime. Rust programs were susceptible to StackClash just as much as C applications. No matter what language you use, if you minimize shared mutable data you'll minimize thread-safety issues.
- CrystalGamma 9y agoIf you use a C FFI in any language, including Java, all kinds of safety are off. Unsafe Rust is equivalent to C (with a lot of mandatory lints) in terms of safety, so Rust is not really less safe.
- alphaalpha101 9y agoNot necessarily. There is no actual reason that you couldn't be required to prove to the compiler that your code is safe. That proof might be parameterised by a proof that some external FFI function was safe, which you might not be able to actually prove and have to assume, but then you would have your assumptions well-documented. As it is, you have to justify the safety of your unsafe blocks to other programmers using comments, which kind of sucks. Still better than every other fast language in this area though so I can't complain much.
- jandrese 9y agoI get that all bets are off once an unsafe() block is in play, but in the intended use case it seems like it should be safe.
- kibwen 9y agoYou are correct there. Though we can quibble over the definition of "thread safety", concurrent memory safety is a subset of memory safety, which safe Rust enforces (or at least intends to enforce, modulo bugs).
- steveklabnik 9y agoIt is data-race free. That's one definition of "threadsafe", but there could be others; it's a pretty broad term.
- dbaupp 9y agoNB. associated functions have always existed. Also, to be a little snide: "Modules" = modules "Concepts" = traits C++ keeps approaching Rust's feature set! It's normal and even expected that languages in a similar space will either draw inspiration from each other or converge on similar solutions.
- steveklabnik 9y agoWhat's funny about this is that I had worded it even less clearly, and it was changed due to feedback: https://github.com/rust-lang/blog.rust-lang.org/pull/192#discussion_r135933426 https://github.com/rust-lang/blog.rust-lang.org/pull/192#dis... Guess I didn't go far enough :)
- EmanueleAina 9y agoMaybe "In previous Rust versions you could already define traits, ..."?
- steveklabnik 9y agoSounds great, thank you! https://github.com/rust-lang/blog.rust-lang.org/commit/e74c57dd8e18b213219623e7ad51f978b85aafdc https://github.com/rust-lang/blog.rust-lang.org/commit/e74c5...
- kibwen 9y agoYou've misread the announcement; Rust has had associated functions since time immemorial. Associated consts aren't class variables, because constants can't vary (that's sort of the whole point of constants...). Rust also doesn't have classes in any recognizable sense (we can argue all day about whether Rust supports "OOP", but the separation of data and behavior into structs and impls respectively pretty thoroughly subsumes classes themselves).
- copx 9y ago>Associated consts aren't class variables, because constants can't vary That's why I wrote "limited version of". Rust's "associated constants" are a subset of C++'s class variables feature. Namely you can only have variables qualified "const" i.e. constants. >Rust also doesn't have classes in any recognizable sense What? If you have instantiatable abstract data types with associated methods you have a "class". Calling them "structs" does not change that. There are classes defined with "struct" in C++ and D too. Oh and calling an interface a "trait" does not change its fundamental nature either. Rust clearly is an OOP language.
- dbaupp 9y agoHaving aggregate data types and syntactically having methods seems like a very facile definition of "OOP", versus the conventional uses referring to Smalltalk style message passing or inheritance and overrides. Other than being able to call functions as x.foo() rather than foo(x) or foo x or similar, languages like Haskell and C seem to satisfy the requirements for being OOP, which seems to make "OOP" completely useless as a category. In any case, this point is been argued at length for every language, including Rust.
- sanxiyn 9y agoI actually think having syntactic methods is an important (if not the most important) part of "OOP". You compared x.foo() with foo(x), but that's the wrong comparison. The correct comparison is that when x if of type Tree, x.foo() vs tree_foo(x). Otherwise you get name clashes. That is, I think the essence of "OOP", OOP-as-used, not any theoretical OOP, is function name resolution depending on type. That and syntax. So first, you write x.tree_foo() instead of tree_foo(x) which is purely syntactic. And then you shorten x.tree_foo() to x.foo(), because x is a tree, which is a great semantic help. According to this definition, Haskell and C are not "OOP", which matches common understanding.
- steveklabnik 9y agoI, at least, don't really think of Rust as having classes. Rusty design patterns don't really look like many traditional OOP design patterns. This is often a hurdle for new Rust programmers. Chapter 17 of the book (https://doc.rust-lang.org/book/second-edition/ch17-00-oop.html https://doc.rust-lang.org/book/second-edition/ch17-00-oop.ht...) is trying to grapple with this question. Part of the difficulty here is nailing down what "class" even means, exactly. Rust doesn't fit into either of the three major schools of OOP, which I personally nickname the "smalltalk school", the "java school", or the "javascript school".
- zaphar 9y agoObviously Rust is in the 4'th school which I like to call "The Rust School". All kidding aside, I think for any regular working day programmer Rust is obviously OOP. The debates are really just which parts of which favorite school of OOP you think Rust is inspired by. But what really matters is that Rust gives you: * Encapsulation * Polymorphism. * And Code Reuse. Which are the only three things anyone who reaches for OOP is really looking for anyway. As fun as the PL theory discussions are. (Kind of a Hobby for me at least) I think those are the bits that actually matter to the people who truly need an answer to the question "Is Rust OOP or not?"
- steveklabnik 9y ago> Obviously Rust is in the 4'th school which I like to call "The Rust School". Ha! But yeah, if you're going to claim Rust is OOP, I would argue that makes the most sense. > what really matters is that Rust gives you: Right, this is the "Java School" definition. However, when people talk about OOP this way, when they say "polymorphism", they mean "subtype polymorphism" not "parametric polymorphism." Take https://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html https://docs.oracle.com/javase/tutorial/java/IandI/polymorph... as an example. Rust's only sub-typing is for lifetimes. > As fun as the PL theory discussions are. I definitely agree that in some senses, this is academic. But at the same time, practitioners can be mis-led by using terms in a different way than they're used to. This is sort of the argument I'm making above; since many practitioners see "polymorphism" as being equal to "subtype polymorphism", calling other types of polymorphism "polymorphism" is a more academic, less user-focused distinction.
- deleted 9y ago[deleted]
- tatterdemalion 9y agoAssociated consts allows constants to be derived from our trait-constrained parametric polymorphism. C++ currently has no analog to this system (templates can be used, but don't provide a constraint system; concepts are not in C++17 afaik). At an introductory level, like this announcement, we teach this system by analogy to OO, but its not OO in its core and snide comments like this only reveal the limits of your knowledge.
- steveklabnik 9y ago> concepts are not in C++17 afaik That's correct, though they have been added to the C++20 draft: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0734r0.pdf http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p073...
- tomjakubowski 9y ago> Rust keeps approaching C++'s feature set.. If there's one thing we C++ programmers can agree on, it's that having tons of features makes C++ a pleasure to work with... ?
- trapperkeeper74 9y agoMaybe by "pleasure" they mean "obscurity, gotchas and complexity are pleasurable because they trip up newbies and lend themselves to job security."
- jmcomets 9y agoI used to be a huge fan of C++. I loved how you could work with a somewhat OO language while still wielding dark powers like: manual memory management, mix-n-match polymorphism (aka. virtual inheritance), templates, type-erasing. Then I got a full time job working with a large C++ codebase. Since then I've been dealing with: - uninitialized variables / members - NULL checks - buffer overflows, especially with C-strings - (mutable) global variables - exception safety All these things don't exist in (safe) Rust. I'm sold. Foot note: the only thing I'm really missing in Rust right now is specialization in generics, which is being worked on here: https://github.com/rust-lang/rust/issues/31844 https://github.com/rust-lang/rust/issues/31844.
- PopsiclePete 9y agoThat's my main beef with C++. You read a book and it's beautiful - no uninitialized memory, safety, no C arrays/strings, RAII everywhere, it's clean, fast.....then you get to the "real world" and realize that 99% of C++ programmers are pretty f###ing horrible at their job and should've just stuck to naked C because at least then you'd be able to pin-point the bugs easier. So C++ is a wonderful language - in a world that only exists in the creators' heads, or on committee tables, or on brand-new, post C++03 codebases. Basically - nowhere.
- ryl00 9y agoBut isn't that the case with basically ANY programming language? Once its use becomes widespread, escaping the confines of the highly skilled early adapters, you can be sure a whole lot of dodgy code will be written in it.
- pjmlp 9y agoRegarding languages with OOP support, but lacking class methods. - Object Pascal (in the TP days) - Modula-3 - Oberon, Oberon-2, Active Oberon, Oberon-07 - Component Pascal You need to resort to plain functions/procedures instead.
- kibwen 9y agoInterestingly, because Firefox chooses to use stable Rust exclusively, this is the version of Rust that will be used to deliver Quantum when Firefox 57 releases on November 14 (by which time Rust 1.21 will be out (releasing October 12), but Firefox 57 will be in beta by September 20).
- tempay 9y agoTo save others from searching, Quantum[1] is the new engine for Firefox replacing(/building upon) gecko. [1] https://wiki.mozilla.org/Quantum https://wiki.mozilla.org/Quantum
- dblohm7 9y agoQuantum is the project, not the engine. The resulting product is still Gecko.
- Vinnl 9y agoAnd the goal of the project is to significantly improve the performance of Firefox.
- bad_user 9y agoSo they are already switching to the new Quantum-powered Gecko in Firefox 57?
- cpeterso 9y agoThere is no single switch. Quantum is an ongoing initiative to import more code from Servo into Firefox/Gecko and to improve Firefox responsiveness.
- bad_user 9y agoAwesome.
- giancarlostoro 9y ago
- kbenson 9y ago> An unstable sort could provide this result, but could also give this answer too: It might just be me, but using "this" twice in the same sentence to refer to two distinct items, one a prior example and one an upcoming example, is somewhat odd. That said, I understood it perfectly fine, it just caused me to stop and ponder the wording for a moment. The following might be more clear: An unstable sort could provide that result, but could also give this answer too:
- steveklabnik 9y agoSounds great, I've made the change. Thanks! https://github.com/rust-lang/blog.rust-lang.org/commit/92031b9f4c7783c7442da421223a6ef3325738f3 https://github.com/rust-lang/blog.rust-lang.org/commit/92031...
- coldtea 9y agoWell, while you're at it, "also" and "too" perform the same role there. So that phrase could be shortened to "but it could also give this answer".
- evincarofautumn 9y agoIn case anyone’s curious, this is called discourse deixis[1]. It’s a frequent source of errors for non-native English speakers, because in many languages, you use “that” to refer to an example that follows, but English is unusual in that it generally uses “this” for the future and “that” for the past. So that[2] sounds wrong: This[3] probably screwed you up. The somewhat confusing thing is that “this” is also used for the present or recent past, especially in more formal writing. [1]: https://en.wikipedia.org/wiki/Deixis#Discourse https://en.wikipedia.org/wiki/Deixis#Discourse [2]: The following example [3]: The previous sentence
- JoshTriplett 9y ago> English is unusual in that it generally uses “this” for the future and “that” for the past. I don't know that I'd describe it as "future" and "past"; it's more that English uses "this" for "current" and "that" for "other", whether past or future. For instance, "this situation is broken, that solution looks promising" uses "this" to refer to the present and "that" for the future.
- sevensor 9y agoNot a Rust programmer, so the answer to this question may be in TFM -- Rust has floats, but it doesn't make available constants for inf and NaN? Are you not guaranteed IEEE754 floats? const NAN: f32 = 0.0f32 / 0.0f32; const INFINITY: f32 = 1.0f32 / 0.0f32; Or is that just for the sake of example?
- Mouse47 9y agohttps://doc.rust-lang.org/std/f32/ https://doc.rust-lang.org/std/f32/ I believe you can do std::f32::NAN
- eddyb 9y agoIt's just for an example - see https://doc.rust-lang.org/std/f32/ https://doc.rust-lang.org/std/f32/.
- tatterdemalion 9y agoThe consts are currently defined in modules with the same name as the type, `std::f32::NAN;`. This feature allows them to be associated with the type itself - `f32::NAN`. A small convenience.
- kzrdude 9y agoIt's more than a small convenience. It's a big feature that we can use when designing traits and APIs.
- tatterdemalion 9y agoYes of course (I work on Rust). I mean that the difference between f32::NAN and std::f32::NAN is a small convenience.
- whyever 9y agoThis is how making the constants available could look like. Currently they are available via a module, and they are implemented like above: https://github.com/rust-lang/rust/blob/master/src/libcore/num/f32.rs https://github.com/rust-lang/rust/blob/master/src/libcore/nu...
- slavik81 9y agoIs f32 the only suffix for float literals? That seems really noisy compared to just f in C. I'm mostly concerned about vector and matrix declarations, but even in the examples given, 1.0f32 / 0.0f32 looks like a mess of numbers compared to 1.f / 0.f;
- kibwen 9y ago> Is f32 the only suffix for float literals? Nope. `f32` is for single-precision floats, and `f64` is for double-precision floats. There have been some people lobbying for `f16` and `f128` as well. Also, you don't need to use those suffixes. I imagine the OP is using them for maximum explicitness, but you can just write `1.0` and it will be inferred to a floating-point type as necessary (contrast `1`, which will be inferred to an integral type).
- steveklabnik 9y ago> I imagine the OP is using them for maximum explicitness I used them because that's the style that was used inside of std::f32; const INFINITY: f32 = 1.0 / 0.0; totally works.
- tomjakubowski 9y agoGLSL programmers may rejoice in learning that `1.` is accepted syntax too.
- GenKali 9y agoHaving used Rust since pre-release I'm slightly embarrassed that I'm only learning about this today.
- ComputerGuru 9y agoThat's ok. I presume you weren't a C/C++ dev before, that's old school shorthand for C coders that don't want to type out suffixes or cast numeric values.
- 9y ago
- pcx 9y agoAssociated functions and associated constants sound so yum! That's one thing I would love to have in Flowtype. But classes seem to serve that use case well enough.
- SideburnsOfDoom 9y ago> Associated functions and associated constants sound so yum! In the simple (not trait) case they just seem to be the same as "public static" constants and functions in C#, java etc.
- a_humean 9y agoYou get something similar to with static methods and properties on class definitions. In plain ES2017-ish class syntax: class Test { static value = 1; static someStaticMethod = () => { return 5; } } console.log(Test.value) // 1 console.log(Test.someStaticMethod()) // 5 Edit: Sorry, this actually a Stage 3 TC39 feature. Not sure if you can use it with flowtype (I think you can), but you might be able to if you enable babel with stage-3 features: https://github.com/tc39/proposal-class-fields https://github.com/tc39/proposal-class-fields You don't actually need this fancy class syntax anyway. You can just define a class and then do: Test.value = 5 Test.someStaticMethod = () => { return 5; } The class stuff is just sugar over the prototype stuff anyway, and a class is just a constructor function object with a prototype chain.
- pcx 9y agoYeah, either way it is objects. Traits feel so much more flexible though; and more natural a layer over JS object-orientedness. It also makes it a pain that there are some important semantics missing while using classes in JS. Someitmes Java feels much better.
- akatechis 9y agoThe real problem is that ES classes map onto a paradigm of prototypal inheritance rather than traditional inheritance (as in Java). The discrepancies between the two cause a leaky abstraction, like how a class still has a prototype chain, for example.
- SloopJon 9y agoIn case anyone else runs into this, I was getting this error trying to update from 1.19: $ rustup update stable info: syncing channel updates for 'stable-x86_64-apple-darwin' error: missing key: 'url' After I updated rustup from 1.0.0 to 1.6.0 with rustup self update, it worked.
- _wmd 9y agoWhat happened to the exhaustive list of unnamed Github contributors? I thought that was a really powerful marketing method: dear unnamed Github user x1230134384, your contribution has been acknowledged! Feedback without requiring identification, it's kindof very cool in a project like this.
- steveklabnik 9y agoIf you click on the final link to thanks.rust-lang.org, you'll find it there! There have been talks about moving it back, but it's complicated. You can do the "duplicate it" strategy, but that has downsides. You could do an iframe, but then you're using an iframe. You could use JavaScript, but then a whole different crew of people will Get Mad.
- kibwen 9y agoBut if you use iframes, then Servo will get to show off its intra-page content isolation. :)
- kbenson 9y agoObviously the solution is to turn the thanks page into an API, add CORS headers, and then a bit of JS on the announce page to fetch the list from thanks.rust-lang.org and build the list on load or fall back to the current behavior. Good luck determining whether I'm joking or not. I'm not even sure myself...
- jeffdavis 9y agoI've been having a little trouble using rust for a little project: I need a tree with uplinks (meaning there are cycles). I asked on IRC a couple times and I think what I need is a weak reference inside a refcell, but it's not very easy to make it work cleanly. For one thing, it doesn't look like refcell works well with traits (the nodes in the tree are traits, not plain structs). I'm a bit frustrated because this is so easy in C. Is there any way this can be easier? I am imagining a special way to construct cyclical structures where everything inside would have the same lifetime and be destructed at once. We may need to hide the references from destructors though, otherwise a destructor could see a dangling reference.
- kibwen 9y agoHave you considered leveraging crates.io? I'm not sure of your exact constraints, but I've heard some good things about petgraph: https://crates.io/crates/petgraph https://crates.io/crates/petgraph . It sounds like you might also want to check out some implementations of arenas.
- steveklabnik 9y ago> a weak reference inside a refcell, It'd likely be the other way, that is, you'd put a RefCell inside of an Rc/Weak. > it doesn't look like refcell works well with traits It should; I bet you had problems since you did it the other way around. > I'm a bit frustrated because this is so easy in C. You could do it the same way as you do it in C, if you're willing to resort to `unsafe`. FWIW, Rust sort of changes the equation for what's easy here; thread-safe concurrency? Simple! Data structures? Hard! C is the other way around. So feeling a bit frustrated is normal; your C skills won't carry over, but it feels like they should. > I am imagining a special way to construct cyclical structures where everything inside would have the same lifetime and be destructed at once This sounds like an arena to me, which is another option, for sure. If you post to https://users.rust-lang.org/ https://users.rust-lang.org/, someone might be willing to whip up an example, or if you post your in-progress stuff, someone might be able to fix it for you. https://crates.io/crates/petgraph https://crates.io/crates/petgraph might also just already have what you need, maybe.
- 9y ago
- shmerl 9y agoLooks like this is moving now: https://github.com/rust-lang/rfcs/pull/1615 https://github.com/rust-lang/rfcs/pull/1615 because of recently added $XDG_BIN_HOME.
- phkahler 9y agoSo are these associations (functions and constants) just using the object type as a name space? Or is there something more to it?
- kzrdude 9y agoOnly that they are members of a trait, so you can use it in Rust's generic programming (bounded polymorphism).
- leshow 9y agois bounded another word for ad-hoc? I always considered Rust's polymorphism implementation like Haskell's, and AFAIK they call it ad hoc polymorphism.
- steveklabnik 9y agoIIRC, "bound" has more to do with quantification than polymorphism more generally. There is "parametric" vs "ad-hoc". https://stackoverflow.com/questions/6730126/parametric-polymorphism-vs-ad-hoc-polymorphism https://stackoverflow.com/questions/6730126/parametric-polym... has a link to the text of TAPL explaining the difference here. Rust traits and Haskell typeclasses would be ad-hoc.
- leshow 9y agoThat's interesting, I always assumed parametric polymorphism was a requirement of ad hoc polymorphism. I suppose I'm using a more narrow definition. On the opposite end, Elm for instance, has parametric polymorphism: type Parametric a = Parametric a f : (a -> b) -> Parametric a -> Parametric b f fn (Parametric a) = Parametric <| fn a but no support for ad-hoc, like you said, in Haskell: data Parametric a = Parametric a f :: Ord a => Parametric a -> Parametric a -> Parametric a f (Parametric a) (Parametric b) = Parametric (min a b)
- mikebenfield 9y agoIt may be easier to see their significance when they're associated with a trait, rather than a struct. For a simple example, imagine I've got a trait Number, and every implementation of Number is supposed to have a zero constant. Then I could let that constant be Number::ZERO, and refer to it as such in generic code, with each implementation of Number having a different value for Number::ZERO.
- firebones 9y agoMay be too late in this thread for a response, but does the completion of the 1.20 release free up some time for the "State of Rust 2017" survey results blog post before the 1.21 release? I know there was a huge response, and you don't want to step on the release news, but I'm waiting to see where the user community stands.
- steveklabnik 9y agoIt's not really about that; the post is almost done, and you should see it soon. It was a lot of work to go through.
- chrismorgan 9y agoWhile thinking about such things—what happened to https://underhanded.rs/ https://underhanded.rs/?
- steveklabnik 9y agoResults post should be up soonish; I know it's being worked on.
- fdchn2016 9y agoIs there a book on Programming Data Structures in Rust. Like from scratch. Trees, Graphs etc. I would expect the first 2 to 3 chapters to be on Rust pointer system and the rest of the book to be on implementing Data Structures using the pointer system. Similar to like Tanenbaum's book for Data Structures in C or Kruse, Leung and Tondo's book for Data Structures in C.
- kybernetikos 9y agoIf I can't code some data stuctures in a language I don't feel like I know it, so I would like something like this too. The closest I found was the 'too many linked lists' book, which I learned a lot from. http://cglab.ca/~abeinges/blah/too-many-lists/book/ http://cglab.ca/~abeinges/blah/too-many-lists/book/
- andrewflnr 9y agoIn Rust, you almost always want to use a library for things like that. If you want to write the library, you may want the Rustonomicon: https://doc.rust-lang.org/nomicon/ https://doc.rust-lang.org/nomicon/. Also, of course, the "Too Many Lists" article in the other reply. Rust definitely pushes a different way of thinking about and using data structures.
- xoroshiro 9y agoAnd here I am, still waiting for the second edition of that book on learning Rust to be finished. I want to learn Rust for fun and I know I should probably just start on reading the book, but I keep making up excuses not to. I want my ++, --, ?:, SIMD, etc.
- steveklabnik 9y agoWe had some delays, but things should wrap up on the book soon! ++, --, and ?: are not happening, but SIMD is actively being worked on!
- lalaithion 9y agoyou've got `let x = if y {100} else {99};`, which IMO is more readable than `let x = y ? 100 : 99`.