News for hackers. Stuff that matters.
Re:Rust vs C Pitfalls by gravypod on Tuesday December 27, 2016 @05:00PM (#13266573)
If you're fighting, you've lost. The way to convert everyone to Rust you need to be better the the competition. Not just better as in "look at my features that will make your code safer". People may see the value but think "I get on just fine without the borrow checker so it isn't too important". You need to be far better then the replacement by providing the following:

   * Great Tooling ( IDEs )
   * Great Libraries ( Everything and a kitchen sink )
   * Better Documentation
Anything that can be done easily in C or C++ will need to be easier in Rust for everyone to move. No amount of language features will pull people who are doing well at their job, currently building everything they need to, and who maintain low level systems. You have to be able to entierly replace the old systems in a completely feature-complete way that's also easy to migrate to.

Blog posts wont pull me away from C, tooling and docs will.

Re:Rust vs C Pitfalls by Manishearth on Tuesday December 27, 2016 @11:32PM (#13268425)
> If you're fighting, you've lost.

I don't really get this attitude, it means that nothing new will ever overcome anything that's been established.

> Anything that can be done easily in C or C++ will need to be easier in Rust for everyone to move

You don't even need folks to move from C. Rust has had lots of success when it comes to folks completely new to systems programming learning it through Rust. Predominantly python/ruby/whatever shops are using Rust because they need a fast language, but don't want to deal with safety issues.

> Blog posts wont pull me away from C, tooling and docs will.

Then you are not the right audience for this blog post :) I find that such blog posts are extremely helpful in convincing people who have a choice between starting to use C and starting to use Rust, not folks who are already invested in C or C++. But I have seen such overviews to have impact on invested C/C++ programmers too; everyone is different!

Re:Rust vs C Pitfalls by tines on Tuesday December 27, 2016 @11:46PM (#13268456)
>> If you're fighting, you've lost.

> I don't really get this attitude, it means that nothing new will ever overcome anything that's been established.

I think he's trying to quote Dan Saks' "extern c: Talking to C Programmers about C++" when he said "If you're arguing, you've lost." Saks, at least, meant that if you're striving with someone, you've already lost because your partner is already in a "frame" of mind set against your arguments that is unbreakably strong, even by logic. The talk is really good just for that aspect.

Re:Rust vs C Pitfalls by Manishearth on Wednesday December 28, 2016 @12:12AM (#13268568)
Ah, I see. Yeah. I read that post more as a demonstration than a "fighting".

But that's a valid point.

Re:Rust vs C Pitfalls by gravypod on Wednesday December 28, 2016 @08:57AM (#13270453)
I would have linked the video had I remembered the talk but exactly. This is what I meant.
Re:Rust vs C Pitfalls by vatotemking on Wednesday December 28, 2016 @03:16AM (#13269193)
> You don't even need folks to move from C. Rust has had lots of success when it comes to folks completely new to systems programming learning it through Rust. Predominantly python/ruby/whatever shops are using Rust because they need a fast language, but don't want to deal with safety issues.

This resonates well with me. I came from PHP and I decided to learn C in order to contribute to an existing PHP extension. I was doing well following a C tutorial. Until I arrived at string manipulation at which point I said, f* this sh.

So looked for an alternative systems programming language to learn, and found Rust. And its a pleasant language to work on. Its a low level language that feels like a high level language.

My only fear is that, if Rust becomes a popular language, the community will end up as toxic as the JS and PHP community. Right now, the Rust community is filled with the smartest and friendliest devs I know of among open source communities, and I kinda like it to stay that way.

Re:Rust vs C Pitfalls by hueving on Wednesday December 28, 2016 @03:46AM (#13269323)
>Right now, the Rust community is filled with the smartest and friendliest devs I know of among open source communities, and I kinda like it to stay that way.

I have yet to find a community that has gotten popular and hasn't turned into a sewage plant. I think it's just a reflection on what the general population is like... :(

Re:Rust vs C Pitfalls by sjellis on Wednesday December 28, 2016 @05:53AM (#13269755)
The Rust community is as great as it is because of very deliberate decisions and investment in engineering structures to maintain a positive community at scale, so I'm pretty optimistic that it will stay really good. Arguably this work is as innovative as the language itself.

Emily Dunham of Mozilla has a talk about this Community Automation: https://www.youtube.com/watch?v=dIageYT0Vgg

Re:Rust vs C Pitfalls by hueving on Wednesday December 28, 2016 @04:14PM (#13273709)
The rust community is very small so it's easy to 'engineer it' at this phase. Once it gets so large that you have an eternal September it's effectively a complex system that will be very difficult to reason about with any changes resulting in lots of gnarly side effects.

Name a large programming community that is pleasant to interact with for newcomers and insiders alike.

Re:Rust vs C Pitfalls by sjellis on Friday December 30, 2016 @05:36AM (#13283704)
One of the distinctive things about Rust community management is how much is literally engineered, and done with software and processes so that it can scale. All of the touch points are designed: the rustup tool manages the installation experience, Cargo the build process and 3rd party dependencies, the RFC process structures online discussion, the This Week in Rust newsletter provides a heartbeat for the community, bots like Bors handle routine interactions, all of the entry points for interacting with other community members display the Code of Conduct, and so on.

It's a level of purposeful design that only the .NET and Go ecosystems approach, and because it's unusual and largely unobtrusive, it's not obvious that this really good, highly productive community is not at all accidental.

I would say that most large programming communities split into multiple communities, so different kinds of folks don't interact as much: the people on the core developer mailing lists are mostly not the same people as on IRC, or on user mailing lists, or working on issues for 3rd party frameworks in GitHub. I can't think of a sub-community that I've had experience with that I would characterize as unpleasant.

Re:Rust vs C Pitfalls by hordeallergy on Wednesday December 28, 2016 @04:44AM (#13269528)
"My only fear is..." - this is why I'm in the process of moving to Rust after 3 years of Go. I've come to the conclusion that a low barrier to entry leads to a bad state of affairs. Rust has a learning curve that I hope will help it avoid devolving into chaos.
Re:Rust vs C Pitfalls by adrianN on Wednesday December 28, 2016 @02:14AM (#13268976)
I'm currently trying to learn Rust, but if my motivation were "I need a fast language and don't want to deal with safety issues", I'd use Java. It's plenty fast (especially compared to something like Ruby!), the ecosystem is huge and tooling is extremely mature.
Re:Rust vs C Pitfalls by masklinn on Wednesday December 28, 2016 @05:11AM (#13269617)
> if my motivation were "I need a fast language and don't want to deal with safety issues", I'd use Java. It's plenty fast

Context is pretty important to the original comment:

> python/ruby/whatever shops are using Rust because they need a fast language, but don't want to deal with safety issues.

they're not looking for a fast language in isolation, they're looking for a fast language in the broader context of their existing technological stacks. Java doesn't help that much when you're looking into ways to speed up your Ruby application, at least not easily.

Furthermore, Java has the issues that it's only fast when memory is plentiful and it has a long startup/running time, it's "eventually fast" if you will.

Re:Rust vs C Pitfalls by the8472 on Wednesday December 28, 2016 @09:24AM (#13270628)
Java's FFI story is not as good as rust's though. So if you're interacting with the OS / native libs a lot then it might not be a good choice.

Although there's JNR and JNA and it might become a standard feature at some point in the future[0]. But for a good FFI experience you also need arrays-of-structs, which means you need value types in java. So it's a long journey to get there.

[0] http://openjdk.java.net/jeps/191

Re:Rust vs C Pitfalls by hueving on Wednesday December 28, 2016 @03:50AM (#13269334)
>It's plenty fast

Unless you need guarantees that can't be offered by garbage collection.

Re:Rust vs C Pitfalls by adrianN on Wednesday December 28, 2016 @05:07AM (#13269601)
Java is pretty popular for HFT applications. I think most programs are less latency sensitive than that. But of course, Java is not for you if you have hard real time constraints.
Re:Rust vs C Pitfalls by the8472 on Wednesday December 28, 2016 @09:14AM (#13270553)
There's a commercial pauseless GC offering for java. It's pretty much aimed at HFT applications.

https://www.azul.com/resources/success-stories/

Re:Rust vs C Pitfalls by fpoling on Wednesday December 28, 2016 @06:48AM (#13269929)
In some cases those HFT installations are tuned not to run full Java GC within 8 hours during the trading day. Then they are rebooted.
Re:Rust vs C Pitfalls by njs12345 on Wednesday December 28, 2016 @08:25AM (#13270301)
That sounds interesting - have you got any more details on exactly how they set this up?
Re:Rust vs C Pitfalls by gravypod on Wednesday December 28, 2016 @09:00AM (#13270469)
You can also use the Java Unsafe class to squeeze performance (every last bit) by recycling objects. I'm sad to say that I've had to do that at least once to make some vector math work.

Edit: Check out https://dzone.com/articles/understanding-sunmiscunsafe

Re:Rust vs C Pitfalls by fpoling on Wednesday December 28, 2016 @10:32AM (#13271094)
A person on a conference has not mentioned details. The reason for Java is productivity and access to a bigger pool of developers.
Re:Rust vs C Pitfalls by notsoeasy on Wednesday December 28, 2016 @07:09AM (#13270003)
> I think most programs are less latency sensitive than that.

Hardly, compared to anything that takes user input and aims at 60/120 FPS (where you inevitably end up bypassing GC if you can't force it to run when you want to).

https://www.quora.com/Is-Java-overtaking-C++-in-the-HFT-worl...

> One thing about HFT is that the speed of the language oddly doesn't matter much. You are talking about latencies of 20 milliseconds, and any language can do 20 milliseconds since CPU times are measured in nano-seconds rather than milisecond.

and

> If you are playing in the sub 10 microsecond range doing everything in C++ helps a lot. [..] A lot of high frequency strategies run in the 20-30 microsecond time frame. I think this is where a lot of diversity in terms of setup occurs.

and

> To lower execution costs, we need a low latency system that has good determinism in response times. You cant pause for hash table rebucketing or garbage collectors or the OS taking away your time slice or moving you to a diferent processor or whatever. There are guys who try this in Java, and spend most of their time trying to make Java work deterministically. Good luck

and

> If you are in ultra HFT then C++ is the only answer (besides custom hardware - FPGA & ASIC). You just can't afford garbage collector.

Re:Rust vs C Pitfalls by EugeneOZ on Wednesday December 28, 2016 @02:56AM (#13269112)
Java has not so great reputation about safety.
Re:Rust vs C Pitfalls by cesarb on Wednesday December 28, 2016 @05:13AM (#13269625)
Java's bad safety reputation comes mostly from the Java plugin, where many inventive ways to bypass its sandbox have been discovered. When used as a normal programming language, without depending on it for sandboxing (that is, when all code running in the JVM is trusted), its safety is quite good.

(Java also has a not so great reputation about speed. That one also comes from the Java plugin, which often took minutes to start up, while keeping the Netscape browser UI locked up.)

Re:Rust vs C Pitfalls by the_why_of_y on Wednesday December 28, 2016 @07:28AM (#13270070)
... and just to add to that, if you look at the other popular sandboxed client-side runtimes, i.e., Adobe Flash and various JavaScript-enabled browsers (all of them implemented in C++), you will find a similar number of critical vulnerabilities.
Re:Rust vs C Pitfalls by EugeneOZ on Wednesday December 28, 2016 @08:43AM (#13270368)
I see a lot of NPE in IntelliJ IDEA, especially EAPs. It's not browser's plugin.
Re:Rust vs C Pitfalls by the8472 on Wednesday December 28, 2016 @09:11AM (#13270529)
NPEs are not a security issue in the same sense as a buffer overflow or null pointer dereference.
Re:Rust vs C Pitfalls by enqk on Wednesday December 28, 2016 @03:15AM (#13269191)
That's interesting because it is based on the same principles of not letting developers do harmful thing as a way to guarantee security.
Re:Rust vs C Pitfalls by EugeneOZ on Wednesday December 28, 2016 @03:55AM (#13269345)
Rust uses RAII for memory-safety, not just language constructions to raise some limitations.
Re:Rust vs C Pitfalls by copperx on Wednesday December 28, 2016 @03:11AM (#13269167)
Please expound on that. Garbage collection, no pointer arithmetic, and array bounds checking seems like good safety features.
Re:Rust vs C Pitfalls by lmm on Wednesday December 28, 2016 @10:10AM (#13270930)
No sum types (the Java alternative to Rust enums is the horribly verbose visitor pattern, so a lot of the time people don't bother). null. A verbose and unabstractable "checked exception" system (that the standard library uses poorly in places) that leaves many users resorting to unchecked exceptions (panics) instead. Uninitialized field problems because methods are virtual even when called from superclass constructors. Excessive overhead to custom types that leads to using weakly typed Strings etc. rather than domain types. Poor abstraction facilities that lead to a culture of "magic" annotations and runtime reflection (or worse, proxies and bytecode manipulation) e.g. @Transactional, @Inject.

Java is safer than C, but it's a long way behind a modern functional language like Rust.

Re:Rust vs C Pitfalls by duneroadrunner on Wednesday December 28, 2016 @03:44AM (#13269314)
Re:Rust vs C Pitfalls by chungy on Wednesday December 28, 2016 @04:42AM (#13269519)
Security exploits say nothing about the language's memory safety design. Java still qualifies as a safe language.
Re:Rust vs C Pitfalls by EugeneOZ on Wednesday December 28, 2016 @09:04AM (#13270491)
As a safe language from race conditions or memory leaks?
Re:Rust vs C Pitfalls by adrianN on Wednesday December 28, 2016 @05:11AM (#13269614)
The Java VM is written in C though.
Re:Rust vs C Pitfalls by rat87 on Wednesday December 28, 2016 @05:53AM (#13269750)
There are many java vms

The most popular(hotspot/openjdk) is written in c++/assembly

Re:Rust vs C Pitfalls by EugeneOZ on Wednesday December 28, 2016 @03:57AM (#13269361)
GC is not about safety at all, sorry. I'm not going to start flamewar, but just to answer your question: https://www.google.com/#newwindow=1&q=null+pointer+exception
Re:Rust vs C Pitfalls by gravypod on Wednesday December 28, 2016 @09:03AM (#13270478)
You can't say NPEs are an example that Java is unsafe. The propegation is Nulls isn't Java's fault and Java's evolving currently to make use of Optionals as we speak to attempt to avoid this.
Re:Rust vs C Pitfalls by EugeneOZ on Wednesday December 28, 2016 @09:05AM (#13270493)
I can say it.
Re:Rust vs C Pitfalls by throwitfar on Wednesday December 28, 2016 @05:52AM (#13269745)
Do people choose languages based on blog posts? I've never done that, usually it was a combination of literature and mailing lists that convinced me. I also ignore conferences entirely.
Re:Rust vs C Pitfalls by jedahan on Wednesday December 28, 2016 @10:27PM (#13275455)
I would love examples of writing about rust that didn't mention or assume any C/C++ knowledge before. So much of what I've run into assumes that knowledge.

I'd love to read tutorials or books that assume Rust is someone's first programming language, period. What would that look like?

Re:Rust vs C Pitfalls by steveklabnik on Wednesday December 28, 2016 @11:08PM (#13275575)
The book doesn't assume any C/C++ knowledge.

There isn't any good "first programming language" material using Rust yet, IMHO.

Re:Rust vs C Pitfalls by all2well on Tuesday December 27, 2016 @10:02PM (#13268112)
I mean by that logic, no progress is possible at all in programming languages, because at some point every language had worse tooling than the competition.

I'd say the greatest part about Rust is the community, which realizes all of the issues you've listed, and especially the really tough learning curve for beginners.

Generally, use the right tool for the job. I don't think I'd really want to use Rust in production yet, but it's really great for side-projects and other more experimental things.

Re:Rust vs C Pitfalls by bluejekyll on Wednesday December 28, 2016 @12:02AM (#13268521)
> I don't think I'd really want to use Rust in production yet...

Why not out of curiosity? I've already been showing off to people at work how much shorter Rust code is than our corresponding Java, how much easier it is to build and deploy, and how much more stable it is.

Granted I've been using it at small micro service scale at the moment, but I see no reason not to go into production at the moment (well, except for the fact that I'm the only one that will be called at night if it fails... luckily it hasn't, and I'm not worried).

Re:Rust vs C Pitfalls by htaunay on Tuesday December 27, 2016 @05:07PM (#13266618)
Well, I can't speak for tooling or productivity comparison to C/C++, but Rust has one of the best programming language documentations I have ever had the pleasure of reading [1].

[1] https://doc.rust-lang.org/stable/book/

Re:Rust vs C Pitfalls by steveklabnik on Tuesday December 27, 2016 @06:08PM (#13267063)
Thank you! Carol and I are working on the second edition, you can read what we have so far here: http://rust-lang.github.io/book/

(I think it's even better, but I'm biased)

Re:Rust vs C Pitfalls by devty on Wednesday December 28, 2016 @02:04AM (#13268940)
I'm enjoying the new write-up on ownership/references/borrowing! It reads more like an fascinating reading on PL concept than a language-specific manual. Great work!
Re:Rust vs C Pitfalls by steveklabnik on Wednesday December 28, 2016 @12:14PM (#13271906)
Thank you!
Re:Rust vs C Pitfalls by sli on Tuesday December 27, 2016 @09:15PM (#13267934)
Count me excited. I've always liked the book, but I've also found it fairly lacking in ways I'm not sure I completely understand, I can likely lend part of that to my relative inexperience with Rust and systems programming in general.

One I can note is that I really like the new sections. I've never quite liked that the current book is just a bunch of chapters all in a row. Not that that's necessarily bad in and of itself, but I do think the new way is better.

Re:Rust vs C Pitfalls by steveklabnik on Tuesday December 27, 2016 @09:17PM (#13267944)
Thanks! If you do check it out, don't hesitate to file issues.
Re:Rust vs C Pitfalls by copine on Tuesday December 27, 2016 @09:33PM (#13267999)
How far along is the new documentation? Would it be better to read https://doc.rust-lang.org/book/ or the github.io page? Maybe both?
Re:Rust vs C Pitfalls by steveklabnik on Wednesday December 28, 2016 @12:18AM (#13268595)
Both would be ideal, if you have the time. The new book is far enough along that you can learn the most basic stuff from it, and the intermediate bits are coming along. The more advanced stuff is only an outline. So if you start with new, and then switch to old, you'll get the best of both.
Re:Rust vs C Pitfalls by lobster_johnson on Tuesday December 27, 2016 @11:40PM (#13268446)
I believe I reported this once, but it's still there: You're hijacking alt-cmd-left/right, which I use for switching tabs in Safari. Your arrow-key event handler needs to have a guard:

    !(event.ctrlKey || event.shiftKey ||
      event.altKey || event.metaKey)
(Thanks for writing great documentation, though.)
Re:Rust vs C Pitfalls by steveklabnik on Wednesday December 28, 2016 @12:17AM (#13268588)
Yeah I'm not sure mdbook has patched it yet. I'll make sure a ticket is filed tomorrow, I thought we did, but am not sure. Even if we did, well, that's open source.
Re:Rust vs C Pitfalls by mmstick on Tuesday December 27, 2016 @05:16PM (#13266687)
Rust has by far the best tooling and documentation support out of all languages I've seen. Library support is great too, at least in that Cargo is an amazing platform and it's too easy to import C libraries.

What do you feel is actually missing from Rust? Nothing has stopped me from replacing C entirely on the low end, and even the high end for application software development.

Re:Rust vs C Pitfalls by the8472 on Wednesday December 28, 2016 @11:20AM (#13271484)
> Rust has by far the best tooling and documentation support out of all languages I've seen.

IDE support is easily better for java. IDEs have total type knowledge since they directly integrate with the compiler, including in long chains of type-inferred code, usually even while you're in the middle of typing and your code is in a syntactically invalid intermediate state. They also automatically manage imports where needed.

Similarly, IDE-integrated debuggers can do a edit-code, recompile, hot patch into running application while you're stepping through the code.

CPU Profilers seem about en par, but memory profilers in java are again better because they offer a combination of taking heap dumps, allocation recording and diffing dumps at the same time. While also offering good GUIs for exploring object graphs.

Re:Rust vs C Pitfalls by santaclaus on Tuesday December 27, 2016 @05:22PM (#13266714)
> What do you feel is actually missing from Rust?

A solid NumPy-like library, for one.

Re:Rust vs C Pitfalls by gravypod on Tuesday December 27, 2016 @05:44PM (#13266887)
Like you I need something like NumPy and SciPy for my work. I'd also like an IDE. An IDE goes a long way to helping me feel comfortable to use a language.
Re:Rust vs C Pitfalls by mmstick on Tuesday December 27, 2016 @06:06PM (#13267039)
Rust's had great IDE support for more than a year now.

- There's Atom with Tokamak, which you must also install racer and clippy via cargo to get rapid in-line linting.

- Then there's Visual Studio Code with RustyCode which you can also integrate with racer and clippy, that provides faster code completion and hovering over items will show a tooltip that documents that items.

- Some people like IntelliJ Rust, but I've not tried it myself.

As for installing racer and clippy, it's been made a lot easier recently via rustup:

rustup component add rust-src

rustup component add rust-docs

rustup toolchain install nightly

rustup run nightly cargo install clippy

rustup run nightly cargo install racer

Now you're good to go.

As for NumPy, there's official crates like the `num` family, which provides a plethora of useful numerics capabilities. It's not my forte though so others would know more about the best numerics crates outside of the `num` family. If you know what functionality you are looking for, it may already be created and is searchable at Crates.io.

Re:Rust vs C Pitfalls by Manishearth on Tuesday December 27, 2016 @10:56PM (#13268323)
I don't really consider this to be "great IDE support", I consider this to be a start at IDE support. I love Rust, and I'm productive even without racer or YCM -- but I think there's a large gap between what Rust has and what many IDEs get you.

Most Java/C# IDEs will get you:

- Type-based autocomplete (racer/YCM/etc have this for Rust)

- Jump-to-definition / documentation (YCM/etc has this)

- Autoimport

- Non-grep-based refactoring

- Auto-boilerplating: for example, type `impl Trait for MyType` and have it tab-complete to a skeleton trait impl

- Some error integration (not just fileline jump-to), with tooltips and stuff asking you how you want the IDE to auto-fix the error

- A bunch of other smaller useful things which I can't remember at the moment

Now, autocomplete is a major chunk of this, but nowhere near being all of it.

The way I see it there are two camps on this issue. There's the "text editor" camp who use vim/emacs/sublime and when they are asking for IDE support they just mean autocomplete and jump-to that works from their editor. Then there's the camp coming from Eclipse or VS who want the whole deal.

The Rust Language Server project (https://github.com/jonathandturner/rls) is working on exposing this info from the compiler in a structured form so that IDEs and "text editor"s alike can get IDE features.

Re:Rust vs C Pitfalls by EugeneOZ on Wednesday December 28, 2016 @03:39AM (#13269291)
IntelliJ IDEA Rust plugin gives all of that, although sometimes types detection is not smart enough.
Re:Rust vs C Pitfalls by estefan on Wednesday December 28, 2016 @03:53AM (#13269344)
I've been using this plugin but it doesn't seem to do auto-import. Does it definitely support that? Apart from that, as you say, type detection isn't always particularly useful. Apart from those two features, it's pretty much there, definitely usable.
Re:Rust vs C Pitfalls by Animats on Wednesday December 28, 2016 @12:59AM (#13268740)
Agree that Rust lacks something comparable to NumPy for numeric work.

Rust does have lots of numeric crates. Too many. I took a look at matrix multiply functions recently.(See [1], below "Here is what the Rust compiler actually does", for some notes on the effectiveness of Rust's subscript checking optimization.) "algebloat" wouldn't compile on stable. "matrixmultiply" is all unsafe code, with C-type raw pointers. "ndarray" has unsafe indexing. "scirust" has more raw pointer manipulation. "matrices" is an empty project.

In the "num" crate, there are bigints, along with rationals and complex numbers, but no matrices. "numeric" has matrices, but it's just a wrapper for some common C libraries. (Also, calling "panic" for a singular matrix in SVD is kind of drastic.[2])

Each of these matrix crates has its own representation for multidimensional arrays. You can't mix them easily.

Matrices in Rust really need some standardization.

[1] https://news.ycombinator.com/item?id=13230551 [2] https://github.com/numeric-rust/numeric/blob/master/src/lina...

Re:Rust vs C Pitfalls by Manishearth on Wednesday December 28, 2016 @01:34AM (#13268854)
> "ndarray" has unsafe indexing

We've had this discussion before. ndarray exposes both a safe and unsafe API for indexing, just like the stdlib vector (or core arrays). That is not problematic.

I agree that the lack of standardization is a problem, though. I think that mostly folks use ndarray or nalgebra (and num if they need bigints). Anyone can upload a crate, the question is if the crate is the main one used by the community. There are some efforts to make it easier to choose crates amongst alternatives on crates.io, however.

I suspect most of the issue here is just that no group (only individuals) is using Rust for scientific computing yet, so there's no large driving effort behind getting good libraries here. When I needed this in Rust I just picked the library that I thought would work best with very little thought, with no thoughts on writing my own or improving the lib because I didn't have time. When larger groups work on things generally attention gets paid to stuff like this and better solutions come out on top. Rust isn't quite there yet in adoption for this to happen, maybe soon :)

Re:Rust vs C Pitfalls by Animats on Wednesday December 28, 2016 @02:26AM (#13269013)
Is it really necessary to expose an unsafe API that bypasses subscript checking? Another Rust advocate was arguing, the last time this came up, that LLVM could optimize out most of the subscript checks. As I showed, it optimizes out about half of them for multidimensional arrays implemented with an explicit multiply. That may improve, especially if there's some standard idiom for declaring multidimensional arrays and the compiler handles that idiom well.

For 1D, though, the optimization of checking is pretty good. "get_unchecked" for Vec may be obsolete. There's an amusing Stack Overflow question [1] from someone who complains that he changed an access from "[]" to "get_unchecked()" and his program didn't get faster.

Maybe it's time to deprecate some of the legacy "unsafe" stuff. Preferably before the first CERT advisory involving a buffer overflow in a Rust program.

[1] http://stackoverflow.com/questions/39196594/why-dont-i-get-p...

Re:Rust vs C Pitfalls by Manishearth on Wednesday December 28, 2016 @03:18AM (#13269204)
> Is it really necessary to expose an unsafe API that bypasses subscript checking?

Sometimes you don't want to rely on LLVM, and sometimes the size invariants aren't as easily available to LLVM.

I'm not even sure if this would be that necessary outside of the library itself; unchecked indexing would be useful to implement ops like matrix multiplication within the library, once, and probably never used after that.

The API exists mostly to mirror the stdlib one and its use cases. I don't think it's supposed to be the API you reach for usually. Just because the API exists doesn't mean it's the one you're supposed to reach for.

> That may improve, especially if there's some standard idiom for declaring multidimensional arrays and the compiler handles that idiom well.

I mean, multidimensional arrays do exist in the language. I'm not sure what you're getting at here.

These libraries provide support for treating multidimensional arrays as matrices, with the semantics of matrices (multiplication, addition, etc). You seem to be arguing for making this a first-class compiler feature? I'm not sure how that's very different from just implementing it with some unsafe code -- the bug-prone-ness of the unsafe code is about the same as the bug-prone-ness of a compiler optimization that replicates it. There is a case to be made for moving it into the stdlib itself to avoid the "10 ways to do it" issue, but Rust wants to keep the stdlib small so that's not going to happen.

This is the point of unsafe code. You use it to implement a few safe abstractions like matrix multiplication and iteration in libraries, verify that, and use them. There's a stigma attached to unsafe code; rightly so; because there be nasal demons. But we shouldn't take it to its extreme and conclude that all unsafe code is bad.

> "get_unchecked" for Vec may be obsolete.

Wouldn't count on it. A single datapoint doesn't really mean much here.

I mostly see get_unchecked being used in the context of other unsafe code. Rust's stdlib uses it for the internals of CString and for implementing iterators. It also uses it in an optimization for the rand crate; which does some trickyness with bitmask indexing that LLVM might optimize but I suspect the authors didn't want to rely on LLVM optimizing.

Servo doesn't use it at all, though. Going through my cargo cache dir the crates that use it are those which implement abstractions. Some, but not all, of the use cases there probably get optimized out anyway. The regex stuff also uses it but the invariants there are complicated enough that llvm won't optimize it (there are some decent comments listing out these invariants and explaining exactly how to use it safely, though).

Re:Rust vs C Pitfalls by mmstick on Wednesday December 28, 2016 @01:22AM (#13268814)
Did you not check out `nalgebra`?

https://docs.rs/nalgebra/0.10.1/nalgebra/

Re:Rust vs C Pitfalls by Animats on Wednesday December 28, 2016 @01:43AM (#13268884)
That's mostly a library for small vectors for 3D graphics and such. (I once did one of those myself, for C++.[1]) There's some support for 2D matrices in "nalgebra", but with heavy use of "unsafe".[2]

Every matrix package I've seen so far in Rust turns off subscript checking with unsafe code.

[1] http://graphics.stanford.edu/courses/cs148-10-summer/algebra... [2] https://docs.rs/crate/nalgebra/0.10.1/source/src/linalg/deco...

Re:Rust vs C Pitfalls by mmstick on Wednesday December 28, 2016 @02:02AM (#13268935)
Is there a problem with doing that with unsafe code? You have to remember that unsafe doesn't mean unsafe in Rust. If a developer is choosing to use the unsafe keyword, it's merely telling the compiler that they know what they are doing and what they are doing is safe. One shouldn't automatically infer that unsafe code is bad or negative. It doesn't deserve the negative stigmatism that it's given.
Re:Rust vs C Pitfalls by Animats on Wednesday December 28, 2016 @02:34AM (#13269039)
No, it means the developer thinks they know that they are doing. Often, they don't. Read some CERT advisories for buffer overflows. There are hundreds of them.

The whole point of Rust is to put an end to that.

Re:Rust vs C Pitfalls by gravypod on Tuesday December 27, 2016 @06:13PM (#13267103)
I've still not been able to get Rust to work with work in IntellJ or Eclipse. I've not tried Atom but that doesn't support "Projects" or "Solutions" in the way I'd like. From what I understand there is no "go" button.
Re:Rust vs C Pitfalls by steveklabnik on Tuesday December 27, 2016 @06:38PM (#13267263)
Have you tried https://intellij-rust.github.io/ and http://rustdt.github.io/ ? If they don't work, I'm sure their maintainers would appreciate bug reports.
Re:Rust vs C Pitfalls by tupshin on Tuesday December 27, 2016 @10:30PM (#13268237)
rustdt has been my daily rust environment for months. has some warts, but mostly just works
Re:Rust vs C Pitfalls by mmstick on Wednesday December 28, 2016 @01:29AM (#13268842)
Atom does support Projects. The left panel is where you add your projects, which is created via cargo and has integration within Atom. You can hide the projects panel by pressing Ctrl + '\'. There's an integrated terminal if you install the tokamak terminal. I'm not sure what you mean by a 'go' button though.
Re:Rust vs C Pitfalls by lobster_johnson on Tuesday December 27, 2016 @11:14PM (#13268378)
Does Rust have anything like "gorename" and "goimports"?
Re:Rust vs C Pitfalls by Manishearth on Wednesday December 28, 2016 @01:35AM (#13268858)
https://github.com/jonathandturner/rls (still WIP) has rename support I think. Unsure if imports are in the plan.
Re:Rust vs C Pitfalls by empath75 on Tuesday December 27, 2016 @05:56PM (#13266974)
Re:Rust vs C Pitfalls by gravypod on Tuesday December 27, 2016 @06:00PM (#13267000)
I don't care about the backend and how it's implemented. I don't write IDEs. I write software in IDEs. If the IDE is good, and by good I mean provide autocomplete and features on par with Eclipse for Java,and it needs to be fast and easy to use.

Fast and easy to use are UI tricks that can't be handled by a server.

Edit: I just realized that this may be read in a negative connotion and I didn't mean it like that. I just mean that I'm not the person who should be looking at those. I just know it doesn't exist yet and I'd like it to. Telling me of the Rust Server thing gives me no information as to how close the IDE is to being done.

Re:Rust vs C Pitfalls by mmstick on Wednesday December 28, 2016 @01:31AM (#13268846)
The only thing the RLS is going to provide is just faster completion for the most part. All the IDE's in existence right now are making use of racer for completion and clippy for linting quite well without it, especially Atom.
Re:Rust vs C Pitfalls by Manishearth on Wednesday December 28, 2016 @12:47PM (#13272156)
The point of that comment was to say "It's being worked on, actively"
Re:Rust vs C Pitfalls by mmstick on Tuesday December 27, 2016 @06:07PM (#13267057)
There does exist an entire family of official `num` crates. Chances are that what you are searching for is already done.
Re:Rust vs C Pitfalls by voidlogic on Tuesday December 27, 2016 @05:05PM (#13266604)
On similar note, why Rust over Go?

If I look at everything I used to write in C, I'd say 80% is well suited for Go and the rest I would fallthrough to Rust for. For the stuff where having a GC and slightly less control is OK, I don't see why I would want to use Rust. Rust is just much more complex and I prefer to keep it simple stupid (KISS).

Basically, Go is good for 90% of what I used to use Java for and 80% of what I used to use C for... trying to understand where it makes sense for Rust to fit in.

Re:Rust vs C Pitfalls by burntsushi on Tuesday December 27, 2016 @05:11PM (#13266647)
I don't use Rust just because I want to avoid GC. I also use it for algebraic data types, compile time elimination of data races, sophisticated polymorphism, a clear and simple module system, excellent tooling in the form of Cargo and an unrelenting focus on providing abstractions with as little overhead as possible.

(I've used Go and Rust daily for the past few years. I love them both.)

Re:Rust vs C Pitfalls by voidlogic on Tuesday December 27, 2016 @05:50PM (#13266928)
Good point. I agree there are other reasons to use Go, I what mean is that unless I have a project that gets a lot of bang for the buck out of those, the KISSness of Go wins out over those nice features and their correlated complexity. GC was just at the forefront of that list of features.
Re:Rust vs C Pitfalls by Ericson2314 on Tuesday December 27, 2016 @11:24PM (#13268406)
There's simple, and there's anti-intellectual. Go would be the latter.
Re:Rust vs C Pitfalls by oblio on Wednesday December 28, 2016 @03:58AM (#13269363)
Well, in many respects Basic, PHP, Javascript, Java all had various forms of "anti-intellectualism" baked. They lost some of them along the way.

Still, the fact that all those languages are in the top 10 of programming languages kind of says that people don't really care.

Re:Rust vs C Pitfalls by _callcc on Wednesday December 28, 2016 @04:36AM (#13269498)
Go is against the sort of programming that builds up conceptual dream worlds of gratuitous abstraction and needless complexity. When that's the only way you want to think, Go will indeed seem anti-intellectual.
Re:Rust vs C Pitfalls by tigershark on Wednesday December 28, 2016 @08:39AM (#13270350)
A modern programming language without any whatsoever support for generics from my point of view is just extremely bad, for someone else can very well be proof of anti-intellectualism.
Re:Rust vs C Pitfalls by santaclaus on Tuesday December 27, 2016 @05:21PM (#13266708)
> I don't use Rust just because I want to avoid GC.

Go that is?

Re:Rust vs C Pitfalls by burntsushi on Tuesday December 27, 2016 @05:28PM (#13266760)
Hmm, not sure I understand? Re-reading, perhaps my phrasing wasn't clear. What I meant was that I use Rust, and it's not simply because it lacks GC. There are lots of other good reasons too.

To be even clearer: I don't think Rust's value proposition depends on whether you absolutely must avoid GC or not.

Re:Rust vs C Pitfalls by empath75 on Tuesday December 27, 2016 @05:51PM (#13266932)
Rust has gc, no?
Re:Rust vs C Pitfalls by dignan on Tuesday December 27, 2016 @06:01PM (#13267006)
No, it does not. It does allow you to implement it if you want though: https://github.com/Manishearth/rust-gc
Re:Rust vs C Pitfalls by dcsommer on Tuesday December 27, 2016 @06:01PM (#13267004)
Not in the mandatory runtime overhead, cycle collection, or non-deterministic pausing senses of the word.
Re:Rust vs C Pitfalls by Ygg2 on Tuesday December 27, 2016 @10:56PM (#13268321)
It has optional RC type.

It had GC as core part of language, about six or more years ago?

Re:Rust vs C Pitfalls by lossolo on Tuesday December 27, 2016 @06:23PM (#13267172)
It has reference counting for some of the constructs which is a form of garbage collection and it has runtime overhead. Like shared_ptr in c++.
Re:Rust vs C Pitfalls by tomjakubowski on Wednesday December 28, 2016 @06:16AM (#13269832)
This is a misleading comment. A Rust program may use reference counting by explicitly wrapping some value in Rc<> or Arc<>; the standard library provides these generic types. There are no "constructs" built in to the language or standard library that require the use of reference counting.
Re:Rust vs C Pitfalls by lossolo on Wednesday December 28, 2016 @01:43PM (#13272574)
I wrote it's like shared_ptr which is also construct in C++ standard library, what is misleading here? You don't need to use it also in C++ and I did not wrote that you need to use it in Rust also, you disagree with something that I didn't wrote.

PS. Pozdrawiam rodaka.

Re:Rust vs C Pitfalls by 404-universe on Tuesday December 27, 2016 @06:01PM (#13267011)
Not really, no. You could implement one with e.g. reference counting, but one is not provided for you by default.
Re:Rust vs C Pitfalls by colejohnson66 on Tuesday December 27, 2016 @05:57PM (#13266982)
Depends on your Rust implementation. You can have an implementation without one and use it to make, say, an operating system.
Re:Rust vs C Pitfalls by steveklabnik on Tuesday December 27, 2016 @06:07PM (#13267054)
There is only one implementation of Rust, and it does not have tracing GC. The language does not include semantics for one, so it would be an extension of the language.
Re:Rust vs C Pitfalls by colejohnson66 on Wednesday December 28, 2016 @12:00AM (#13268511)
Right. I had that backwards. (I don't program Rust)
Re:Rust vs C Pitfalls by TheDong on Wednesday December 28, 2016 @12:49AM (#13268715)
> I don't program Rust

If you don't know anything about rust, you shouldn't respond to a question about rust.

Re:Rust vs C Pitfalls by jjnoakes on Tuesday December 27, 2016 @05:29PM (#13266779)
No. He is saying he uses rust to avoid GC, but not just to avoid GC.
Re:Rust vs C Pitfalls by plandis on Tuesday December 27, 2016 @05:58PM (#13266988)
For me this is pretty easy. It's about leadership.

The leaders of Go foster an attitude of exclusiveness (just like a month ago they wanted to get rid of the Go subreddit in favor of a solely Google owned option of Google groups).

The leaders of Rust are very receptive and helpful to new people. They are on IRC / Reddit and many other channels.

I'd much rather invest my time into a truly open language and to me, that is not Go.

Re:Rust vs C Pitfalls by atombender on Tuesday December 27, 2016 @10:11PM (#13268163)
> just like a month ago they wanted to get rid of the Go subreddit

To be fair, this was a proposal by a single person on the Go mailing list, and it was in reaction to Reddit's CEO publicly admitting to editing other users' comments. The person who proposed closing the Go subreddit was also under the mistaken impression that the subreddit was hosted by the Go team, which wasn't the case. In the end, there was a lot of discussion, and nothing was deleted. Tempest in a teapot, as usual.

Go has a serious culture problem, but that's not a good example of it.

Re:Rust vs C Pitfalls by euyyn on Tuesday December 27, 2016 @11:09PM (#13268365)
What's a good example of it?
Re:Rust vs C Pitfalls by lobster_johnson on Tuesday December 27, 2016 @11:32PM (#13268427)
I don't want to single out anyone or any specific projects, so this is going to be a generalization, but since you asked:

In my experience, there's a lot of hostility and arrogance. More than in any other community, I've seen Go developers shut down discussion by closing comment threads on Github; reject pull requests and refuse to debate the merits of the change; castigate people for "not following proper procedure"; and being dismissive or contemptuous instead of humble when they fail to understand a problem that is being discussed (on, say, the official Go Slack channel).

My very personal hypothesis is that this is a case of mirroring. The Go development team may be said to have a laconic, authoritarian style, which is, of course, their prerogative, and which befits their position; unfortunately, a lot of Go developers seem to be under the impression that they, too, can behave like demigods. In particular, Go developers, more than other cultures, seem to get an ego boost out of telling people "no".

But this is a generalization. There are lots of friendly Go devs around, to be sure (I hope I'm one of them). I'm particularly happy with the Kubernetes team. At the same time, I do think it's an issue and one that the community needs to be aware of.

Re:Rust vs C Pitfalls by neoasterisk on Wednesday December 28, 2016 @03:15AM (#13269192)
> the official Go Slack channel

This is very misleading. There is no official Go slack channel. The slack channel you are referring is just a slack channel and is by no means official.

> In particular, Go developers, more than other cultures, seem to get an ego boost out of telling people "no".

This is misleading as well. Go was designed from the start to exclude certain features e.g. inheritance and many others. There are usually very good reasons for those decisions that people who've been following Go from its creation know and understand very well.

Now what happens when someone who does not understand those design choices comes to Go? They want their favorite feature obviously! When Go members attempt to explain them why something like that does not exist in Go (and probably will never be included) it starts feeling like "no". And people do not take "no" well. They get emotional and fail to see reason. If they had bothered to explore the language or at least read the official documentation and FAQ, that state of mind might have been avoided.

Including everyone's favorite feature does not make a language better.

Go has created a community around a certain school of programming. Nobody claims that it is better than other schools but it is a fact that it exists. But who is the one that is close-minded in this case? The new person that doesn't bother with the teachings of the school or the school that dismisses the ideas of the new person? Who is really the one that says "no"?

From my experience, the Go developers always carefully consider every new idea that is brought to the table. But it is also a fact that after the 10th time you've seen the same idea, you are not going to sit down and spend time considering it. You are just gonna link a previous discussion and say "Sorry this has been brought up before, please check these". How does that feel to a new person? I bet it feels like "no" again.

Re:Rust vs C Pitfalls by hueving on Wednesday December 28, 2016 @03:58AM (#13269362)
>I'm particularly happy with the Kubernetes team.

I find it pretty terrible that Kubernetes develops on Slack/Github/Videochats. For being an Open Source project, it rejects open source tooling and makes it difficult for people with poor english skills to participate (text-based meetings are much better for inclusiveness). Even if you do listen in on the video meetings, they give you a feeling as an outsider that many of the decisions have already been made elsewhere (some obscure github issue conversation, a google doc comment on a 1 year old doc, etc). It seems the only way to meaningfully participate in the k8s community is to be a known insider... :(

Re:Rust vs C Pitfalls by pas on Wednesday December 28, 2016 @07:35AM (#13270095)
Kubernetes is opensource-by-Google. It's suprisingly open, and still alive, which is better than almost any of the other Google known projects. (Chrome and Android come to mind, but Android is write-only, and Chrome isn't that great at listening to users either. Not that Mozilla is better about Firefox stuff, but the Rust team is wonderful.)
Re:Rust vs C Pitfalls by stablemap on Tuesday December 27, 2016 @10:30PM (#13268238)
I only saw one Go team member speak in favor of deleting /r/golang, in order to dissociate the project from what he viewed as deplorable actions by reddit's CEO. I thought the proposal was rash but I don't believe the purpose was to herd people onto Google properties.
Re:Rust vs C Pitfalls by NoahTheDuke on Tuesday December 27, 2016 @09:52PM (#13268055)
> (just like a month ago they wanted to get rid of the Go subreddit in favor of a solely Google owned option of Google groups)

Wow, really? Do you have a link to that discussion? That's wild.

Re:Rust vs C Pitfalls by gmjosack on Tuesday December 27, 2016 @09:58PM (#13268090)
I believe this post was stickied at the top of the golang subreddit for a bit: https://www.reddit.com/r/golang/comments/5eubdp/the_future_o...

It should be a good summary of the event.

Re:Rust vs C Pitfalls by pornel on Tuesday December 27, 2016 @07:43PM (#13267569)
I write libraries.

In Go I can only write libraries for Go programs. In Rust I can write libraries for any program.

i.e. Rust can easily produce static and dynamic libraries that are linkable with C programs and any language with a C FFI. I can write Rust code that works for programmers using C, C++, C#, D, Go, Swift, Python, PHP, Java, etc.

Re:Rust vs C Pitfalls by thegeekpirate on Tuesday December 27, 2016 @09:39PM (#13268016)
Re:Rust vs C Pitfalls by shanemhansen on Tuesday December 27, 2016 @09:30PM (#13267990)
> In Go I can only write libraries for Go programs.

That's not true. It's quite simple to create a loadable shared object in go and call it using anything with a c ffi.

Re:Rust vs C Pitfalls by lobster_johnson on Tuesday December 27, 2016 @11:00PM (#13268338)
Calling C from Go has significant overhead [1], doesn't that mean calling Go from C is equally slow?

[1] https://www.cockroachlabs.com/blog/the-cost-and-complexity-o...

Re:Rust vs C Pitfalls by hueving on Wednesday December 28, 2016 @04:00AM (#13269370)
It may be even worse calling Go from C since you are bring the whole Go runtime with GC and all when you call into Go.
Re:Rust vs C Pitfalls by TheDong on Tuesday December 27, 2016 @10:59PM (#13268332)
It's not quite simple because of the GC go brings.

As proof, notice that it barely happens, and only as an oddity in Go, yet in rust there are actual uses (e.g. ruby and python library optimization)

Re:Rust vs C Pitfalls by Zardoz84 on Wednesday December 28, 2016 @05:51AM (#13269742)
Like Dlang
Re:Rust vs C Pitfalls by dispose13432 on Tuesday December 27, 2016 @05:33PM (#13266801)
>On similar note, why Rust over Go?

I was going to say that rust has performance advantages over Go (due to GC), but look at benchmarks:

http://benchmarksgame.alioth.debian.org/u64q/compare.php?lan...

Go wins some and looses some, but it's all in the ballpark (except Binary trees [1] which it loses even to Java(!)).

It's true that rust is a new language, but so is Go.

[1]: I assume it's because it's a test of GC, but Go loses to Java (which, like Go, is a GC language)

Re:Rust vs C Pitfalls by mmstick on Tuesday December 27, 2016 @05:56PM (#13266971)
This is only because the Rust implementations are using particularly slow code paths, either because SIMD/AVX optimizations requires a nightly compiler, some optimizations would require unsafe code, or that other languages are using particularly hacky code that would never fly in real world software.

For example, many of the Java/C/C++ benchmarks are using custom optimizations that should be illegal for the benchmarking. Case in point, some are featuring custom hash maps that feature hashing algorithms that, while fast, would never be useful as they provide no protection against collisions. You'll see a hashing algorithm in a C preprocessor, for example, that just fakes having an actual algorithm whereas Rust examples are sticking to the tried and tested production-grade algorithms shipping in the standard library.

Re:Rust vs C Pitfalls by lossolo on Tuesday December 27, 2016 @06:40PM (#13267282)
> Case in point, some are featuring custom hash maps that feature hashing algorithms that, while fast, would never be useful as they provide no protection against collisions.

They are useful in this case, aren't they? Custom hash map implementation can be also useful in other scenarios. This is feature of the language that allow you to do that so it's not "illegal" to use it. If Rust doesn't allow you to do something while other language does, it doesn't mean it should be illegal.

I don't see any tricks in Go and it's faster than Rust and more memory efficient in almost half of the benchmarks. So what's your excuse for those cases ?

Re:Rust vs C Pitfalls by anoonmoose on Tuesday December 27, 2016 @10:40PM (#13268268)
I believe that what your OP mmstick might have been saying is that the hashing algorithm used in the C version of the algorithm might be very different from the hashing algorithm in the Rust version, and this difference might be significant.

I don't speak Rust, so I can't quite tell what's going on here:

http://benchmarksgame.alioth.debian.org/u64q/program.php?tes...

But the C hash function is very simple, and probably not at all collision-resistant:

  #define CUSTOM_HASH_FUNCTION(key) (khint32_t)((key) ^ (key)>>7)
Again, I don't speak Rust so hopefully someone can comment on this, but I don't see that same simple hash is being used in the Rust...I think it might be using a default hash function, which would probably be more collision resistant.

I am sure that the Rust could be made to do the same thing, but I am not sure that, as-is, this is an apples to apples comparison.

Re:Rust vs C Pitfalls by Manishearth on Tuesday December 27, 2016 @11:18PM (#13268392)
llogiq wrote a post about this: https://llogiq.github.io/2016/12/08/hash.html
Re:Rust vs C Pitfalls by richard_todd on Tuesday December 27, 2016 @09:39PM (#13268018)
I have wondered the same for a while now, and I hope someone familiar with Rust can explain it: in many of the benchmarks Go is faster and/or uses less memory despite the GC. Like you, I don't see much trickiness in the Go code.
Re:Rust vs C Pitfalls by dalailambda on Tuesday December 27, 2016 @10:54PM (#13268314)
In many cases a garbage collected language will be faster when it comes to allocations than using naive allocation strategies (e.g. reallocating a hashmap many times as it grows instead of reserving memory upfront). This is because the garbage collector tends to have preallocated memory lying around, while the RAII needs to call malloc and free every time a heap object is created.

Another factor could also be that the GC in the Go examples just never runs since it doesn't allocate enough memory. It's hard to say exactly what's happening without tracing the runtime, which would also affect the benchmarks a bit.

It's important to note that while both languages are natively compiled (and I suspect Rust would inch out in that category due to LLVM's backing) most of the overhead would probably be from memory, whether allocations, or cache usage, which makes comparing them in microbenchmarks a little inaccurate.

Re:Rust vs C Pitfalls by Manishearth on Wednesday December 28, 2016 @01:13AM (#13268784)
> This is because the garbage collector tends to have preallocated memory lying around,

This is an allocator feature independent of garbage collection; jemalloc does this for example. GCd languages have the potential to do this better since they can do additional analysis whilst tracing for little additional cost, but non-GCd languages can still benefit from this.

Re:Rust vs C Pitfalls by voidlogic on Wednesday December 28, 2016 @10:16AM (#13270984)
Part of this is also that Go is really good at not allocating from the heap when it can allocate from the stack (escape analysis). Until Go 1.5 the Go garbage collector was pretty weak, but it didn't matter as much as it would have for a language with heavier heap allocation.
Re:Rust vs C Pitfalls by steveklabnik on Tuesday December 27, 2016 @06:45PM (#13267312)
Rust can do custom hashes as well, to be clear. There's been some arguments over what hashes are allowed in the game in the past.
Re:Rust vs C Pitfalls by igouy on Wednesday December 28, 2016 @12:40PM (#13272101)
To be clear: anyone can contribute a Rust k-nucleotide program that uses a custom hash function, just like those used by other programming languages.
Re:Rust vs C Pitfalls by steveklabnik on Wednesday December 28, 2016 @12:55PM (#13272213)
Wasn't there some issues around a "custom library" for this? I distinctly remember there being some kind of argument about what is legal and what isn't.

That is, I think I'm thinking of this:

> k-nucleotide will explicitly require built-in / library HashMap.

https://alioth.debian.org/tracker/?func=detail&group_id=1008...

Since C doesn't have a standard library hashmap, you can write an entirely custom one just for the benchmark. But since Rust has a built-in hashmap in the standard library, you cannot. Even though you could write Rust code that's the same as the custom C hashmap.

Re:Rust vs C Pitfalls by pvg on Wednesday December 28, 2016 @01:43PM (#13272573)
The rules provided aren't as clear as they could be:

http://benchmarksgame.alioth.debian.org/u64q/knucleotide-des...

But they say 'don't write a custom hash table', not 'don't write a custom hash function'. Maybe the problem is that the data in this benchmark is just not a good way to exercise the hash tables in a given language the way the benchmark intended. That probably means the benchmark should be modified; the complaint that some implementations use 'laughably bad' hash functions that seem to be measurably decent hash functions for the data at hand seems really strange.

Re:Rust vs C Pitfalls by igouy on Wednesday December 28, 2016 @01:57PM (#13272700)
To be clear: anyone can contribute a Rust k-nucleotide program that uses a custom hash function, just like those used by other programming languages.

> Since C doesn't have a standard library hashmap, you can write an entirely custom one just for the benchmark.

NOT TRUE!

#315195 states the opposite!

"k-nucleotide will explicitly require built-in / library HashMap"

Re:Rust vs C Pitfalls by steveklabnik on Wednesday December 28, 2016 @02:01PM (#13272722)
The "library" distinction you're drawing here is arbitrary. Or at least, my understanding of it is.

Am I allowed to use a custom HashMap for the game, or am I required to use the one in std::collections? My understanding is that it's the latter, and so that penalizes Rust or any other language that includes a map in their standard library.

Re:Rust vs C Pitfalls by igouy on Wednesday December 28, 2016 @02:09PM (#13272785)
Just like everyone else Rust advocates can contribute programs that use a custom Hash function with a library Hash map.

In fact, the current Rust k-nucleotide DOES use a custom Hash function with a library Hash map.

No one is allowed to -- in your words -- "write an entirely custom [hashmap] just for the benchmark".

(This was all discussed to death, in early December on https://www.reddit.com/r/rust/ ).

Re:Rust vs C Pitfalls by steveklabnik on Wednesday December 28, 2016 @02:16PM (#13272829)
You didn't answer my question. Does "library" here mean that you are able to use a hashmap other than the one in std::collections, or not?
Re:Rust vs C Pitfalls by igouy on Wednesday December 28, 2016 @03:44PM (#13273456)
When someone fully re-implements khash in Rust and publishes the library, that will become a question which merits consideration.

Meanwhile, from the task description: Please don't implement your own custom "hash table" - it will not be accepted.

Re:Rust vs C Pitfalls by steveklabnik on Wednesday December 28, 2016 @04:18PM (#13273750)
So, the answer is "no, you are not allowed." But you're open to changing that. Got it.
Re:Rust vs C Pitfalls by igouy on Thursday January 05, 2017 @06:40PM (#13332544)
Can't use a library that doesn't exist.

If you want an additional Rust hashmap implementation then you need to persuade the Rust community to provide one in std::collections.

No one is allowed to -- in your words -- "write an entirely custom [hashmap] just for the benchmark".

Re:Rust vs C Pitfalls by steveklabnik on Thursday January 05, 2017 @07:58PM (#13333012)
> If you want an additional Rust hashmap implementation then you need to persuade the Rust community to provide one in std::collections.

Again, this means that for Rust, it _has_ to be in the standard library. But C doesn't have one in the standard library. So C gets to write their own, and Rust doesn't.

Re:Rust vs C Pitfalls by fungos on Tuesday December 27, 2016 @10:42PM (#13268277)
If it compiles and run it is allowed. There is no rule to benchmark other than having the same end result with best performance.

I still can't get all the moral over this.

The best thing here for Rust would be to achieve the best performance on this game and nothing else.

To me this game is important and winning on it is more yet. So, if Rust don't have better result by "morals" this is so wrong that hurts. But each time I read these responses I ask myself if there is not a real problem there.

I like Rust, but performance is decisive. It is really sad to see that Rust keeps going down on this game and that makes me question myself when trying to invest time on Rust.

Re:Rust vs C Pitfalls by sanderjd on Tuesday December 27, 2016 @11:57PM (#13268496)
It isn't a "moral" issue. It is about the usefulness of the benchmarks. If you're using them to make a recommendation about which programming language to choose, it is important to have a sense for how well the results generalize to the kind of programs you will be writing. Using unrealistic hacks targeted specifically to each benchmark is contra that goal.

If, on the other hand, you're just treating the benchmarks as a fun competition akin to code golf, then by all means, hacks ahoy!

Re:Rust vs C Pitfalls by fungos on Wednesday December 28, 2016 @11:38AM (#13271638)
Benchmark GAME :)
Re:Rust vs C Pitfalls by igouy on Wednesday December 28, 2016 @01:31PM (#13272481)
Re:Rust vs C Pitfalls by eridius on Wednesday December 28, 2016 @03:05AM (#13269145)
> The best thing here for Rust would be to achieve the best performance on this game and nothing else.

That's absurd. The benchmarks game doesn't measure real-world performance in any sense whatsoever. And many of the implementations are written in ways that you'd never ever put into production code (for example, the laughably bad hashing functions that are mentioned elsewhere in this thread). From what I understand, the Rust implementations tend to not get up to those shenanigans, which makes them appear worse than the implementations from other languages that do.

It's not the goal of a programming language to be the top on a silly benchmarks game. The goal is to be an excellent language for real-world use.

Re:Rust vs C Pitfalls by steveklabnik on Wednesday December 28, 2016 @12:14AM (#13268575)
The Benchmarks Game has more rules than that. It's not about "morals."
Re:Rust vs C Pitfalls by fungos on Wednesday December 28, 2016 @11:30AM (#13271570)
But people are criticizing C because the way it is implemented is not "realistic". Or that the "hash" is a hack that will never work in real life.

I don't know the background of these people, but they couldn't be more wrong. So that looks like "morals" or just they don't ever saw real C code out there.

Re:Rust vs C Pitfalls by steveklabnik on Wednesday December 28, 2016 @12:13PM (#13271889)
You're making an assumption that the C code is allowed to have the same algorithm as the Rust code. This is not actually exactly true, based on the rules of the game.
Re:Rust vs C Pitfalls by igouy on Wednesday December 28, 2016 @01:41PM (#13272555)
Where is it forbidden for programs written in C to use FNV?

For example https://github.com/haipome/fnv/blob/master/fnv.c

Re:Rust vs C Pitfalls by steveklabnik on Wednesday December 28, 2016 @01:58PM (#13272701)
I'm referring to my reply to you above about not being able to implement a different map, not the hash function itself.
Re:Rust vs C Pitfalls by pvg on Tuesday December 27, 2016 @06:38PM (#13267265)
Which ones do you have in mind? Preprocessor sounds a little cheaty but picking a hash function that's a better fit for the data is a pretty basic, practical sort of optimization.
Re:Rust vs C Pitfalls by simcop2387 on Tuesday December 27, 2016 @06:44PM (#13267302)
I think he's talking about the C code here, right at the top

http://benchmarksgame.alioth.debian.org/u64q/program.php?tes...

Re:Rust vs C Pitfalls by pvg on Tuesday December 27, 2016 @06:58PM (#13267365)
If that's the one it's a custom hash function which is then inlined as a macro. That still seems like a pretty vanilla C optimization that one might write in real C code.
Re:Rust vs C Pitfalls by eridius on Wednesday December 28, 2016 @03:06AM (#13269149)
The problem isn't that it's a macro. It's that it's a horrifically bad hash function. It's blazing fast, but completely unsuitable for use in any real code.
Re:Rust vs C Pitfalls by pvg on Wednesday December 28, 2016 @08:30AM (#13270315)
If it's a hash function that only works for this exact data set, I can see the argument. If it's a hash function that works for this kind of data (these particularly formatted, particularly constrained strings), it's fair play. Which one is it? 'It's not a good general purpose hash function' alone doesn't seem like a valid criticism, especially along with 'the Rust version uses a general purpose hash function'. Nobody said you had to use a general purpose hash function.

Imagine you got a million strings of variable length such that the last 4 bytes are guaranteed to be unique value between 0 and 999999. Lopping off the last four bytes is a perfectly good hash function for that kind of data.

Re:Rust vs C Pitfalls by eridius on Wednesday December 28, 2016 @01:41PM (#13272556)
Sure, but it's not a good benchmark. If you can demonstrate that you have blazing fast performance, but only for the exact input data that the benchmark uses, then you haven't demonstrated anything at all beyond an ability to tailor your code to a very specific input. But in the real world, we don't get to write our code for a very specific input (after all, if we knew what the input was ahead of time, we could just pre-calculate the answers instead of publishing the program).

So yeah, if you can come up with a tailored hash algorithm that works correctly and is DoS-immune for all possible input for the program, go ahead and use that tailored hash algorithm. But if your hash algorithm is only correct for the specific input data the benchmark game uses, and would not be collision-resistant under real world conditions, then you really shouldn't use that hash as it does not demonstrate anything useful.

Re:Rust vs C Pitfalls by pvg on Wednesday December 28, 2016 @01:56PM (#13272690)
Well, here's the thing. I don't think it's for the exact input, it's for a type of inputs. Custom hash functions for specific types of data are a basic optimization technique and I find it odd you'd even suggest every hash function should be 'DoS-immune'. There's absolutely nothing 'real world' about this unless you think the world consists entirely of hostile inputs. In the real world, people absolutely optimize.

Your argument seems to be that that's not the intent of the benchmark which may be true but it's not clear from the rules provided at all. To me, it looks like the opposite is true - they talk about using a standard hash table and most of those allow user-specified hash functions.

Re:Rust vs C Pitfalls by mmstick on Wednesday December 28, 2016 @02:26PM (#13272927)
Rust's default hashmap algorithm is DoS-immune for any kind of input, which is a perfectly logical default algorithm for a language intended to be used in security-critical areas like operating systems, system libraries, web browsers, etc. and promotes memory safety.
Re:Rust vs C Pitfalls by pvg on Wednesday December 28, 2016 @02:33PM (#13272996)
That's really great but I don't see how it's related unless your argument is truly 'custom hash functions are bad', in which case I don't really know what else to tell you beside 'that's completely wrong'.
Re:Rust vs C Pitfalls by merb on Tuesday December 27, 2016 @05:50PM (#13266927)
yeah of course, doing simple programs and checking their time is a good benchmark... oh wait.. also real world performance in bigger programs is mostly different, especially when you deal with big heaps.

Btw. this site is extremly bad for benchmarks since it also measure's the startup time of the runtime in java/go/rust.

Re:Rust vs C Pitfalls by igouy on Wednesday December 28, 2016 @02:18PM (#13272848)
Re:Rust vs C Pitfalls by FreeFull on Wednesday December 28, 2016 @06:44PM (#13274664)
Rust doesn't have any significant runtime startup cost, but it certainly is an issue for Java (and presumably Go as well).
Re:Rust vs C Pitfalls by igouy on Tuesday January 03, 2017 @01:30PM (#13311587)
It is an issue for Java programs that complete in a few tenths of a second. So these do more work than a few tenths of a second.
Re:Rust vs C Pitfalls by duneroadrunner on Wednesday December 28, 2016 @03:11AM (#13269166)
While you're checking out the performance of Rust and Go benchmark implementations, you can also check out some SaferCPlusPlus benchmarks[1] (and kind of compare them with other languages (transitively via the C++ benchmaks)). I've already suggested that these days a "memory safe" implementation category for the benchmarks would be of interest. But apparently not to the current maintainer of the benchmarks. Anyone else out there got nothing better to do than maintain a benchmark site for memory safe implementations? :)

[1] https://github.com/duneroadrunner/SaferCPlusPlus-BenchmarksG...

Re:Rust vs C Pitfalls by Animats on Wednesday December 28, 2016 @12:40AM (#13268680)
The binary trees benchmark forks off a large number of threads. Those are OS threads in Rust, and that is probably an inefficient way to compute something if you have more threads than CPUs.
Re:Rust vs C Pitfalls by hamilyon2 on Wednesday December 28, 2016 @12:27PM (#13271998)
This site explicitly mentions that results of benchmarks mean almost nothing. Google published results that tell that golang is slower than java in real applications
Re:Rust vs C Pitfalls by igouy on Wednesday December 28, 2016 @02:16PM (#13272836)
> This site explicitly mentions that results of benchmarks mean almost nothing.

Quote?

Re:Rust vs C Pitfalls by moosingin3space on Tuesday December 27, 2016 @05:13PM (#13266660)
The ownership system isn't strictly about memory management and can make it easier to catch yourself making larger architectural errors, and you can have more confidence in a refactor with Rust than Go. In fact, I'd argue Rust leads to simpler architectures that fit well into the ownership model as opposed the the "ad-hoc" architectures programs written in other languages seem to invariably turn into.
Re:Rust vs C Pitfalls by estefan on Wednesday December 28, 2016 @03:25AM (#13269243)
I've literally just started learning Rust after following it for a few years. I wanted a language that was type-safe and produced binaries to simplify deployment. I chose Rust over Go because I wanted a functional language with generics. Go's repetitiveness regarding error handling just put me off.

I've tried learning C/C++ at several times but I just don't have the inclination to have to bother about null-terminating strings, etc in 2016. I don't mind spending a little more time getting something to compile if it prevents silly mistakes.

Having said all that, it's obviously too early for me to say whether I like Rust. I'm picking it up pretty quickly since I know FP thanks to Scala, but I'll see how much time I spend fighting the borrow checker.

Re:Rust vs C Pitfalls by treehau5_ on Tuesday December 27, 2016 @10:42PM (#13268278)
4th law of HN: Whenever Rust is brought up, Go inevitably follows, and vice versa.
Re:Rust vs C Pitfalls by saghm on Wednesday December 28, 2016 @01:18AM (#13268803)
It's kind of a shame, because I don't really feel like the languages are used for similar things in practice, so the constant comparisons don't do either of them justice.
Re:Rust vs C Pitfalls by mmstick on Tuesday December 27, 2016 @05:48PM (#13266912)
I was writing software in Go for a year before I switched to Rust. I've not felt a need to touch Go since. Basically, anything you can do in Go, you can also do in Rust, but Rust will let you do it with higher efficiency and with significantly less lines of code. In the end, it's just easier to write software with Rust than it is Go.

Feature-wise, Rust features generics and functional programming via higher-order functions and iterators, which is something that Go especially lacks in. Go doesn't have nice concepts like `Option`, `Result`, or `Iterator`. That's not something I'd personally want to live without today. The Go method is effectively writing boiler plate code everywhere, which leads to much room for error prone implementations that require more testing.

I haven't felt that Rust was more complex than Go, at least when you're actually writing software in Rust. Rust libraries feature semantic versioning and are automatically downloaded and verified at build time based on the contents of your `Cargo.toml` and `Cargo.lock` files. No importing of Git repositories directly required. Go does not provide an equal on that front.

There are a lot of great libraries out there to bring you extreme performance, simply, such as the bytecount crate, which is just a library that features a single function, a function that counts the occurrence of a specific byte, 32 bytes at a time with AVX, with additional SSE/SIMD implementations depending on what the processor supports.

All there is to truly know about Rust is the borrowing and ownership mechanism and how to implement a custom `Iterator`. If you have a solid understanding of both then you've pretty much mastered all you need to know about Rust.

The borrowing and ownership mechanism can be simplified down to: - Passing a variable by value will move ownership, dropping the original variable from memory - Passing a variable by mutable reference will keep the original variable, but allow you to modify the variable. - You may only borrow a variable mutably once at a time, and you may not immutably borrow while mutably borrowing. - You may have as many immutable borrows as you want, so long as you aren't modifying that value. - You may mutably borrow a field in a struct, and then mutably borrow a different field in the same struct simultaneously, so long as you aren't also mutably borrowing the overall struct. - You can use `Cell` and `RefCell` to allow for mutably modifying an immutable field in a struct. - You may mutably borrow multiple slices from the same array simultaneously so long as there is no overlap. - Safe memory practices means that instead of mutably borrowing the same variable in multiple places, you queue the changes to make in a separate location and apply them serially one after another.

Then for the `Iterator` trait, you would know that all traits have required methods, whereby as long as you implement the required methods for your type, you will automatically gain all of the other methods associated with the trait. For the `Iterator` type, you only need to implement the `next` method, and that looks something like so:

``` struct DataIterator<'a> { data: &'a [u8], index: usize, }

enum Token<'a> { One(&'a [u8]), Two(&'a [u8]) } impl<'a> Iterator for DataIterator<'a> { type Item = Token<'a>; fn next(&mut self) -> Option<Token<'a>> { let start = self.read; for element in self.data.iter().skip(self.read) { self.read += 1; // if next value is found then return Some(&value[start..self.read]) } None } } ```

[dead]
Re:Rust vs C Pitfalls by steveklabnik on Tuesday December 27, 2016 @06:32PM (#13267232)
http://blog.burntsushi.net/ripgrep/

There are only seven instances of unsafe: https://github.com/BurntSushi/ripgrep/search?q=unsafe&type=C...

Four of them are related to calling libc/kernel32 functions, which need unsafe to be called. One is due to using a memory map, which needs unsafe to be called. Only two are actual unsafe rust functions.

That's a "real-world" example though, you're asking for a more specific implementation. I can't compare because my Go would be very poor; if you posted a Go implementation, I'd be willing to give this a shot and write a Rust one.

(Mostly out of personal interest, I don't think it really proves anything larger about the two languages.)

[dead]
Re:Rust vs C Pitfalls by burntsushi on Tuesday December 27, 2016 @06:53PM (#13267344)
None of the unsafe code that Steve linked to had anything to do with searching a file line by line. It has to do with other parts of ripgrep, like determining whether a tty is available or communicating with a Windows console to do coloring. (Hell, ripgrep doesn't even require memory maps, but they are faster in some cases.)

Your benchmark proposal is interesting on the surface, but if done correctly, its speed will come down to highly optimized SIMD routines. For example, in Go, the code to search for a single byte on amd64 is written in Assembly (as it is in glibc too). This means it's probably not a good indicator of language performance overall.

Re:Rust vs C Pitfalls by lossolo on Tuesday December 27, 2016 @07:04PM (#13267401)
The thing is I don't really care if Go implementation is highly optimized for amd64 like memchr is in C which is also written in assembler and optimized for different platforms. What I care is that simple code written by me is faster without going into C/unsafe code myself. So it's correct, fast, simple and I do not pay with my time to figure out how to make it as fast in Rust. This is the point I am making. Of course this is only one example, but still a proof that what OP wrote is not valid in all cases.
Re:Rust vs C Pitfalls by burntsushi on Tuesday December 27, 2016 @07:13PM (#13267445)
ripgrep is your proof. Go read the blog post Steve linked. If you still don't believe it, read the code. There's not that much of it.

Then compare it with similar tools written in Go, like sift and the platinum searcher.

The problem is, searching a file quickly is not as simple as you want to believe. If you show me a naive line by line approah, I'll show you an approach that is much faster but more complex. Top speed requires work, sometimes algorithmic work, regardless of the language you choose.

Re:Rust vs C Pitfalls by lossolo on Tuesday December 27, 2016 @07:37PM (#13267537)
> If you show me a naive line by line approah, I'll show you an approach that is much faster but more complex.

Of course, I did it myself many times. But this is NOT the point, I've already wrote it. The point is that I wrote naive approach in both languages and it's a lot faster in Go. Which is a reply to what OP wrote (don't forget where this discussion started). In this case this is the fact and I don't see any reason to fight with facts if we get bias out of the equation. In other cases? I don't know. What I would expect is that naive searching for one string in haystack to be faster than Go in a language that is performance/zero-cost abstractions oriented like Rust but its false in this case. But it doesn't mean it's false in every case. And to be honest writing that "but they have faster implementation in assembler" is not an excuse at all, you can also write your own, especially if it works so well for those languages that have custom asm for specific platforms. In the end average Joe will not care if it's hand written assembler, he will care that his naive solution using standard library without any magic is just faster.

Re:Rust vs C Pitfalls by burntsushi on Tuesday December 27, 2016 @09:51PM (#13268051)
> The point is that I wrote naive approach in both languages and it's a lot faster in Go.

I tried your challenge, and the first data point I uncovered contradicts this. Here is the source code of both programs: https://gist.github.com/anonymous/f01fc324ba8cccd690551caa43... --- The Rust program doesn't use unsafe, doesn't explicitly use C code, is shorter than the Go program, faster in terms of CPU time and uses less memory. I ran the following:

    $ /usr/bin/time -v ./lossolo-go /tmp/OpenSubtitles2016.raw.sample.en the
    $ /usr/bin/time -v ./target/release/lossolo-rust /tmp/OpenSubtitles2016.raw.sample.en the
Both runs report 6,123,710 matching lines (out of 32,722,372 total lines). The corpus is ~1GB and can be downloaded here (266 MB compressed): http://burntsushi.net/stuff/OpenSubtitles2016.raw.sample.en.... --- My /tmp is a ramdisk, so the file is in cache and I'm therefore not benchmarking disk reads. My CPU is an Intel i7-6900K.

The Go program takes ~6.5 seconds and has a maximum heap usage of 7.7 MB. The Rust program takes ~4.2 seconds and has a maximum heap usage of 6 MB. (As measured by GNU time using `time -v`.)

---

IMO, both programs reflect "naive" solutions. The point of me doing this exercise is to show just how silly this is, because now we're going to optimize these programs, but we'll limit ourselves to smallish perturbations in order to put a reasonable bound on the task.

If I run the Go program through `perf record`, the top hotspot is runtime.mallocgc. Now, I happen to know from experience that Scanner.Text is going to allocate a new string while Scanner.Bytes will not. I also happen to know that the Go standard library `bytes` package recently got a nice optimization that makes bytes.Contains as fast as strings.Contains: https://github.com/golang/go/commit/44f1854c9dc82d8dba415ef1... --- Since reading into a Go `string` doesn't actually do any UTF-8 validation, we don't lose anything by switching to using raw bytes.

Knowing this, we can tweak the Go program to great effect: https://gist.github.com/anonymous/c98dc8f6be6d414ae3e7aa6931... --- Running the same command as above, we now get a time of ~2.3 seconds and a maximum heap usage of 1.6 MB. That's impressive.

Now let's see if we can tweak Rust, which is now twice as slow as the Go program. Running perf, it looks like there's an even split between allocation, searching and UTF-8 validation, with a bit more towards searching. Like the Go program, let's attack allocation. In this case, I happen to know that the `lines` method returns an iterator that yields `String` values, which implies that it's allocating a fresh `String` for every line, just like our Go program was. Can we get rid of that? The BufReader API provides a `read_line` method, which permits the caller to control the `String` allocation. If we use that, our Rust program is tweaked to this: https://gist.github.com/anonymous/a6cf1aa51bf8e26e9dda4c50b0... --- It's not quite as symmetrical as a change as we made to the Go program, but it's pretty straight-forward IMO. Running the same command as above, we now get a time of ~3.3 seconds and a maximum heap usage of 6 MB.

OK, so we're still slower than the Go program. Looking at the profile again, the time now seems split completely between searching and UTF-8 validation. The allocation doesn't show up at all any more.

Is this where you got stuck? The next step from here isn't straight-forward because getting rid of the UTF-8 validation isn't possible to do safely while still using the String/&str search APIs. Notably, Rust's standard library doesn't provide a way to search an `&[u8]` directly using optimized substring search routines. Even if you knew your input was valid UTF-8 before hand, there's no obvious place to insert an unsafe `from_utf8_unchecked` because the BufReader itself is in control of producing the string contents. (You could do this by switching to using `BufReader.read_until` and then transmuting the result into an &str, but that would require unsafe.)

Let's take a leap. Rust's regex library has a little known feature that it can actually search the contents of an &[u8]. Rust's regex library isn't part of the standard library, but it is maintained as an official crate by the Rust project. If you know all of this, then it's possible to tweak the Rust program just a bit more to regain the speed lost by UTF-8 checking: https://gist.github.com/anonymous/bfa42d4f86e03695f3c880aace... --- Running the same command as above once again, we now get a time of ~2.1 seconds and a maximum heap usage of 6.5 MB.

In sum, we've beaten Go in CPU time, but lost the Battle for Memory and the battle for obviousness. Beating Go required noticing the `read_until` API of BufReader and knowing that 1) Rust's regexes are fast and 2) they can search &[u8] directly. It's not entirely unreasonable, but to be fair, I've done this without explicitly using any unsafe or any C code.

None of this process was rocket science. Both the Go and Rust programs were initially significantly sub-optimal because of allocation, but after some light profiling, it was possible to speed up both programs quite a bit.

---

Compared to the naive solution, some of our search tools can be a lot faster. Performing the same query on the same corpus:

    ripgrep    1.13 seconds, 7.7 MB
    ripgrep    1.35 seconds, mmap
    GNU grep   1.73 seconds, 2.3 MB
    ag         1.80 seconds, mmap
    pt         6.41 seconds, mmap
    sift      50.21 seconds, 16.6 MB
The differences between real search tools and our naive solution actually aren't that big here. The reason why is because of your initial requirement that the query match lots of lines. Lots of matches results in a lot of overhead. If we change the query to a more common type of search that produces very few matches (e.g., `Sherlock Holmes`), then our best naive programs drop down to about ~1.4 seconds, but ripgrep drops to about 200 milliseconds.

From here, the next step would be stop parsing lines and start searching the entire buffer directly. (I hope to make even this task very easy by moving some of the searching code inside of ripgrep to an easy to use library.)

---

In sum, your litmus test essentially comes down to these trade offs:

- Rust provides a rich API for its String/&str types, which are guaranteed to be valid UTF-8.

- Rust lacks a rich substring search API in the standard library for Vec<u8>/&[u8] types. Because of this, efficient substring search using only the standard library has an unavoidable UTF-8 validation cost in safe code.

- Go doesn't do any kind of UTF-8 checking and provides mirrored substring search APIs between its `bytes` and `strings` packages.

- The actual performance of searching in both programs probably boils down to optimized SIMD algorithms. Therefore, once you get past the ability to search each line of a file with minimal allocation, you've basically hit a wall that's probably the same in most mainstream languages.

In my opinion, these trade offs strike me as something terribly specific, and it's probably not something that is usefully generalizable. More than that, in the naive case, Rust is doing you a good service by checking that your input is valid UTF-8, which is something that Go doesn't do. I think this could go either way, but I think it's uncontroversial that guaranteeing valid UTF-8 up front like this probably eliminates a few possibly subtle bugs. (I will say that my experience with text encoding in Go has been stellar though.)

Most importantly, both languages at least have a path to writing a very fast program, which is often what most folks end up caring about at the end of the day.

Re:Rust vs C Pitfalls by Manishearth on Tuesday December 27, 2016 @10:40PM (#13268271)
... whoa that's a comprehensive comment.

Do you think you could refactor out bytestring-based string manipulation into its own library? Even better would be something that worked for all encodings (using https://github.com/servo/tendril or something)

Re:Rust vs C Pitfalls by burntsushi on Wednesday December 28, 2016 @06:54AM (#13269950)
> Do you think you could refactor out bytestring-based string manipulation into its own library?

IIRC, someone was working on making the Pattern trait work on &[u8], but I'm guessing that work is stalled.

To factor it out into a separate crate means copying the &str substring routines, since there's no way to safely use them on an &[u8] from the standard library. (bluss did that in the `twoway` crate, so we could just use that.)

It does seem like a plausible thing to do, at least until std gets better &[u8] support.

> Even better would be something that worked for all encodings

I suspect the standard practice here is something like "transcode to UTF-8 and then search the UTF-8." (This is what I hope to do with ripgrep.)

> (using https://github.com/servo/tendril or something)

I don't think I know what problems tendril is solving, so it's not clear to me what it's role is.

Re:Rust vs C Pitfalls by degurechaff on Wednesday December 28, 2016 @12:39AM (#13268677)
woah, amazing comment. I usually just a silent reader on hacker news, but this comment urge me to create an account. I think lossolo just want a flamewar. He already has opinion which you cannot easly change. So any futher discussion after this comment will be pointless.

EDIT: and how can you have time to write this? I just usually close the browser tab when this situation occurs...

Re:Rust vs C Pitfalls by burntsushi on Wednesday December 28, 2016 @06:56AM (#13269955)
Thanks! I like to think I usually close the browser tab, but text search is just kinda my thing. :-)
Re:Rust vs C Pitfalls by lossolo on Wednesday December 28, 2016 @01:49AM (#13268900)
> I think lossolo just want a flamewar. He already has opinion which you cannot easly change. So any futher discussion after this comment will be pointless.

My opinion in this case is empirically checked. I am not saying this to start flame war, I am just showing my observations in particular example. I can also say that Rust regex implementation eats Go regex implementation by magnitudes (performance wise) and it will also be true and I am not looking for any flame war in this case also. I am only sharing my experience that I've backed up with proofs (code + perf results) for that particular use case. This is a factual discussion, I don't agree it's pointless.

Re:Rust vs C Pitfalls by nickpsecurity on Tuesday December 27, 2016 @10:12PM (#13268170)
Best example of evidence-based rebuttal I've seen in a language argument on here in a long time. Great write-up!
Re:Rust vs C Pitfalls by andreimuntean on Tuesday December 27, 2016 @11:11PM (#13268372)
Amazing! Thank you for your comment.
Re:Rust vs C Pitfalls by lossolo on Tuesday December 27, 2016 @11:44PM (#13268452)
I have different times for both naive solutions on my machine than you.

It's 2.6 seconds for Go and 3.5 seconds for Rust, both perf results and code here: http://pastebin.com/WwhvHH6S

Re:Rust vs C Pitfalls by burntsushi on Tuesday December 27, 2016 @11:52PM (#13268478)
Your Rust program corresponds to my second Rust program.

Your Go program is not what I would expect. A bufio.Scanner is the idiomatic (and naive) way to read lines in Go. But this is immaterial. Your results are consistent with mine. They aren't different. I just included more analysis and more programs to provide proper context to this challenge of yours.

Re:Rust vs C Pitfalls by lossolo on Wednesday December 28, 2016 @12:16AM (#13268585)
I gave you naive solution, now lets see amateurish solution (someone totally new to both languages)

Rust 4.6s

Go 3.1s

http://pastebin.com/r6K22Dt2

EDIT:

Using grep 0.6s

Then I have installed rigrep and...

Using ripgrep 0.4s

Really nice burntsushi. I am surprised by those (ripgrep) results compared to grep.

Re:Rust vs C Pitfalls by mmstick on Wednesday December 28, 2016 @01:53AM (#13268906)
Seems I can't reply to your other comment, so I'll reply here. How can you say that my naive implementation is not naive? What is not naive about it? It's very naive. It's basically the same naive code that you were writing, but in actual idiomatic Rust with the linting issues fixed.

Using a `lines()` approach is naive because that allocates owned heap-allocated strings. An optimal, non-naive solution would not use heap allocation for buffering lines but use a stack-allocated array. That alone would bring significant speedup versus the line approach.

As for ripgrep, it's a pretty comprehensive application that makes use of SIMD/AVX so it's only natural that it's fast.

Re:Rust vs C Pitfalls by lossolo on Wednesday December 28, 2016 @01:21PM (#13272412)
Your solution is not naive in my opinion because you set the size of the buffer and use map/filter but ok... Let's check. Your solution is the slowest from all the solutions.

It took 4.6s which only confirms what I wrote on beginning when we started this discussion. Perf counters for your solution in here:

http://pastebin.com/Anak1ahe

Re:Rust vs C Pitfalls by mmstick on Wednesday December 28, 2016 @01:09AM (#13268769)
Your Rust example is kind of odd. Did you not see the linting errors in your editor or from the compiler? You can basically boil it down to just two lines of Rust.

https://gist.github.com/mmstick/a8316ba0514f9d9ab33b18fa9b91...

As for timing, I'm doubtful that Go is any faster than Rust. I don't have this www.js file so I can't test it on my laptop, but I'm pretty sure you didn't even attempt to do things like enabling LTO, setting O3, disabling jemalloc, and using the musl target. All these things can make significant differences to the performance of the binary that's produced.

Re:Rust vs C Pitfalls by lossolo on Wednesday December 28, 2016 @01:35AM (#13268859)
I don't know if you noticed but we are discussing naive solutions which I've mentioned couple of times in previous posts. Code you linked is not naive solution. Things you proposed to do are not naive either.
Re:Rust vs C Pitfalls by AsyncAwait on Wednesday December 28, 2016 @08:09AM (#13270217)
Here's the thing; it is a naive solution. You may not want to accept it because that contradicts your claim of the Go solution being faster, but at that point it becomes a he-said/she-said kind of scenario because I can claim that your Go solution isn't naive as well and have exactly the same "validity" for such a claim as you do.
Re:Rust vs C Pitfalls by lossolo on Wednesday December 28, 2016 @01:11PM (#13272341)
Anyway this really doesn't matter as his solution is the slowest of all which confirms what I wrote and contradicts what he wrote.

For me it's not naive solution. Do you have any proof it is? Can you mathematically prove that it's naive solution? I know I can't. For everyone naive solution is something different, what I saw in burntsushi reply and what I wrote myself is the closest to what I think are naive solutions.

> like enabling LTO, setting O3, disabling jemalloc, and using the musl target.

And this is for sure not part of naive solution either.

> You may not want to accept it because that contradicts your claim of the Go solution being faster

This is not true. You can find perf numbers of his solution in my second reply to him. Or you can compare those solutions yourself.

Re:Rust vs C Pitfalls by AsyncAwait on Wednesday December 28, 2016 @02:34PM (#13272997)
> For me it's not naive solution. Do you have any proof it is? Can you mathematically prove that it's naive solution?

You can't prove a negative.

Re:Rust vs C Pitfalls by caconym_ on Tuesday December 27, 2016 @09:52PM (#13268057)
> but still a proof that what OP wrote is not valid in all cases.

You are not proving anything until you post your Go code, which you don't seem to have done (please correct me if I'm wrong, I am legitimately curious to see for myself how Go and Rust stack up against each other for this problem). Until then, all you're doing is making vague claims backed up by precisely zero evidence. Why should anyone take you seriously?

Re:Rust vs C Pitfalls by lossolo on Tuesday December 27, 2016 @11:46PM (#13268457)
I have posted the code in reply to burntsushi. You can check it out yourself.
Re:Rust vs C Pitfalls by neandrake on Wednesday December 28, 2016 @02:32AM (#13269029)
I've seen several of your comments indicate "requiring naive implementation". This seems strange to me. Why require a naive implementation and then be concerned over some slight differences in performance?
Re:Rust vs C Pitfalls by steveklabnik on Tuesday December 27, 2016 @06:47PM (#13267319)
Yes, and I was saying "even in production-grade, world-class grep implementation, there is barely any unsafe." I also acknowledged that that was different than what you were asking about.

When did you ask on IRC? I'll take a look.

Re:Rust vs C Pitfalls by saghm on Wednesday December 28, 2016 @01:24AM (#13268824)
> I told you NO unsafe code. Use only standard library, no third party tools.

I'm not sure what such an example would prove, other than maybe "Go is better than a subset of Rust excluding some of the core features of the language and its greater ecosystem".

Re:Rust vs C Pitfalls by oconnor663 on Wednesday December 28, 2016 @01:51AM (#13268903)
> no third party tools

This is a tricky requirement. The standard libraries of both languages use a crap ton of unsafe code. You might end up just asking for a comparison of which standard library is bigger.

But at the end of the day, both languages have perfectly standard buffered readers (https://doc.rust-lang.org/std/io/struct.BufReader.html), shouldn't a simple search like this compile to the exact same code?

Re:Rust vs C Pitfalls by lobster_johnson on Tuesday December 27, 2016 @10:57PM (#13268327)
As someone who writes Go full time, once I'm done with my current big Go project I will be taking a break to investigate alternatives. Both Rust and Swift are at the top of my list.

Go is good, even great, at many things. But it's a language largely defined by its limitations, usually intentionally. It's an engineering language, not made for big abstractions. For me, the largest frustration is that the language gets in the way, and the pain increases with the scale of the problem. Which is to say: I think Go scales to large projects just fine, but there are problems where you'd like to build big building blocks on top of smaller blocks on top of smaller blocks, and Go doesn't lend itself to certain kinds of big, composable, data-oriented abstractions. It's small building blocks all the way.

I've bumped into several very real problems recently where Go's coarse, not-very-data-oriented imperative approach has revealed itself as a liability, and where I found myself fantasizing how I could have done it in just a few elegant lines in Haskell. Sometimes they're about expressing things simply in a composable manner, and sometimes these problems simply manifest themselves in immense blobs of boilerplate/repetition (for example, because you have to implement the same method a few dozen times on different data structures, which in a different language could be solved with a generic implementation), where Go's solution is to either eschew type safety, use slow reflection APIs, or programmatically generate the Go code as part of the build process.

Go's is also frustrating in its selective pragmatism. Where Go has chosen to automate and sugarcoat some complicated things (memory management, goroutines), it's stubbornly unpragmatic about other things (error handling, working with polymorphic data, memory safety). Go has been ridiculed for its simplistic error system, but I'm not an extremist here; I'm all for errors being values, and not a fan of exceptions. But if you look at actual Go code, a huge amount of code has to interact with errors. When nearly every function is riddled with "if err != nil", you should know that your language is crying out for just a little syntactic sugar. Or a solid type-system solution for that matter. Enums (Rust-style) and pattern matching wouldn't go against Go's grain at all, but since Go is "done", we're stuck with how it is.

I think Go's focus on simplicity is very important (I'm a big fan of the Wirth school of languages), and my worry about Rust and Swift is that they never learned this lesson. To me, both Rust and Swift looked more promising early in the design process than in their current state; Swift looks increasingly like Scala every time I visit it, whereas Rust often feels lost in a sea of punctuation. That said, my annoyance with Go is acute enough that I'm willing to deal with a few downsides if I can get a language that better matches the kinds of projects that I build.

Re:Rust vs C Pitfalls by Manishearth on Tuesday December 27, 2016 @11:02PM (#13268346)
> my worry about Rust and Swift is that they never learned this lesson

FWIW the Rust team does consider simplicity to be important. However, it's not the only goal, which means that sometimes it has to be sacrificed to be able to make something possible. But most new language features do get discussed in the context of a "complexity budget".

So Rust tries pretty hard to ensure it doesn't get more complex than it has to; but it doesn't put simplicity as the overriding goal and hang all else to get it.

> Enums and pattern matching wouldn't go against Go's grain at all

I've said this many times before -- I don't miss generics from Go; I understand why they're not in the language. I miss ADTs and pattern matching.

Re:Rust vs C Pitfalls by red75prime on Tuesday December 27, 2016 @11:26PM (#13268417)
We need some syntax blurring goggles to judge new languages it seems.
Re:Rust vs C Pitfalls by EugeneOZ on Wednesday December 28, 2016 @02:58AM (#13269119)
Rust is not more complex, it just takes some more time to get used to.
Re:Rust vs C Pitfalls by fleetfox on Wednesday December 28, 2016 @03:55AM (#13269346)
I'm learning Rust. I considered Go but feature wise compared to Rust it seems really boring and plain. IMHO modern language has to have functional flavor.
Re:Rust vs C Pitfalls by catnaroek on Tuesday December 27, 2016 @05:56PM (#13266973)
Go's GC isn't a big deal for most use cases. However, the loss of static guarantees regarding thread-safe manipulation of arbitrarily complex data structures is a big deal.
Re:Rust vs C Pitfalls by enygmata on Wednesday December 28, 2016 @06:10AM (#13269809)
> * Great Libraries ( Everything and a kitchen sink )

This is a must for me. I often give up on compiled languages because libfoobar isn't yet available on the language and I have no interest in writing (and maintaining) language bindings for third party libraries I use.

My dream is that one day I'll be able to do something like:

    Foobar = link_cimport({"headers": ["foobar.h"], "packages": [ "foobar" ], "prefix": "foobar_"});
    Foobar.do_something(); // would call foobar_do_something();
    Foobar.hidden_function(); // would call hidden_function();
and the compiler will give me an executable that was linked against `foobar` instead of dlopen()ing it at runtime. Something like:

    Foobar = loadable_cimport({"headers": ["foobar.h"]});
    foo = Foobar.load("/path/to/foo.so");
    bar = Foobar.load("/path/to/bar.so");
would also be nice because I often have to load libraries that implement the same API/ABI.
Re:Rust vs C Pitfalls by psi-squared on Wednesday December 28, 2016 @07:20AM (#13270034)
It looks like the first part of what you want is nearly there - see https://github.com/Yamakaky/rust-bindgen

It converts C headers to a Rust module containing the relevant type/function/etc. definitions. On stable you need to either pregenerate the module (not too bad if you're pinning particular versions of libraries anyway) or add a build script to autogenerate it. On nightly, there's also a compiler plugin that does all the work for you, and looks not-too-dissimilar to what you wanted. The only thing it lacks is the prefix removal stuff.

Re:Rust vs C Pitfalls by manaskarekar on Tuesday December 27, 2016 @05:13PM (#13266667)
In addition to htaunay's post about docs, Rust also has cargo and crates.io which are fucking awesome.

Blog posts are one way to showcase tooling, perhaps not this one, but they have their place.

Re:Rust vs C Pitfalls by santaclaus on Tuesday December 27, 2016 @05:25PM (#13266740)
> Rust also has cargo and crates.io

Which is undersold in the propaganda! Safety, ehhh I'll take it but I'm not jonesing for it. A package manager that works and has adoption? Thank you! I've wasted far too much time wrangling C++ dependencies by hand...

Re:Rust vs C Pitfalls by masklinn on Wednesday December 28, 2016 @05:26AM (#13269666)
My guilty pleasure these days is running `cargo doc --open`. Generating local docs for my package and all its dependencies? I love.
Re:Rust vs C Pitfalls by lacampbell on Tuesday December 27, 2016 @05:12PM (#13266648)
C is a very small and very portable language. Rust is not. I wondering why people bother comparing them at all.
Re:Rust vs C Pitfalls by pornel on Tuesday December 27, 2016 @07:57PM (#13267636)
Because Rust is IMHO the first viable replacement for C. Well-written C programs translate almost 1:1 to Rust.

It can do (roughly) everything C can, i.e. it's natively compiled, gives control of memory layout, and doesn't depend on a GC runtime.

Previous C killers were either dependent on a fat runtime (which isn't a big problem in general, but is a problem for some of niches dominated by C), or didn't offer meaningful improvement in safety/concurrency/expressiveness.

Re:Rust vs C Pitfalls by blub on Wednesday December 28, 2016 @06:52AM (#13269942)
Easy on the propaganda there.

* "First viable replacement for C" - C++ was invented decades before Rust.

* "Well-written C programs translate almost 1:1 to Rust" - C programs can be compiled with minimal changes as C++.

* "or didn't offer meaningful improvement in safety/concurrency/expressiveness." - C++ offers major improvements in all of those areas.

A lot of C programmers don't want to switch to C++ and they won't want to switch to Rust either, because they favor simplicity and neither Rust nor C++ are simple. They'll probably switch to golang for some stuff if anything and just stick to C for the rest.

Re:Rust vs C Pitfalls by pornel on Thursday December 29, 2016 @06:39AM (#13276802)
I'm a C programmer who switched to Rust.

Rust is complex because it's powerful.

C++ is complex because it's a mashup of overlapping features from different eras.

I don't write C++, because whatever I write will crash for subtle reasons and I'll be told it's my fault, because of course you don't do this when you use that feature.

I can write Rust with all its complexity, and as long as I get it to compile, I'm confident it works more reliably than any C++ that I could have written.

Re:Rust vs C Pitfalls by cderwin on Wednesday December 28, 2016 @12:42AM (#13268688)
On my system (2015 MBP running Ubuntu 16.04) Rust's hello world compiles to a nearly 3.5 MB executable. Compiling the equivalent C program with clang results in an executable under 9 KB. Rust may not have a GC, but its executables are certainly fat.

Note: Rust executables can be trimmed down by stripping them (not done by default, even for release builds) and using libc malloc instead of jemalloc. But even then, C wins by a lot.

Relevant blog post: https://lifthrasiir.github.io/rustlog/why-is-a-rust-executab...

Re:Rust vs C Pitfalls by Manishearth on Wednesday December 28, 2016 @01:38AM (#13268869)
> But even then, C wins by a lot.

C links dynamically to its libraries. Rust does not (by default).

This overhead is a constant overhead so it rarely matters. When it does, you can strip it down further and get it to the same level as C.

This is not evidence of a fat runtime, just a different default compilation strategy that prodces larger binary executables. jemalloc is (an optional) part of the runtime, but the rest isn't.

Re:Rust vs C Pitfalls by cderwin on Thursday December 29, 2016 @12:52AM (#13275867)
Sure. It's not a fair comparison. But dynamically linking rust's std isn't an option. If you are optimizing for executable size, c wins because dynamically linking libc is possible. This is even true controlling for glibc, since most rust code also dynamically links glibc too (including hello world). In fact, rust's hello world has quite a few more shared dependencies than the equivalent c anyway: it uses libdl, librt, libpthread, and libgcc_s as well.
Re:Rust vs C Pitfalls by Manishearth on Thursday December 29, 2016 @01:32AM (#13275999)
Dynamically linking to rust std (with a specially compiled libstd dylib) is an option iirc, just that nobody does it. Lack of stable ABI makes it less useful, but if you really need to do it there's nothing stopping you.

I suspect rust hello world using those libraries is due to a lack of LTO? Hello world doesn't need any of those.

Re:Rust vs C Pitfalls by mmstick on Wednesday December 28, 2016 @01:39AM (#13268871)
Not a particularly fair comparison though, as that C binary is loading dynamic libraries that are already in memory. Case in point: that C program needs the C standard library, but by default that library (glibc) is dynamically loaded. Rust is not offering the Rust standard library as a dynamic library, hence the larger size. Not to mention, interfacing with C functions in the kernel.

You'll get different results if you compile them with musl for truly static and fair binary size comparisons.

Re:Rust vs C Pitfalls by 0xFFC on Wednesday December 28, 2016 @03:13AM (#13269182)
I am no expert, but you should compile barebone executable for comparing executable size! not "hello world", because hello world does use shitload of library (for printing and loading and stuff), and those ones in C is compiled in dynamic, but in rust those compiled inot executable itself.

if I remember correctly you should compile with --nostd flag and provide _start function for loader to load your executable!

you can do same with rust too (But I think you should provide a special flag for compiler). You can find all of it on documentation.

Re:Rust vs C Pitfalls by gpderetta on Tuesday December 27, 2016 @05:13PM (#13266665)
I doubt that a significant amount of C programmers will switch to Rust. On the other hand, rust is very attractive for us C++ programmers.
Re:Rust vs C Pitfalls by zerofan on Tuesday December 27, 2016 @05:49PM (#13266924)
To me it's almost the opposite. I like C a lot, but I ended up compromising on C++11 because in some programs I need more features to keep the implementation clean. I pay the cost of a messy language (C++) when implementing my libraries in order to have simpler applications that use those libraries.

I've written C-like programs in Rust, and that goes very well. But I really like function overloading, generic operators that I can overload from the left and the right, integer parameters for my templates/generics, copy semantics as the default (opt-in for moves), placement new, explicit destructors, and maybe a few other niceties. Rust does none of these things the way I want, and so I'm stuck with C++.

There are a few other things where I think Rust just made the wrong choices and didn't improve over C++. I've always hated the dichotomy in C++ strings between const char* pointers and an actual string class. Rust had the chance to make strings feel as comfortable as integers, but instead they introduced their own dichotomy with String and &str.

Speaking of integers, Rust's choice of 32 bit integers as the default for literals is painful given that every machine most of us will ever care about is now 64 bit. I routinely deal with arrays and files that are too large for a 32 bit integer. This means I have to remember to suffix all of my integers in for loops etc... C++ has this wrong too (backwards compatibility), but Rust had a chance to make a clean start and did not.

Re:Rust vs C Pitfalls by mmstick on Wednesday December 28, 2016 @02:15AM (#13268979)
> Rust had the chance to make strings feel as comfortable as integers, but instead they introduced their own dichotomy with String and &str.

It makes perfect sense when you understand the differences and reasoning behind it. A `String` is a heap-allocated string that can grow in size. On the other hand, a `str` is basically a fixed-size string array, but you'll never interact directly with this type because there's no point in it. It's tucked away inside the `String` that created it.

Meanwhile, an `&str` is a slice of that fixed-size `str` array that's hidden within the `String`. You can think of a `str` as an `[char]` with it's own special `str` methods, and an `&str` as an `&[char]` which has access to all of the `str` methods as well.

> Speaking of integers, Rust's choice of 32 bit integers as the default for literals is painful given that every machine most of us will ever care about is now 64 bit. I routinely deal with arrays and files that are too large for a 32 bit integer.

I'm pretty sure the default integer is a `usize`, which is 32-bit on 32-bit systems and 64-bit on 64-bit systems.

Re:Rust vs C Pitfalls by Manishearth on Wednesday December 28, 2016 @12:50PM (#13272181)
No, the default integer is u32. If type inference can't provide a better type, Rust will pick u32. If type inference can pick a better type it will use it. So if you create an unsuffixed int literal and use it for indexing, that literal will be a usize.

Rust will type error if you try to use u32s with an array so it's all good though. Just means that you need to specifically `: usize` things.

If you need an integer type for counting stuff, u32 is fine. If you actually need to talk about memory, use usize. The compiler will force you to do it.

Re:Rust vs C Pitfalls by ayosec on Wednesday December 28, 2016 @02:37PM (#13273026)
> No, the default integer is u32

Maybe I'm missing something, but it seems to be i32: https://play.rust-lang.org/?gist=c79d4afef7fa20c81ba14de61f5...

Re:Rust vs C Pitfalls by mmstick on Wednesday December 28, 2016 @01:56PM (#13272682)
That makes sense. I've never noticed what the default size was, but because it always worked with indexing, I assumed that it was always usize.
Re:Rust vs C Pitfalls by Manishearth on Wednesday December 28, 2016 @02:22PM (#13272887)
I believe it used to be uint (usize) back in pre-1.0, and IIRC inference didn't work well or wasn't supposed to work at all. I recall needing explicit prefixes on my int literals back then. No longer the case.
Re:Rust vs C Pitfalls by steveklabnik on Tuesday December 27, 2016 @06:10PM (#13267081)
How would you have gone about making Strings be "as comfortable as integers"?

Arrays are indexed by usize, so if you're on a 64-bit machine, then you shouldn't need a cast. It's _unconstrained_ numbers that default to i32, not anything without a suffix.

Re:Rust vs C Pitfalls by zerofan on Tuesday December 27, 2016 @07:47PM (#13267587)
> How would you have gone about making Strings be "as comfortable as integers"?

I would prefer a str be a str be a str, regardless of how you got it. Lowercase type-name and fundamental like an integer.

I'm fairly certain I understand why Rust made the choice they did. I've read the forum threads at HN, Reddit, and users.rust-lang, and I've seen previous replies by you and other Rusties, so I hope you won't try to educate me about the performance advantages of having slices as references and another string type as an ownership class or why we need OsStr and friends.

If strings are the fundamental processing concern in your application, then I think you should be able to opt-in to that kind of micro-optimization and complexity, but it would've been better to spare the rest of us who have different concerns. I don't want to become a string expert to build a filename, and the default implementation could (at least conceptually) be always on the heap for all I care. Go one step further and implement the "small string optimization" (Alexandrescu's fbstring), and you'd probably get back most of the performance without nearly the complexity.

> Arrays are indexed by usize

I've spent the last half hour trying to troubleshoot why this line hangs:

    let aa = vec![1u8; 10e9 as usize];
However, I'm at home using the Ubuntu under Windows thing, so maybe there's some bad mojo between Rust and my less than usual setup. (If you're interested, I have 32 Gigs of memory, and the equivalent C malloc and memset code runs just fine, so I don't think Rust is doing the right thing here...).

Anyways, I wanted to test the commented out line, but I'm stuck for now. If that line would work, I'll admit I was wrong, but I think the uncommented line shows a similar complaint.

    for ss in 0..63 {
        //print!("{}\n", aa[1<<ss]);
        print!("{}\n", 1<<ss);
    }
> It's _unconstrained_ numbers that default to i32, not anything without a suffix.

Looking at the present and the future, why is that a sensible default? Both x64 and ARM are going to use a 64 bit integer register for the operations, and many of those operations are going to be 1-clock throughput. You can probably find a counter example, but 32 bit integers aren't generally faster than 64 bit ones.

Re:Rust vs C Pitfalls by mmstick on Wednesday December 28, 2016 @02:28AM (#13269020)
> I would prefer a str be a str be a str, regardless of how you got it. Lowercase type-name and fundamental like an integer.

The lowercase type names are reserved to primitives. The String type is not a primitive but a comprehensive data structure, hence the capital S. The String type contains an `str` primitive though, along with size information.

> I don't want to become a string expert to build a filename, and the default implementation could (at least conceptually) be always on the heap for all I care.

Is it that hard to understand that when you create a string, you will create it as either a `String` or `PathBuf`? File methods are designed to automatically convert input parameters into a `&Path` so it doesn't matter what string structure you provide.

There is also no way (currently) to create a stack-allocated string with the standard library out of the box. You can do this with crates like `arrayvec` though. It's very much opt-in for that performance.

let path = String::from("/tmp/file");

let mut file = File::open(&path).unwrap();

> Looking at the present and the future, why is that a sensible default? Both x64 and ARM are going to use a 64 bit integer register for the operations, and many of those operations are going to be 1-clock throughput. You can probably find a counter example, but 32 bit integers aren't generally faster than 64 bit ones.

No need to use a 64-bit integer when you only need a 32-bit integer. You can fit two 32-bit integers into a single 64-bit integer and perform a calculation on both simultaneously with a single cycle, versus spending two cycles to calculate two 64-bit integers. There's also no need to pay that memory cost either.

Re:Rust vs C Pitfalls by steveklabnik on Tuesday December 27, 2016 @07:56PM (#13267633)
> so I hope you won't try to educate me

No, I'm interested in what tradeoffs you would have made differently. I now understand. Thanks! (I disagree, but at least I understand.)

> why this line hangs:

It compiles and runs effectively instantaneously for me on Ubuntu under Windows as well, so that's very strange. Maybe file a bug?

> I think the uncommented line shows a similar complaint.

Yes, there's no constraint on that literal, so it's going to be an i32. When we made this decision, we did some analysis, basically no numbers in real world programs weren't constrained, it was often tests, toy programs, and documentation. It should be a rare thing. YMMV.

> why is that a sensible default?

Your assertion about the speed was the opposite of what was asserted while we had the discussion, basically. And not everybody is running on 64-bit hardware, so it's a broader default.

Re:Rust vs C Pitfalls by zerofan on Tuesday December 27, 2016 @10:10PM (#13268158)
> It compiles and runs effectively instantaneously for me on Ubuntu under Windows as well, so that's very strange. Maybe file a bug?

Follow-up: I tried it with -O (don't know why I didn't think of that earlier), and it runs fine. So maybe the debug version is just generating terrible code, initializing by iterating through 10 billion bounds checks or something?

Anyways, more importantly, it works as I would like and does not behave as a 32 bit integer. I think I understand what you mean by "constrained" now. And clearly, I was wrong.

However, if most un-suffixed integers in real-world programs will become constrained (as you claimed), this further confuses me why i32 is the unconstrained choice. It doesn't seem like something so rare could be enough of a performance problem to justify being anything but the largest supported size.

Re:Rust vs C Pitfalls by steveklabnik on Wednesday December 28, 2016 @12:14PM (#13271905)
Weird, it was fine for me with and without -O. Nothing about that code should be doing bounds checks, as it's just allocating an array.
Re:Rust vs C Pitfalls by zerofan on Wednesday December 28, 2016 @07:51PM (#13274947)
Version: rustc 1.13.0 (2c6933acc 2016-11-07)

I remember installing it by cut and pasting one of the "curl ... | sh" commands there.

> Nothing about that code should be doing bounds checks, as it's just allocating an array.

I didn't dive into the macro definition for vec!, but I assume there is a loop in there to Copy the initialization element 10 billion times. I think you guys do bounds checking on the lower level reference to a slice that Vec uses. But I really don't know. If it's not that, then it was hanging or spinning doing something else. (Debug version of your memory allocator?)

Re:Rust vs C Pitfalls by zerofan on Tuesday December 27, 2016 @08:18PM (#13267731)
> basically no numbers in real world programs weren't constrained, it was often tests, toy programs

The fact that C and C++ compilers generally chose to leave int at 32 bit on 64 bit platforms, combined with the standards requiring "usual promotions" for smaller types to go to int bites me all the time. I'm very happy that Rust dodges the promotions problem altogether, and I'm sorry if I'm wrong about the array subscripting thing (does the snippet I provided panic at 1<<32 or 1<<34?).

> And not everybody is running on 64-bit hardware, so it's a broader default.

That argument could be used to justify 8 or 16 bit integers... :-)

Re:Rust vs C Pitfalls by steveklabnik on Tuesday December 27, 2016 @08:23PM (#13267750)
> (does the snippet I provided panic at 1<<32 or 1<<34?).

Overflow is a "program error", and in debug builds, is required to panic. In other builds, if it does not panic, it's required to two's compliment overflow. Rustc currently just overflows, but in the future, we'll see.

That's true except our 16 bit support is nonexistant at the moment :)

Re:Rust vs C Pitfalls by zerofan on Tuesday December 27, 2016 @08:33PM (#13267785)
> That's true except our 16 bit support is nonexistant at the moment :)

If you pay attention to your usage statistics, I'll bet you drop 32 bit support before the May 2025 deadline we were discussing in the other thread.

Re:Rust vs C Pitfalls by steveklabnik on Tuesday December 27, 2016 @09:00PM (#13267885)
We'll see!
Re:Rust vs C Pitfalls by gpderetta on Tuesday December 27, 2016 @06:04PM (#13267029)
> But I really like function overloading, generic operators that I can overload from the left and the right, integer parameters for my templates/generics, copy semantics as the default [etc.]

I do too, and I'd like to think that rust needs all those things to be a replacement, but realistically I think that the only killer feature that rust is still missing is reasonable interoperability with C++ (which admittedly might require implementing a few of those features).

Disclaimer: I have been following rust since Graydon initial announcement, but I have yet to write a single line of code in it.

Re:Rust vs C Pitfalls by akiselev on Wednesday December 28, 2016 @02:29AM (#13269021)
What do you mean by "reasonable?" There are several crates that provide inline C++ macros on both nightly (rustcxx) and stable (rust-cpp). I haven't used the latter but the former works really well (albeit using a gcc specific feature). I usually break out the C++ code into a wrapper (unless its a few lines) to make it more idiomatic Rust but at the end of the day, there aren't that many hoops to jump through.
Re:Rust vs C Pitfalls by duneroadrunner on Wednesday December 28, 2016 @06:32AM (#13269882)
> and so I'm stuck with C++

If you're stuck with C++, might as well do it safely:

(shameless plug) https://github.com/duneroadrunner/SaferCPlusPlus

Re:Rust vs C Pitfalls by zerofan on Wednesday December 28, 2016 @07:37PM (#13274894)
I looked briefly at your code. I don't believe it is possible to be compatible with the STL and safe in the way the Rust guys intend.

I might be wrong though. What happens with your vector in this code?

    using namespace mse::mstd;
    vector<double> data(10);
    double& dangling = data[0];
    data.resize(100000);
    double crashing = dangling;
You use a lot of typedefs, so I couldn't tell for sure, but I think your operator[] returns a C++ reference right?

The problem here is there is only one operator[] for both reading and writing. This is a simple contrived example, and taking a reference like that looks artificial, but there are a lot of other ways in real programs to stumble on to this. (I don't think it's as bad as the Rusties do, but I stumble into this bug once or twice a year...)

The Rust folks seem to believe you need a borrow checker to solve this problem, but I think that a different container library in C++ could do the trick. For instance, favoring value copies instead of references, and returning a proxy object from operator[] instead of a reference.

Re:Rust vs C Pitfalls by duneroadrunner on Wednesday December 28, 2016 @09:20PM (#13275274)
Native C++ references are technically unsafe, so code that uses them would not qualify as "strict" SaferCPlusPlus code. In the case of your example, the "double&" is technically not kosher. The easiest way to make it safe would probably be to use a (safe) iterator instead of a native reference. So instead of

    double& dangling = data[0];
you could make it

    auto not_dangling_iter = data.begin();
    // not_dangling_iter += 0;
C++ references are the one unsafe element that does not have a "compatible" safe replacement. Unfortunately, you have to convert your references to pointers (or iterators). I don't think there is a way to create a "safe" reference with an interface compatible with native references. Apparently C++ will at some point add the ability to overload the dot operator, but I'm not sure that will be enough to be able to emulate C++ references.

And while I can overload the & (address of) operator to "prevent" you from getting a native pointer to a "safe" object, I don't know if there's a way to prevent you from getting a native reference. If you wanted to somehow enforce a prohibition on the use of unsafe C++ elements (like references), that would probably require some sort of static tool that is not yet available. But should be fairly straightforward to implement, I think.

But if you just want some confidence in the safety of the code you write, it doesn't take much effort to reliably avoid using C++'s unsafe elements.

Re:Rust vs C Pitfalls by duneroadrunner on Wednesday December 28, 2016 @09:34PM (#13275323)
> returning a proxy object from operator[] instead of a reference.

Oh yeah, maybe. But that would still require the ability to overload the dot operator, wouldn't it? And how would you know when to deallocate the proxy object? And presumably there would be some run-time overhead. Hmm, I don't know if it wouldn't be more practical to create a static tool (or "precompiler") to automatically convert references to (safe) pointers (or iterators).

Re:Rust vs C Pitfalls by zerofan on Wednesday December 28, 2016 @10:19PM (#13275435)
Yes, the dot operator is a headache. As soon as you march down the road of a precompiler, you're off to building a new language. I think C++'s grammar is too much of a mess to just tweak the parse tree reliably. I suspect there really isn't a way to win at this - every workaround is partial and involves compromise.
Re:Rust vs C Pitfalls by duneroadrunner on Wednesday December 28, 2016 @11:40PM (#13275658)
You know, while C++ references are technically unsafe, there is TRegisteredRefWrapper<> [1]. It's a safe version of std::reference_wrapper. Which kind of acts like a reference. So, if you don't mind me using std::strings instead of doubles, your example could be rewritten like

    mse::mstd::vector<mse::TRegisteredObj<std::string>> data(10);
    data[0] = "some text";
    mse::TRegisteredRefWrapper<std::string> dangling = data[0];
    data.resize(100000);
    try {
    	std::string crashing = dangling;
    }
    catch (...) {
    	// expected exception (not a segfault)
    	int q = 3;
    }
Does that work for you? I'm not an expert on std::reference_wrapper, so I'm not sure when it can and cannot substitute for a reference. (Btw, if it's a little verbose for you, there are shorter aliases available. Just search for "shorter aliases" in the header files.)

[1] https://github.com/duneroadrunner/SaferCPlusPlus#tregistered...

Re:Rust vs C Pitfalls by Scuds on Tuesday December 27, 2016 @05:30PM (#13266781)
Do you think we'll see major games having significant engine components being written in Rust?
Re:Rust vs C Pitfalls by mmstick on Wednesday December 28, 2016 @02:31AM (#13269026)
DICE and others have been investigating and using it to create tools for developing games. There is interest in using it within game engines, but there's just the issue of Rust support for major consoles. There is great interest in the PC gaming world though that's not constrained by console support.
Re:Rust vs C Pitfalls by gpderetta on Tuesday December 27, 2016 @06:01PM (#13267008)
I don't work in that field, but I doubt game programmers will be early adopters of rust, as I think they mostly don't care about memory safety.
Re:Rust vs C Pitfalls by Crespyl on Wednesday December 28, 2016 @12:58PM (#13272234)
There's been some interest, but I think the likes of jblows Jai language that emphasize rapid development and programmer convenience over correctness will likely catch on faster.
Re:Rust vs C Pitfalls by lacampbell on Tuesday December 27, 2016 @05:31PM (#13266786)
I appreciate rust making a break from C++ and cleaning up some of the warts, immutability by default, having real modules etc. But I am not really impressed with its memory management thing. It's a bit tiresome to check stuff with valgrind, sure, but I don't have to worry about making cyclic data structures satisfy the borrow checker. I don't see the trade off as worth it.
Re:Rust vs C Pitfalls by groovy2shoes on Tuesday December 27, 2016 @07:56PM (#13267632)
C has also been relatively stable for the last 25 years or so. Rust changes every 6 weeks.

Yes, the language is supposedly not undergoing breaking changes without strict deprecation and feature-gating. However, as new features are added to the language, libraries are usually updated to leverage them, and suddenly you are also forced to keep up with that rapid release cadence to ensure your project still builds.

Meanwhile, C code from the mid-80s still compiles with modern compilers, often without any changes whatsoever, and without any feature gates.

Don't get me wrong. I like Rust and what the project is trying to do. But I see two major problems that are preventing me from using it for anything major at the moment: 1) the extremely rapid rate of change, and 2) difficulty of implementation (both of which, taken together, lead me to expect no serious alternative implementations any time soon).

Before I will be willing to adopt Rust as a C replacement in full, there needs to be a solid, stable language definition that compiler writers can target and programmers can rely on for a good chunk of time. I have a feeling we'll get there eventually once the number of new, useful features that are missing from the language starts to dwindle and it begins to stabilize naturally.

In the meantime, I'd settle for an "LTS" release or something. That could work.

Re:Rust vs C Pitfalls by tatterdemalion on Tuesday December 27, 2016 @10:47PM (#13268292)
I don't understand. Rust 1.0 code will compile on Rust 1.14, in the same way that C code from the 80s will compile on modern C compilers. That's what "backwards compatible" means!

Your dependencies don't upgrade themselves. Its true that if you upgrade your dependencies, they may depend on a more recent verison of rustc, in which case you need to upgrade rustc as a part of upgrading that dependency. But if you are using rustup this is not harder than upgrading those dependencies - probably easier in fact.

In exchange for downloading a tarball at most every six weeks, you get a language that is developing new features.

Re:Rust vs C Pitfalls by mmstick on Wednesday December 28, 2016 @01:46AM (#13268890)
The problem with your concept is that C does not offer an official standard library and compiler suite, so you're trying to compare the C spec to the Rust standard library + compiler.

The actual Rust API has changed very little since the 1.0 release. All the changes that have occurred have been mere additions to the language -- nothing breaking. Hence why the 1.0 release was termed as a 1.0 release. There won't be anything happening that causes breakage until a 2.0 release.

You'll find that C compiler and library development is also rapid like Rust. There's new versions of LLVM/Clang and GCC being released on a regular basis. There are constant updates to musl and glibc. These changes tend to break far more often than Rust, which is developed in Rust and can be guaranteed to be much safer.

That Rust is being openly developed on GitHub with a significant number of developers, with an official compiler + library + doc + comprehensive suite of resources collectively being maintained, that's a significant incentive to work with Rust. There's a solid RFC process to prevent anything silly from entering the language. No random decisions by random people -- it takes a lot of convincing to add a new feature to Rust.

Re:Rust vs C Pitfalls by groovy2shoes on Wednesday December 28, 2016 @06:10AM (#13269807)
C does have a standard library. It's described in full in the ANSI/ISO/IEC standard for the C programming language, and it's on your system, likely called libc or something along those lines.

Anyway, I don't think that's a problem with my concept at all. What I want for Rust, and what I think will eventually be feasible for Rust as the language matures, is a standard that can be shared by compiler writers and Rust programmers. A standard is worthless if it's inadequate (see ISO Pascal or ISO Basic, for example), so it's a good thing that right now Rust is iterating quickly and improving rapidly. But eventually, there's going to need to be a stationary target for compiler writers to aim for, and for Rust programmers to be confident that the code they write today won't be littered with deprecated features, anti-patterns, and worst practices tomorrow.

Frankly, I don't see wide adoption happening in the corporate world until there's an LTS compiler at the very least.

Re:Rust vs C Pitfalls by mmstick on Wednesday December 28, 2016 @02:13PM (#13272811)
> C does have a standard library. It's described in full in the ANSI/ISO/IEC standard for the C programming language, and it's on your system, likely called libc or something along those lines.

What you're describing is the spec and not the implementation. An implementation would be glibc (GNU Libc with open source proprietary extensions) and musl (MIT implementation that strictly adheres to spec). These libraries are updated often.

Meanwhile, Rust offers more than just a specification. It offers an implementation. This is libcore-rust + libstd-rust. This is what receives updates each 6 weeks. It also offers a compiler that comes with it, rustc, which is also what receives updates each 6 weeks.

Rust strictly adheres to semantic versioning, so changes to the API are not allowed without a major version bump. Code that was written for Rust 1.0 will still compile on Rust 1.15. Code written for Rust 1.15 may not compile for a Rust 1.0 compiler though because bumping the second version implies adding new features. Rust doesn't yet utilize a patch version though. There's never been any critical issues discovered in each stable release.

> Frankly, I don't see wide adoption happening in the corporate world until there's an LTS compiler at the very least.

Can't say that I understand why a LTS compiler would be required for adoption. Many in the corporate world are already using Rust, either publicly as an official friend of Rust or privately in smaller projects. There's nothing a LTS compiler would provide that would be beneficial to the corporate world. Semantic versioning and the ease of managing Rust toolchains with rustup has everything pretty well covered.

Re:Rust vs C Pitfalls by groovy2shoes on Sunday January 01, 2017 @01:31AM (#13294905)
I can only attest to my own experiences at various corporations, and those corporations want to thoroughly vet any software platform before targeting it and supporting it for customers.

As to my own concerns (rather than corporate concerns), here's where I'm coming from:

When I wind up choosing C for a particular project, I don't use it because I like it (I don't particularly like C). Typically, I use it because there are three factors I've considered: 1) This particular project needs the low-level access or performance characteristics provided by a systems language, and 2) C is such a language, 3) C is ubiquitous.[1]

The thing is, every platform for the last 25 years or so has had a reliable ANSI C compiler. If the goal of my project is to be cross-platform, then a carefully-written application in standard C is guaranteed to be able to reach the widest number of users, many of whom won't have to do or install anything to build my application, since many environments already include a C compiler without any additional installation necessary (think Unix, BSD, a shrinking number of Linux distros, ...). Using C puts my application on the path of least resistance for potential users.

Similarly, if I'm writing a library, every language with an FFI can trivially access a library written in C. Even without an FFI, most languages can interface with C code through some kind of bindings (like the Lua-C API, for example). Again, choosing C is a path to wide adoption.

Again, I don't particularly like C as a language. However, right now, it has an undeniable advantage as a platform. I do like Rust, however, and I would love to see Rust give C the boot. In order to do that, it needs to eventually (one day when the rate of additions/changes to the language has slowed and it has grown more stable/mature) offer a platform that can be relied on to "just work" on any given platform with minimal fuss and without any sudden changes. As such, both the "end-programmer" and the compiler-writer in me would like to see some version of Rust (again, eventually) that is "set in stone" and widely available, which really would solidify that notion of stability/maturity.

I'm not advocating for Rust to stop innovating. For all I care, the "standard" can be a simple snapshot of some state of the language, the way the Haskell Reports seem to be nowadays: GHC keeps on iterating and innovating the language, yet there's still a standard updated every x years that provides a static target. But for that designated snapshot, I want to see a compiler (or two, or several) and a bunch of libraries that are supported for the long haul.

It's simply a matter of "C has this, it's an important property thereof, and any replacement will need it, too".

[1]: Some other projects demand C because they constitute contributions or extensions of other projects that are already written in C; e.g., kernel code for an existing OS that is written in C.

Re:Rust vs C Pitfalls by blub on Wednesday December 28, 2016 @07:06AM (#13269995)
When Rust (or Swift or golang) gets ISO standardized we'll know that it's mature and left the "move fast and break things" phase. This won't happen, because it's still growing now, so it doesn't make sense to standardize. They might even always want to be able to move fast. I don't see golang ever being standardized.

The fact that someone says on HN "we're backwards compatible" doesn't quite offer the same level of trust as an ISO standard. Especially when there are some conflicting reports out there i.e. there are many super-awesome libraries that only work with nightly (I think it's called), because many of those maintainers are early adopters that love to try out new features.

Programming languages and libraries are building blocks, I don't want them to change under me in the middle of a project. This is why I dropped Swift, the 1.0 -> 2.0 migration was a very painful, and from what I hear recent major version changes aren't running smooth either.

Re:Rust vs C Pitfalls by tatterdemalion on Wednesday December 28, 2016 @12:35PM (#13272069)
> Especially when there are some conflicting reports out there i.e. there are many super-awesome libraries that only work with nightly (I think it's called), because many of those maintainers are early adopters that love to try out new features.

This is totally orthogonal to whether or not Rust stable is backward compatible, which it is. People put a lot of work into making sure it is, and this sort of FUD is very frustrating to read.

Re:Rust vs C Pitfalls by mmstick on Wednesday December 28, 2016 @02:18PM (#13272850)
> The fact that someone says on HN "we're backwards compatible" doesn't quite offer the same level of trust as an ISO standard.

Then you shouldn't take the word of a random person but from the Semantic Versioning guidelines. If you're not following the semantic versioning guidelines then you're doing it wrong.

http://semver.org/

> Especially when there are some conflicting reports out there i.e. there are many super-awesome libraries that only work with nightly (I think it's called), because many of those maintainers are early adopters that love to try out new features.

Totally irrelevant. Very few libraries, if any, require a nightly compiler. The libraries that do offer features that require a nightly compiler have those features as optional. Anyway, once the new Macros update releases, the need for that nightly compiler will go away. These libraries are just taking advantage of compiler plugins.

Re:Rust vs C Pitfalls by steveklabnik on Tuesday December 27, 2016 @07:59PM (#13267646)
There's at least two alternate compilers I know of in the works. We'll see when they're in a workable state, or at least, enough to compile rustc.

> In the meantime, I'd settle for an "LTS" release or something. That could work.

This is something we've discussed. I imagine it will happen. At first, I would expect six month LTSes, then longer ones as time goes on.

Re:Rust vs C Pitfalls by groovy2shoes on Wednesday December 28, 2016 @05:11AM (#13269615)
I could definitely live with such a solution, but even six months might be too quick. So, as long as the plan is to lengthen the LTS window, I think that could work.

The company that employs me just upgraded to a C++11-capable version of GCC three months ago. Even C++'s three year cadence seems like it might be too fast! Thing is, the company wants to vet our entire platform, which itself only changes yearly. They want to know they can get bug fixes, and vetting/upgrading the compiler is a lot of work regardless, so they tend to do one and stick with it for a while. OpenBSD does the same thing for security reasons. If Rust wants to replace C and/or C++, these are things that will need consideration eventually. I don't see my company allowing Rust any time soon, unfortunately.

As a compiler-writer, I'm more concerned that Rust is a quickly-moving target at the moment. I think that as Rust becomes more mature, it will naturally begin to stabilize feature-wise, in which case something akin to the Haskell model would work well, with innovation happening in the flagship compiler and "snapshots" of the language being taken as standards periodically. That's just a matter of time, though.

Re:Rust vs C Pitfalls by blub on Wednesday December 28, 2016 @07:14AM (#13270015)
Completely agree, C++'s three year cadence is probably the fastest more conservative industries are willing to go.

And why should one invest time for such a low-level component in their toolkit every 6 months?! I want to build software and solve problems, not spend my time keeping up with programming languages and library updates.

Re:Rust vs C Pitfalls by tatterdemalion on Wednesday December 28, 2016 @12:44PM (#13272126)
I have upgraded Rust every six weeks. I run `rustup update stable` and wait a minute. Its not a time investment! Rust puts so much work into guaranteeing backwards compatibility so that updating every 6 weeks is an effortless process.

You seem to come from an experience where software updates are kinda sorta mostly backwards compatible. Rust works very hard to actually be backwards compatible. There is infrastructure to check for regressions across the entire package ecosystem, which is run regularly on HEAD and on any commit we think might potentially be breaking before we even land it.

Re:Rust vs C Pitfalls by naasking on Wednesday December 28, 2016 @01:03AM (#13268756)
> Meanwhile, C code from the mid-80s still compiles with modern compilers, often without any changes whatsoever, and without any feature gates.

You say that like it's a good thing that with C, you can ignore 30 years of progress in software development. Colour me unconvinced.

Re:Rust vs C Pitfalls by grzm on Wednesday December 28, 2016 @01:11AM (#13268775)
If the software does what you want, being able to run on 30 years worth of hardware with just a recompile is a good thing. At the end of the day, it's what the software does that's important.
Re:Rust vs C Pitfalls by naasking on Wednesday December 28, 2016 @01:22AM (#13268812)
> If the software does what you want, being able to run on 30 years worth of hardware with just a recompile is a good thing

That's not a responsible metric. 30 year old software was written against 30 year old APIs vulnerable to known attack vectors, and with 30 year old notions about security. The belief that this software is suitable for modern use is absurd IMO.

And if the types of programs you're talking about are small UNIX-like utilities, well then they're small and easily updated aren't they?

Re:Rust vs C Pitfalls by groovy2shoes on Wednesday December 28, 2016 @05:23AM (#13269659)
More often than not, brand-spanking-new software is written against 30-year-old APIs.

Lots of software we use day-to-day is old. For example, a couple years ago I fixed a 20-year-old bug in netcat, which itself hadn't been updated since the mid 90s. After I fixed the bug, I didn't need to fish out an old compiler or any compatibility libraries; I just compiled it with the latest GCC and glibc, and carried on with my day.

Don't get me wrong, I like Rust a lot and I'm excited that there's finally a real contender for the systems programming throne after all these years. I'm just less enthused about the prospect of refactoring my codebase once every few months to stay current. This is something that I think will level out with time, though. C itself was almost 20 years old before it was standardized, after all. I don't think it will take Rust that long, and I look forward to the day my copy of the Rust standard arrives on my doorstep.

Re:Rust vs C Pitfalls by naasking on Wednesday December 28, 2016 @09:30AM (#13270669)
> More often than not, brand-spanking-new software is written against 30-year-old APIs.

Let's assume this is true, brand-spanking new software will be written against 30 year old APIs that have survived 30 years of attacks and audits, but will avoid the ones that have proven vulnerable.

> I'm just less enthused about the prospect of refactoring my codebase once every few months to stay current.

The perception that you need to do this is false. You might feel pressured to refactor simply because of new syntactic improvements that make code clearer (like switching try!() macro to infix ?), but that pressure is a fiction. There's no real need to do it.

Re:Rust vs C Pitfalls by dikaiosune on Wednesday December 28, 2016 @12:47PM (#13272155)
> After I fixed the bug, I didn't need to fish out an old compiler or any compatibility libraries; I just compiled it with the latest GCC and glibc, and carried on with my day.

Could you show me an example where post-1.0 you wouldn't be able to do this with Rust?

Re:Rust vs C Pitfalls by grzm on Wednesday December 28, 2016 @01:26AM (#13268834)
If the software does what you want includes all of those criteria. If you find that there are deficiencies in any of those areas, that would qualify as a reason to make changes, weighed against the cost of doing so.
Re:Rust vs C Pitfalls by naasking on Wednesday December 28, 2016 @09:27AM (#13270647)
> If the software does what you want includes all of those criteria.

Except most people don't consider those as criteria at all. The only criteria considered are "does it run and produce the output I want".

Re:Rust vs C Pitfalls by Manishearth on Wednesday December 28, 2016 @01:41AM (#13268876)
And the plan is that you'll be able to do the same with Rust; the more rapid release strategy doesn't affect that at all.

What the rapid releases do affect is the rate at which new releases of libraries stop working on older compilers, which absolutely has to do with ignoring progress.

Re:Rust vs C Pitfalls by erikpukinskis on Wednesday December 28, 2016 @11:08AM (#13271381)
This is my same problem with JavaScript. Yes it's wonderful that changes are backwards compatible. But if the entire community is updating libraries you rely on to use new language features then you have no choice but to use those features too.

Or stop using other peoples' code and just use core language features and build everything from scratch. Which is what I end up doing, and it has some benefits. But it definitely slows you down.

Re:Rust vs C Pitfalls by Manishearth on Tuesday December 27, 2016 @11:23PM (#13268397)
Not everyone uses C only for the cases when they need a "small and portable language". C is used quite a bit in places where Rust or C++ would do fine too, for various reasons. That's where you want to compare them.
Re:Rust vs C Pitfalls by zzzcpan on Tuesday December 27, 2016 @05:48PM (#13266911)
Maybe Rust folks should make their own C backend for better integration with C compilers and have that portability, since LLVM folks decided to abandon theirs.
Re:Rust vs C Pitfalls by Ericson2314 on Tuesday December 27, 2016 @11:47PM (#13268458)
libc is very hard to use portably
Re:Rust vs C Pitfalls by gigatexal on Wednesday December 28, 2016 @01:37AM (#13268865)
Disagree. Rust seems like all the things we have been wishing C/C++ was. It's on my list of languages to learn next for sure.
Re:Rust vs C Pitfalls by steveklabnik on Tuesday December 27, 2016 @06:40PM (#13267279)
We've slowly been shifting how we talk about Rust as it evolves. I agree with you that these things matter, and they're all things we're actively working on.
Re:Rust vs C Pitfalls by zzzcpan on Tuesday December 27, 2016 @05:46PM (#13266895)
I think they are well aware of the problem at this point. Their best bet is to focus on high-performance networking software and networking ecosystem, where alternatives are still weak.
Re:Rust vs C Pitfalls by empath75 on Tuesday December 27, 2016 @05:53PM (#13266951)
It's very rare that any programmer switches computer languages. You're probably going to retire as a c programmer.

What are they going to teach in school? What are new systems programmers going to start with? I suspect it'll be rust and go, not c.

Re:Rust vs C Pitfalls by dozzie on Tuesday December 27, 2016 @06:56PM (#13267356)
> It's very rare that any programmer switches computer languages.

Or is it?

I regularly use six different languages of three paradigms and various abstraction levels, and this doesn't count DSLs nor Turing-complete tools like make. Of my colleagues from current job, half of the more experienced set write in at least three languages as well. Of the team from my previous job, all of them are polyglots. And of course none of us started with all the languages, we picked them up as needed and wanted.

Re:Rust vs C Pitfalls by gravypod on Tuesday December 27, 2016 @05:57PM (#13266980)
I'm 19, I started out with Java, I switched to JavaScript for some time, I move on to Python for which most of my projects exist in and sometimes I use C when the job counts for it. I'm also a pretty big fan of PHP and a few other technologies that are very nice.

To recap this has been my life as a programmer: Java -> JavaScript -> PHP -> Python/C/Assembly

This dream that most people have made up about programmers not switching languages is a dream. I switch whenever one language can do a task better then another. I don't get caught up in "best language" fights.

Re:Rust vs C Pitfalls by empath75 on Tuesday December 27, 2016 @06:01PM (#13267001)
Yeah but you're just getting started. Talk to me after you've been paid to be a python developer for 20 years or whatever and tell me how interested you are in learning a new language.

I play with a lot of languages but I've invested a lot of time into learning python and getting a nice python workflow going. It would take a lot for me to switch to another primary first language.

I work at a tech company that's been around since the 90s and the guys that were writing perl and java in the 90s are still writing perl and java. You'd have to drag them kicking and screaming into using a different language.

Re:Rust vs C Pitfalls by SSTitan on Tuesday December 27, 2016 @06:13PM (#13267100)
Yeah I can use several languages (C,C++,python,go,rust,x64,perl,scala,etc...) But I always envision starting a project in C or C-like C++ because I learned it first and after decades I know that language inside and out and my brain's "workflow" and my actual tool setup and personal libraries means I can crank working solutions out extremely quickly.
Re:Rust vs C Pitfalls by gravypod on Tuesday December 27, 2016 @09:41PM (#13268025)
I pick the best tool for the job. I'm using pything because right now I'm in a python shop. I'd like to go back to Java though as pythons asyncio is insane. The only reason I'm using pyhton is because the people inheriting my code will only know python.

What may look like "bah humbug I'll use X because I like it" may just be picking a language on concerns not limited to features. Looking at social concerns is important as well. Who is maintaining your code after you're gone? Company tooling for certain languages? Will I get fired for using it (AI winter)?

Lots of big questions

Re:Rust vs C Pitfalls by allengeorge on Tuesday December 27, 2016 @09:35PM (#13268007)
That's definitely not true. I've had the pleasure of working with many colleagues that have, and (since good Scala engineers are thin on the ground) my company will often hire good engineers who're interested in working with Scala. Heck - I myself have picked up Scala and Rust in the past couple of years.
Re:Rust vs C Pitfalls by sfilargi on Tuesday December 27, 2016 @05:54PM (#13266964)
> It's very rare that any programmer switches computer languages.

Do you have any data to support this claim?

Re:Rust vs C Pitfalls by tptacek on Tuesday December 27, 2016 @08:51PM (#13267854)
Not even a little bit true.
Re:Rust vs C Pitfalls by kasey_junk on Tuesday December 27, 2016 @11:08PM (#13268361)
I don't know scheme & I have this thing...

It crosses boundaries.