News for hackers. Stuff that matters.
Re:Rust for C++ programmers – part 3: primitive types and op... by sisalcat on Wednesday April 23, 2014 @01:48AM (#7632355)
From the project home page: "prevents almost all crashes (in theory)"

How does it prevent index out of bounds errors and division by zero? No, not even in theory. What a ridiculous claim.

Re:Rust for C++ programmers – part 3: primitive types and op... by nrc on Wednesday April 23, 2014 @02:10AM (#7632426)
All array indexing is bounds checked, so those kind of errors are prevented. We're still working on the story around overflow checking
Re:Rust for C++ programmers – part 3: primitive types and op... by sisalcat on Wednesday April 23, 2014 @02:16AM (#7632444)
This prevents buffer overflow errors, but not crashes.
Re:Rust for C++ programmers – part 3: primitive types and op... by dbaupp on Wednesday April 23, 2014 @02:24AM (#7632475)
It prevents the OS killing a misbehaving application (which is what is meant by "crash" in that context). This allows, for example, a multithreaded server application to continue even if one worker task indexes an array incorrectly.
Re:Rust for C++ programmers – part 3: primitive types and op... by kzrdude on Wednesday April 23, 2014 @05:11AM (#7632942)
It also prevents an equally important problem: silent passing of errors. It's common for out of bounds accesses or stores in C or C++ to just pass silently, potentially corrupting data.
Re:Rust for C++ programmers – part 3: primitive types and op... by b0b_d0e on Wednesday April 23, 2014 @02:13AM (#7632433)
index out of bounds errors are handled in separate ways depending on the kind of array you are using. If I understand how it works correctly, you can be working with one of three main kinds of arrays: slices with a known length, slices with an unknown length, or a growable vector. In the case of slices with a known length, ie: let a = [0]; then trying to say a[1] is a compile error since the compiler knows the length of the array at compile time, it will not let this code compile. In the case of an unknown length at compile time, the compiler cannot really help you here, but it will cause a task failure when you try to access out of bounds (since it does know the length at runtime, it checks to make sure that it is in bounds). This is similar to what happens when you are trying to access an element that doesn't exist on growable vector as well. I hope this information clears up somethings for you and even more so I hope my information is correct! I'm still learning rust, so I still have misunderstandings quite often.
Re:Rust for C++ programmers – part 3: primitive types and op... by dbaupp on Wednesday April 23, 2014 @02:19AM (#7632453)
Indexing a fixed-length array (I.e. known at compile time) out of bounds is not prohibited at compile time. All array types trigger a task failure on out-of-bounds access (unless you explicitly opt-in to unchecked indexing).
Re:Rust for C++ programmers – part 3: primitive types and op... by guelo on Wednesday April 23, 2014 @02:40AM (#7632514)
I was going to ask if array[random_integer()] would compile only some of the time.
Re:Rust for C++ programmers – part 3: primitive types and op... by b0b_d0e on Wednesday April 23, 2014 @02:45AM (#7632534)
Oh thanks for clearing that up! Is there any specific reason why the rust compilier currently doesn't check that a static int accessing into a known length slice doesn't result in a compile error? (I'm only referring to cases where the index you are trying to access is known at compile time as well as the length of the slice)
Re:Rust for C++ programmers – part 3: primitive types and op... by nikbackm on Wednesday April 23, 2014 @03:31AM (#7632651)
It would be inconsistent and not really that helpful. How often do you use (invalid) constant indexes with fixed length arrays?

Probably better to handle all out-of-bounds errors the same way.

Re:Rust for C++ programmers – part 3: primitive types and op... by sisalcat on Wednesday April 23, 2014 @02:16AM (#7632439)
> it will cause a task failure

i.e. a crash.

Re:Rust for C++ programmers – part 3: primitive types and op... by Jweb_Guru on Wednesday April 23, 2014 @02:49AM (#7632545)
Not at all. Tasks can (and do) die without killing the main program. They also don't segfault, and as the stack unwinds destructors get called (which Rust can actually ensure is safe). If you've ever had the pleasure of dealing with a framework that likes to send SIGKILL indiscriminately to important programs, you'll appreciate the difference between the two.
Re:Rust for C++ programmers – part 3: primitive types and op... by coldtea on Wednesday April 23, 2014 @10:31AM (#7634148)
No. Something recoverable, like an exception.

But it seems you have your mind set, and wont accept any other answer.

Re:Rust for C++ programmers – part 3: primitive types and op... by dbaupp on Wednesday April 23, 2014 @02:21AM (#7632459)
It doesn't necessarily take down the whole program, ice. it's recoverable, unlike memory corruption or a segfault.

(Yes I know you can install signal handlers for segv, but that is not isolated at a language level like a task failure is with Rust.)

Re:Rust for C++ programmers – part 3: primitive types and op... by sisalcat on Wednesday April 23, 2014 @02:24AM (#7632473)
Yes, I can catch SIGSEGV. And I can catch Java NullPointerExceptions and ArrayIndexOutOfBoundsExceptions. So Java prevents all crashes, lol.
Re:Rust for C++ programmers – part 3: primitive types and op... by dbaupp on Wednesday April 23, 2014 @02:26AM (#7632479)
Yes, Java applications are rarely killed by the OS/memory manager for trying to access memory they don't have control over.
Re:Rust for C++ programmers – part 3: primitive types and op... by azth on Wednesday April 23, 2014 @05:20AM (#7632974)
Don't argue with him/her, the parent poster is clearly acting immature.
Re:Rust for C++ programmers – part 3: primitive types and op... by sisalcat on Wednesday April 23, 2014 @02:35AM (#7632502)
That's a strange definition of "crash" you have there. I should go tell my customers next time that their programs definitely didn't crash.
Re:Rust for C++ programmers – part 3: primitive types and op... by alkonaut on Wednesday April 23, 2014 @03:55AM (#7632715)
Java raises an exception. If you handle it in code, the app does not crash. The crash is a consequence of failure to handle and recover from the exception.

Granted, it is very rare that you can/should recover from nullpointer or OOB exceptions anyway. What you can do though is try to clean up things, show a polite message and shut down the application in a semi-controlled way.

"Preventing crashes" isn't the best description of Rust's novel protection systems. In my opinion, the best part is providing guarantees about data integrity and security, e.g. preventing heartbleed type read-overruns and preventing data races in concurrent code due to shared mutable state.

Re:Rust for C++ programmers – part 3: primitive types and op... by AlisdairO on Wednesday April 23, 2014 @06:20AM (#7633104)
It's not that uncommon to want to recover from NPEs in a webapp environment. You want your thread of execution to error out, return a 500, and rollback whatever db changes it's made, but you want the webapp as a whole to keep running.
Re:Rust for C++ programmers – part 3: primitive types and op... by lucian1900 on Wednesday April 23, 2014 @09:11AM (#7633690)
And this is precisely what task failure in Rust lets you do. Requests run in tasks, if the task fails, show an error message and make a new task for the next request.
Re:Rust for C++ programmers – part 3: primitive types and op... by AlisdairO on Wednesday April 23, 2014 @11:11AM (#7634421)
Yes, sorry - I wasn't meaning to state that rust is bad in this regard. Just addressing the point of catching NPEs and so on.
Re:Rust for C++ programmers – part 3: primitive types and op... by alkonaut on Wednesday April 23, 2014 @10:52AM (#7634281)
Of course when you have a web application running requests using some thread pool or even more lightweight worker, then what we call the "application" is likely the request and you most likely don't want to kill the app server because you have good reason to believe the other requests are unaffected by the failing requests.
Re:Rust for C++ programmers – part 3: primitive types and op... by coldtea on Wednesday April 23, 2014 @10:36AM (#7634173)
>Granted, it is very rare that you can/should recover from nullpointer or OOB exceptions anyway.

Actually it can be quite common, especially in complex web / network programs with many components. In Java such errors do not corrupt memory like in C, and since they are isolated, no reason not to continue the operation of the overall program. The error could just be due to a resource not being found, or faulty input coming from outside -- that is, nothing that prevents you from continuing to work on other requests.

That's somewhat like Erlang handles the case, if I am not mistaken.

Re:Rust for C++ programmers – part 3: primitive types and op... by adamnemecek on Wednesday April 23, 2014 @01:55AM (#7632375)
I mean you get an error but not a segfault. Rust crashes on division by zero but honestly, how many crashes have you encountered in the real world due to division by zero?
Re:Rust for C++ programmers – part 3: primitive types and op... by pbsd on Wednesday April 23, 2014 @02:02AM (#7632402)
Probably quite a few. There's also more than just division by 0 to be wary of: http://blog.cmpxchg8b.com/2013/02/the-other-integer-overflow...
Re:Rust for C++ programmers – part 3: primitive types and op... by dbaupp on Wednesday April 23, 2014 @02:16AM (#7632440)
You get a task failure (similar to an exception) on division by 0 too.
Re:Rust for C++ programmers – part 3: primitive types and op... by berkut on Wednesday April 23, 2014 @04:15AM (#7632771)
Quite a few if you include what happens after a division by zero without proper checking / clamping.

Had one the other day when renormalising a Cumulative distribution function wasn't taking into account a possible empty set, and the lookup of the index was invalid due to the division by zero.