News for hackers. Stuff that matters.
Re:Getting Past C by staticassertion on Tuesday January 03, 2017 @06:36PM (#13314347)
Panics do not violate memory safety. In C that code would be undefined behavior.
Re:Getting Past C by EugeneOZ on Tuesday January 03, 2017 @07:26PM (#13314601)
You are right, my mistake. I just can't call code failing in runtime "safe" (because I can't rely on it). But in terms of memory safety it's safe.
Re:Getting Past C by moosingin3space on Tuesday January 03, 2017 @07:30PM (#13314625)
Then use the get() function on an array/slice. It returns an Option so it won't perform an access off the end of the array, and you can catch it.
Re:Getting Past C by EugeneOZ on Tuesday January 03, 2017 @07:35PM (#13314657)
thanks, I do. Only thing left is explain it to everybody else to avoid panicking in 3-party crates. I really hate when people use panicking just because it's easier than error handling. So it was "surprise" to see such behavior from "[x]" construction.
Re:Getting Past C by cesarb on Wednesday January 04, 2017 @06:44AM (#13317435)
Like in C, you are not supposed to pass out-of-bounds indexes to the [] operator. If anyone does so, it's a bug. Rust converts it to a panic, which safely terminates the thread/program, instead of C's undefined behavior.

You can also catch this panic at the thread boundary, so other threads in your program can keep running, unless the program was compiled to call abort() on panic.

Re:Getting Past C by EugeneOZ on Wednesday January 04, 2017 @06:54AM (#13317493)
You still think in C limits of possibilities. Rust could just return Option as a result for [x] syntax.
Re:Getting Past C by cesarb on Wednesday January 04, 2017 @07:21AM (#13317638)
And then people would simply call .unwrap() on the return of the [] operator, leading to the same situation. It's extremely common for an array dereference to always be within the bounds, by code construction (I'm iterating over the indexes of the array, or I have an index into the array saved inside some other structure, and so on). The programmer knows it will never be out of bounds. The assert!() within the [] operator is only to protect you when the programmer gets it wrong, and in a correctly written program, will never trigger.

(The alternative instead of .unwrap() would be to propagate the error, polluting the whole program with code to handle errors which never will happen. And since they will never happen, many programmers would simply start ignoring them - and in the process, they would by accident end up ignoring errors that can happen. Not a good situation.)

Re:Getting Past C by EugeneOZ on Wednesday January 04, 2017 @08:39AM (#13318121)
There is HUGE difference between "unwrap" (which can found by grep) and mutual dangerous behavior. Programmer can only assume, not know, without additional checks. "Polluting with error" is much better than panicking.
Re:Getting Past C by EugeneOZ on Wednesday January 04, 2017 @02:03PM (#13321075)
And for safe iterations Rust has "for .. in".