23 ms·
Rust 1.17
- kevindqc 10y ago> String concatenation appends the string on the right to the string on the left and may require reallocation. I don't really know Rust, but isn't that weird? In most languages I know, I would expect for example Vector3(1, 2, 3) + Vector3(2, 3, 4) to return a brand new Vector3(3, 5, 7), with Vector3(1, 2, 3) unmodified? Unless I do vector1 += Vector3(2, 3,4), at which point I expect vector1 to be modified without creating a new vector? Why doesn't rust create a new String and concatenates using this new owned String? I guess that's why beginners get it wrong - I would
- vvanders 10y agoIt's because you don't have a String to begin with, just two const memory values(which most of the time are baked into the program binary in C/C++, not sure about Rust).
- danieldk 10y agowhich most of the time are baked into the program binary in C/C++, not sure about Rust Pretty much the same, string literals are of type &'static str, which is a string slice with a static lifetime. Since literals are not `mut`, you cannot mutate them.
- vvanders 10y agoYeah, semantically you're right. I'm talking about actual binary representation. Some systems/runtimes copy them to the heap/etc while others let you reference the actual memory in the loaded binary. Back when I did more embedded stuff we cared since it could increase your runtime memory by a non-trival amount depending on what the platform did.
- steveklabnik 10y agoIn Rust's case, to be clear, they're stored in .rodata. That's where the &'static str points to. The auto copy to the heap behavior is controversial, desired by many for ergonomic reasons, disliked by others for implicit heap allocation. We'll see if it gets added in the future or not!
- vvanders 10y agoYeah, you'd find me in the latter camp for sure :). Thanks for the details, it's appreciated.
- deleted 10y ago[deleted]
- pcwalton 10y agoBecause the left-hand side is consumed by the + operator, so its allocation can be reused. Most languages don't have the concept of ownership, so it works differently in other languages. If you want to get the semantics of other languages, just clone the left-hand side with ".clone()".
- moomin 10y agoThis is kind of exactly why Rust is worth having. There's plenty of other languages that do the same thing as Java, and it's the right choice for a lot of use cases. Rust, instead, goes for the memory efficient design that doesn't create junk on the heap. Not always the right choice, but well worth having the option.
- zuzun 10y agoJava is a bad example since it could (theoretically) do all kinds of optimizations based on runtime profiling under the hood. It also has features like deduplication, so your string might use no additional memory at all.
- vog 10y ago> Java is a bad example since it could (theoretically) do all kinds of optimizations based on runtime profiling under the hood Isn't that true for every language? No offense intended, but this strongly reminds me of the "Sufficiently Smart Compiler" [1] The Java community had more than enough time to implement that, especially compared to a young language like Rust, so there's probably a reason they don't have it yet. [1] http://wiki.c2.com/?SufficientlySmartCompiler http://wiki.c2.com/?SufficientlySmartCompiler
- twic 10y agoThey have implemented it. javac compiles chained string concatenation to a sequence of StringBuilder calls, and HotSpot knows how to specifically optimize those: https://groups.google.com/forum/m/#!topic/jvm-languages/KtVBYfeT83g https://groups.google.com/forum/m/#!topic/jvm-languages/KtVB... That said, I'm sure there are common cases it doesn't catch.
- Manishearth 10y agoBecause &str is just a string view. If you add vectors, you are adding owned types where you own the allocations. &str is not owned, but String is. We could make the addition operator implicitly allocate here. It's a bit of a philosophical question if implicit allocations like that are desirable in rust. I lean towards being okay with the implicit allocation.
- pc2g4d 10y agoYou could maybe introduce a lint saying "possibly unnecessary allocation" when people `+` `&str`s together. Then beginners can still use the syntax that they're familiar with (and that basically every other language uses) but there's still notification somewhere that they can likely structure their code to be more efficient.
- Lev1a 10y agoIf I understand the ownership rules correctly, it would actually violate those rules to have the "familiar syntax". Also it seems to be Rust's thing to make things explicit, e.g. `.clone()`, not having `++`-style operators etc.
- steveklabnik 10y agoIn this case, due to the rules, it would be possible; the call to clone would be inside the method call, so you wouldn't see it. (With other signatures it may not, but Add returns a value, not a reference.) In other words, it would violate the conventions, but not the rules. (Okay so there's a _really_ obscure rule here that sill prevents this but it only matters because it's the standard library. It's not actually the signature)
- eximius 10y agoI would definitely side with it being okay. I like how string are modeled, but it doesn't mean it isn't painful sometimes. Making some common string operations like &str + &str defined but linted is a good balance of ergonomics and efficiency (that'd be a rare bottleneck, but the lint would make it visible).
- jandrese 10y agoThis is how concat() works in the C library, except that in the case where the original string is too long it will overwrite random memory instead of reallocating the original string.
- mbel 10y agoYou probably mean strcat() (which should really not be used, since now there is strncat()), there is no concat() in standard C library. strcat() does not intercept syntax of operation that does not mutate its arguments in any other context (+), you also cannot do strcat("hello,", " world") since the first argument must be mutable.
- nercury 10y agoIt is fine, because you can't accidentally use modified string on the left.
- humblebee 10y agoAh yes, this is what I was a bit confused about. Rust's ownership system though makes it so, what would be a very subtle and annoying bug I'm sure, can't happen. The error is really helpful though. fn main () { let hello = "Hello".to_owned(); let world = "world"; let hello_world = hello + world; print!("{}, {}", hello_world, hello) } 4 | let hello_world = hello + world; | ----- value moved here 5 | print!("{}, {}", hello_world, hello) | ^^^^^ value used here after move = note: move occurs because `hello` has type `std::string::String`, which does not implement the `Copy` trait The docs are also pretty awesome as well [0]. They also point out most of this and list out all the types which implement the copy trait. > Copies happen implicitly, for example as part of an assignment y = x. The behavior of Copy is not overloadable; it is always a simple bit-wise copy. > Cloning is an explicit action, x.clone(). The implementation of Clone can provide any type-specific behavior necessary to duplicate values safely. For example, the implementation of Clone for String needs to copy the pointed-to string buffer in the heap. A simple bitwise copy of String values would merely copy the pointer, leading to a double free down the line. For this reason, String is Clone but not Copy. > Clone is a supertrait of Copy, so everything which is Copy must also implement Clone. If a type is Copy then its Clone implementation need only return *self (see the example above). I haven't really used Rust at all, but the more I look at it the more I want too. I probably will not be given the opportunity anytime soon though. Such is life in the world of frontend development. [0] https://doc.rust-lang.org/std/marker/trait.Copy.html https://doc.rust-lang.org/std/marker/trait.Copy.html
- baq 10y agowebassembly is our hope. compile rust down to browser bytecode. a man can dream.
- Rusky 10y agoRust's + implementation consumes the String on the left, so the default behavior doesn't do any allocation unless the string needs more capacity. This makes it impossible to "get it wrong" in the way you seem to be suspecting. The part beginners tend to get wrong is trying to add a &str to a &str, which just doesn't have a + implementation to begin with, so they get lost.
- steveklabnik 10y agoSo I should preface this by saying that the Add impl was generally controversial and almost not even implemented, but ended up happening. In a systems language, allocations matter. As such, people are generally really against hidden allocations in Rust, moreso than in other languages. This totally could have been done, but by requiring a String on the left, you are already in "this is heap allocated" land, and that is much more palpatable for Rust's audience.
- jononor 10y agoFor someone coming from C++, it will feel similar to std::string("foo") + "bar", which one gets used to pretty quickly.
- eduren 10y ago>palpatable =palpable + palatable. Pedantry aside though, I like the word. Maybe it describes something can be palpably palatable.
- steveklabnik 10y agoHa! Nice catch. Good thing I'm not like, a person who's paid to write words or anything...
- chrismorgan 10y agoYou wrote a word! A new word!
- MR_HANDS 10y agoor, palpatable could mean 'able to be palpated', which would mean able to be touched carefully with the intent of inspection, especially in medicine.
- mbrubeck 10y agoIt is possible that adding two str slices like ("a" + "b") could be implemented in the future to produce a new owned String, but there is a technical obstacle to doing this in the near term: https://www.reddit.com/r/rust/comments/67rjii/question_about_adding_strs_to_string_concatenate/ https://www.reddit.com/r/rust/comments/67rjii/question_about... The basic problem is that str slices exist in the "core" library, which can be used on targets without an allocator (for example bare-metal embedded systems), while the owned String type exists only in the "std" library, which requires an allocator. Rust has rules that prevent one library from stomping on another library's types in certain ways that could conflict with future API additions, and those apply to the standard libraries here. (One solution would be to allow the author of libraries A and B to declare that A is allowed to treat types from B as though they were its own.)
- iopq 10y agoIt's simple, just let string literals possibly refer to Strings if type inferences deems it necessary. Failing that, it should consider them 'static &strs. Kind of what happens for numerical literals right now.
- epage 10y agoThis is actually being discussed on /r/rust, see https://www.reddit.com/r/rust/comments/67rjii/question_about_adding_strs_to_string_concatenate/?st=j20qiumc&sh=4f6fe872 https://www.reddit.com/r/rust/comments/67rjii/question_about... Turns out my assumption (like C++, make expensive things more verbose) isn't the complete picture. The big blocker is a technical issue preventing `"" + ""` See https://github.com/rust-lang/rust/issues/39018#issuecomment-273276843 https://github.com/rust-lang/rust/issues/39018#issuecomment-...
- kibwen 10y agoHard to believe that this is the 17th minor release since Rust 1.0. :) Lots of good ergonomic improvements in this one, but for those of you who are itching for incremental compilation, a beta is now available for testing using the CARGO_INCREMENTAL environment variable: https://internals.rust-lang.org/t/incremental-compilation-beta/4721 https://internals.rust-lang.org/t/incremental-compilation-be...
- TylerE 10y agoWhy is it so hard to believe? Don't they release a new version every month like clockwork?
- steveklabnik 10y agoSix weeks, but your parent has been involved with Rust for a very long time, possibly longer pre-1.0 than post-1.0. For those of us in that category, it can feel like time flies! While 1.0 was almost two years ago, it feels like yesterday sometimes.
- AsyncAwait 10y agoI guess it's just because it took a long time to reach the 1.0 milestone, so 1.17 "feels" hard to believe.
- kibwen 10y agoIt's every six weeks rather than every month, and the fact that it's like clockwork is what's so impressive! I've actually implored the Rust devs before to slow the release cycle for their own sake, it seems to be quite brutal. They must be masochists, because at least some of them have told me they enjoy it. :)
- pdpi 10y agoThere's a lot of "not finished so wasn't included in this release" in the release notes. With, say, a 12 week cycle instead of 6 weeks, I'm waiting 3 months for My feature to land in the next release, and I feel a lot more pressure to get it done now than I would with a 6-week cycle where missing a release isn't a big deal. Assuming the team has the release process we'll automated and cutting a release doesn't add a lot of work, then there's not much to lose with this cycle.
- 0xFFC 10y agoI wish they include language service in the stable branch soon.
- StavrosK 10y agoYou mean the autocompletion server?
- 0xFFC 10y agoI think they call it officially language service.
- steveklabnik 10y agoYes. It will happen; it was ready to land on nightly just before the branch, but was held off so that we could have the full, usual, "spends six weeks in nightly, then beta, then stable" situation rather than almost immediately jumping into beta. This means it'll hit stable in two releases, 1.19, as long as nothing goes wrong.
- kibwen 10y agoAnd note that if you're using rustup to manage your toolchain, you can now use it to install and manage RLS for you as well: http://www.jonathanturner.org/2017/04/rls-now-in-nightly.md.html http://www.jonathanturner.org/2017/04/rls-now-in-nightly.md....
- merb 10y agorustup is really great. First I was a little bit curious if the updates every 6 weeks would ruin my dev environment since I need to update every time. but rustup makes that a breeze. `rustup update`. that's what to do when there is a new version, nothing more I don't know that any other compilers/interpreter that has the same ease of use. except maybe sbt, but that can't self update. (P.S.: Awesome talk about rust on scaladays: https://www.youtube.com/watch?v=C6dNx9pY7p8&index=2&list=PLLMLOC3WM2r5Ei2mnSHCD-ZD04AXovttL https://www.youtube.com/watch?v=C6dNx9pY7p8&index=2&list=PLL...)
- pvg 10y ago"If you want to understand Rust replace all occurrences of 'Rust' with 'The Thing'"
- sqeaky 10y agoWas your joke any deeper than a superficial reading of it? I also can't tell if it was lighthearted or deeply malicious.
- pvg 10y agoUnreserved malice and nigh-fathomless depth. I'd say more but I'm in the midst of designing a language which can precisely and safely express both concepts.
- sqeaky 10y agoA shame everyone is downvoting this comment. But you are honest then your first comment deserves as many downvotes as possible. Since you are in charge of this new language I won't be using it. The Rust community has largely figured out how to get work done with minimal malice and a language that actually exists.
- pvg 10y agoIn my language, it will be possible to express an impossible number of downvotes, correcting what is perhaps the most glaring design flaw of Arc.
- dang 10y agoWe detached this subthread from https://news.ycombinator.com/item?id=14213364 https://news.ycombinator.com/item?id=14213364 and marked it off-topic.
- daliwali 10y agoI was interested in learning Rust as a safer alternative to C a few years back, and have been observing its progress from afar. I'm much less optimistic about it now, though I only have a layman's understanding of it. The language seems to be more cumbersome to write than many other languages due to its design choices. It may be safer, but not as "ergonomic" subjectively, which ultimately affects productivity. It's hard to think of a good reason to use it, other than the safety features.
- sqeaky 10y agoMost of the changes in this log, and past change logs I have read agree with you. They mention it often and much of the changes work on that. That have chosen safety first and appear to be working on ergonomics without sacrificing that.
- mathw 10y agoErgonomic improvements are a major focus for the Rust team's 2017 roadmap, both in language changes and tooling. But they'll never sacrifice the core safety principles of the language to do it.
- pcwalton 10y agoI'm far more productive in Rust than I am in C. The amount of time I spend writing for loop boilerplate alone is worth the price of admission. Pretty much all of the ergonomics improvements this year have been improving on things you can't do in C to begin with. Like automatic serialization (serde). Or const generics. Or error handling via ?. Or pretty much any reasonable string manipulation ever. There's a legitimate debate to be had with the ergonomics of C++ vs. Rust (though I think Rust wins there too). But not with C. C, as a language that makes you jump through hoops to even get a hash table, doesn't even come close.
- goalieca 10y agopeople write better c than c++ because they are less tempted to over-architect. We've all come across terrible meta-templated c++ code with abuse of OOP all in the name of "information hiding". The main problem against c is the puny standard library. It would be nice to have a few more built-ins. I think c++ without inheritance would be a very nice language.
- GoodbyeEarl 10y agoHey, folks! I have never used Rust but all the buzz around it made me curious, so I may ask you to forgive my ignorance on the matter. I have a few honest questions about it and I'm pretty sure these questions were answered before, so feel free to point me to this resources, I'd appreciate very much. First of all what's the point of Rust? OK, it's a systems language, but what does that even mean? You can write a system in any language right? Python is a scripting language and lots of people use it everyday to write systems. What problems is it trying to solve? Also how does it compare to Go? (I haven't used either one or another). It seems to me that both Go and Rust are mainly used by C/C++ folks that want a new shiny language that somehow resemble the syntax or structure or funcionality of the former and at the same time add some modern features (I might be totally wrong by presuming this). From the perspective of other languages I know a little bit such as Ruby, Python or JS its syntax seems really bloated to me with all those specials symbols. Do you feel more productive writing software in it than in let's say JS? Please keep in mind that I really don't know Rust (nor I've never tried a statically-typed language) and I don't mean to offend anyone. Thank you guys!
- SkyMarshal 10y agoThink of Rust as a better C - when you need low-level hardware control like C enables, Rust gives you that + guarantees about never having memory segfaults or concurrency errors that C can't. If your Rust program compiles, it won't break for those reasons (unless you intentionally enable and use unsafe code, but not by default). Think of Go as a better Python (scripting) or Java (application) language - simpler than Java, faster and with better concurrency than Python. Like Python and Java it's garbage-collected, like Java it's statically weakly typed so can't offer the level of guarantees that Rust can, but possibly faster to develop in as a result. Still too new and thus lacks the extensive libraries of either Java or Python, but growing fast.
- collyw 10y agoI managed to crash compiled Rust programs in my brief experience with it.
- fpgaminer 10y agoI still get excited every time there is a new Rust release :D > When using Cargo’s build scripts, you must set the location of the script in your Cargo.toml. However, the vast majority of people wrote build = "build.rs", using a build.rs file in the root of their project. This convention is now encoded into Cargo, and will be assumed if build.rs exists. This is what I love about Rust's development. Rather than the developers just using their own ideas about how the Rust ecosystem will use a system, they instead wait for the ecosystem to come up with its own conventions. Once those conventions are organically established, they fold them into the official Rust tools. It's the equivalent of finding product market fit. More times than not our ideas about what the market (in this case, Rust users) wants will be wrong. Better to just adapt to the market. That's enough praise for now, so ... how about something not so great about Rust? I started building a password manager in Rust, because I'm not satisfied with the current options for password managers. This will be my first Rust project with a GUI and ... so far it's been very painful. I've looked at GUI in Rust in the past but never tinkered because there were never any good libraries available. It's now Rust 1.17 and there are still no good options. For this project I landed on gtk-rs, but it's a nightmare of Rc's, RefCells, and borrow_mut just to get even the simplest UI working. I'm not sure yet if this is an issue endemic to gtk-rs, or if Rc's and RefCells are a necessary evil in Rust whenever GUIs are involved. It's a bit hard to reason about ownership in this case, where your callbacks can be called at any time, and if they modify the UI that may trigger another, now nested callback, which may trigger yet another callback, etc. So it requires a lot of brain power to think about every borrow and what the lifetime should be. Without thinking too much about it, I feel like things could be better. If Rust was driving the UI loop, and if the callbacks didn't have 'static lifetimes, then it might be possible shove the UI into an isolated struct and not have to deal with too many ownership and lifetime issues. But again, I haven't tried to think it through and it's irrelevant because there's no mature GUI library in Rust that would allow that. Of course, none of that reflects on Rust itself, nor on the Rust developers. This is a third-party issue; it boils down to a lack of good GUI libraries. That's hardly surprising given how new Rust is. For now, I'm blundering forward and just hacking together whatever mess of code works. The critical parts of the password manager are in a separate Rust library with unit tests, and the password database will have history/versioning on every entry. So I ultimately don't care if the UI code is ugly and hacked together. It's just more time consuming than it probably has to be.
- Animats 10y agoTried some programs that used to work: First program: src/main.rs:65:11: 65:19 error: no method named `join` found for type `collections::vec::Vec<&str>` in the current scope src/main.rs:65 sline.join("") // return string OK, what happened to ".join", and who had the great idea of taking it out? Second program: $ cargo build An unknown error occurred To learn more, run the command again with --verbose. OK, must rerun because of useless message. $ cargo --verbose build Invalid arguments. Usage: cargo <command> [<args>...] cargo [options] Note that the "Usage" message doesn't say where to put the options if there's a commmand. So we have to guess: $ cargo build --verbose Failed to parse registry's information for: chrono Caused by: the given version requirement is invalid The version requirement in the cargo.toml file under [dependencies] is chrono="*" which, according to this manual [1] is valid. I used to like Rust, but after years of stuff breaking with each new release, I don't use it for anything serious. [1] http://doc.crates.io/specifying-dependencies.html http://doc.crates.io/specifying-dependencies.html
- Manishearth 10y agoRust has not removed an API since 1.0. Your code is extremely old and predates stability. Yes, star dependencies were removed like a year ago, for semver issues. They went through multiple release cycles of warnings.
- Animats 10y agoRust has not removed an API since 1.0. Your code is extremely old and predates stability. That code worked through at least Rust 1.2. So, yes, Rust has removed an API since 1.0. That code was compiling in December 2016. Yes, star dependencies were removed like a year ago, for semver issues. They went through multiple release cycles of warnings. From the Cargo manual [1]: Wildcard requirements Wildcard requirements allow for any version where the wildcard is positioned. *, 1.* and 1.2.* are examples of wildcard requirements. * := >=0.0.0 1.* := >=1.0.0 <2.0.0 1.2.* := >=1.2.0 <1.3.0 Apparently, nobody bothered to update the manual after removing the feature. [1] http://doc.crates.io/specifying-dependencies.html http://doc.crates.io/specifying-dependencies.html
- XorNot 10y agoWhat's the Rust IDE story looking like these days? For a language like Rust (or almost any language really) the best way I've generally found to learn them is to get a good auto-completing IDE which solves for "what's the standard library and what can I do?" and let's me focus on getting the syntax and design methodology right. Till now Rust hasn't had that. Is there a decent stack?
- maximilian 10y agoVSCode, Atom, Vim, Emacs, and Sublime all have very decent integration of automatic syntax checking, autocomplete (`racer`), and even auto-formatting (`rustfmt`).
- KenanSulayman 10y agohttps://areweideyet.com https://areweideyet.com
- deleted 10y ago[deleted]
- allengeorge 10y agoIntelliJ with the Rust plugin. Pretty awesome, and always improving. The maintainers are great, and welcome contributions.
- cadu2 10y agoThis thread convinced me that my next language to learn will be Rust (coming from a C/C++/Python career path). Can somebody point out a list of resources to start on Rust? Do we already have a good IDE with proper autocomplete support? Thank you!
- burntsushi 10y agoThe canonical source to learn is The Book: https://doc.rust-lang.org/book/ https://doc.rust-lang.org/book/ However, a second edition of the book is in the works and a lot of people have said good things about it. I think enough of it is filled out to be a good starting point: https://doc.rust-lang.org/nightly/book/second-edition/ https://doc.rust-lang.org/nightly/book/second-edition/
- cadu2 10y agoThanks! I'll burn my sushi to this!