A good "plus" for rust here is that in general, the idiomatic way to access
arrays in a loop doesn't use bounds checking. For example, to write a dot
product in c++ you'd do this:

double dot(const double* x, const double* y, int len) {
  double result = 0.0;
  for(int i = 0; i < len; ++i)
    result += x[i]*y[i];
  return result;
}

This isn't very safe, and you don't pay for bounds checks. Fair enough. Now
in rust:

fn dot(x: &Vec<double>, y: &Vec<double>) -> double {
  x.iter().zip(y.iter()).fold(0.0, |k, (x, y)| k + x*y)
}


That's safe, and doesn't bounds check. Potential slowness of the closure
aside, this should be just as efficient. In general, your loops won't
directly index arrays.

  - Clark


On Fri, Mar 28, 2014 at 8:31 AM, Patrick Walton <pcwal...@mozilla.com>wrote:

> On 3/28/14 5:25 AM, Tommi wrote:
>
>> On 28 Mar 2014, at 05:56, Patrick Walton <pcwal...@mozilla.com> wrote:
>>
>>>
>>> I think that Rust should give you the ability to opt out of safety, but
>>> on a per-operation basis. Having it as a compiler option is too much of a
>>> sledgehammer: often you want some non-performance-critical bounds to be
>>> checked in the name of safety, while you want some bounds checks to be
>>> turned off.
>>>
>>
>> One other argument I can give for a "sledgehammer" feature like this
>> is that it can be used as a marketing tool against people who are
>> worried about performance. You can say to those people: "Look, if, at
>> the end of the day, you decide that you'd rather take raw speed over
>> safety, then there's this compiler flag you can use to disable all
>> runtime memory safety checking in your code and get performance on
>> par with C++".
>>
>
> Well, people who would be persuaded by that would probably also want the
> borrow check off, and might want the mutability restrictions off too. It
> just wouldn't be the same language.
>
> It might be interesting to try to design a systems language with no regard
> for safety and the other modern/functional programming goodies that Rust
> has (concepts/traits, algebraic data types, a module system), but Rust just
> isn't that language. Safety-by-default is core to its design.
>
> Patrick
>
>
> _______________________________________________
> Rust-dev mailing list
> Rust-dev@mozilla.org
> https://mail.mozilla.org/listinfo/rust-dev
>



-- 
Clark.

Key ID     : 0x78099922
Fingerprint: B292 493C 51AE F3AB D016  DD04 E5E3 C36F 5534 F907
_______________________________________________
Rust-dev mailing list
Rust-dev@mozilla.org
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to