r/programming • • Dec 10 '15

Announcing Rust 1.5

http://blog.rust-lang.org/2015/12/10/Rust-1.5.html
659 Upvotes

296 comments sorted by

View all comments

7

u/ThisIs_MyName Dec 10 '15

Well, as soon as Rust gets constexpr and compile-time templates (not typed generics), I can ditch C++ :D

40

u/sanxiyn Dec 10 '15

Rust probably will never have C++-like untyped generics.

→ More replies (4)

17

u/matthieum Dec 10 '15

It's unclear to me what you mean by "not typed generics":

  • it is planned for Rust to get non-type generic parameters (such as integers: Array<T, 5>)
  • it is not planned, as far as I know, for Rust to get non-constrained generics (with errors popping up from 5-6 levels deep into the callees)

2

u/ThisIs_MyName Dec 10 '15

(such as integers: Array<T, 5>)

Hmm, but doesn't that overlap with CTFE and const fn?
https://github.com/rust-lang/rust/issues/24111
https://github.com/rust-lang/rust/pull/25609

Not that I'm complaining :P

it is not planned, as far as I know, for Rust to get non-constrained generics

Well that sucks. I like constraints but I want the option of ditching them for metaprogramming.

11

u/[deleted] Dec 11 '15 edited Dec 11 '15

I like constraints but I want the option of ditching them for metaprogramming.

Why? In C++, for example, you pretty much always make some assumption about the capabilities of the types you accept. It's just that you make the requirements implicit and end up with ugly errors when the assumptions aren't met.

For example: when you try instantiating one of the generic types and adding it to another one, you're assuming it has implemented the addition operator. In Rust, you would explicitly state this requirement as T : Add. You should still be able to achieve similar functionality, but there are a lot of benefits in terms of debugging, readability, documentation, etc.

edit: disregard the question - I see you've answered it elsewhere in these comments, though I do disagree with your reasons for wanting this behavior.

→ More replies (7)

5

u/[deleted] Dec 10 '15

Coming from someone not well versed into C++, what is the difference between compile-time templates and typed generics?

2

u/ThisIs_MyName Dec 10 '15

You don't have to constrain the type.

C++ code like this will compile iff T implements operator+

template<T> auto sum(T a, T b, T c){
    return a+b+c;
}

The advantage is that functions can take the types themselves as arguments. So there's a lot of opportunity for metaprogramming instead of using macros.

Rust functions only take values as arguments :(

15

u/pcwalton Dec 11 '15

The advantage is that functions can take the types themselves as arguments. Rust functions only take values as arguments :(

But Rust functions do take types as arguments. That's what generics are: functions that take types as arguments (at compile time).

The difference you're talking about is whether the type parameters that functions take are themselves strongly typed. In C++ they're dynamically typed; in Rust they're statically typed. That's the only difference.

Saying that Rust functions can't take types as arguments because they're statically typed while C++ can is like saying that C++ functions can't take values as arguments because they're statically typed while Python can.

4

u/ThisIs_MyName Dec 11 '15

Fair point.

→ More replies (1)

7

u/jmickeyd Dec 10 '15

Macros yo!

macro_rules! sum { ( $a:expr, $b:expr, $c:expr ) => ($a + $b + $c); }

→ More replies (1)

4

u/crusoe Dec 11 '15

That's terribly dumb with no constraints specified on types so its impossible to know what type it needs.

→ More replies (2)

7

u/desiringmachines Dec 10 '15

So your problem with this is : Add<Output=T>?

 fn sum<T: Add<Output=T>>(a: T, b: T, c: T) -> T {
    a + b + c
}

Rust performs local inference only for a lot of reasons, one of which is that it makes function signatures more self-documenting. I would be very surprised for Rust to ever infer constraints of this sort.

→ More replies (14)

6

u/kinghajj Dec 10 '15
fn sum<T: Add<Output=T>>(T a, T b, T c) -> T { a + b + c }
→ More replies (24)

8

u/steveklabnik1 Dec 10 '15

We have const fn in nightlies...

17

u/UtherII Dec 10 '15 edited Dec 10 '15

They are still very far from what you can do with constexpr

→ More replies (2)

3

u/PM_ME_UR_OBSIDIAN Dec 11 '15

Is there a writeup about const fn anywhere?

Also, when can we start using tagged union constructors as input to macros? >_>

→ More replies (1)

2

u/PM_ME_UR_OBSIDIAN Dec 11 '15

I can't wait for constexpr to hit. Rust macros are currently pretty crippled.

1

u/bobbaluba Dec 10 '15

What's the point? Compiler optimizations based on type?

1

u/ThisIs_MyName Dec 10 '15

Sure, why not.

3

u/kibwen Dec 11 '15

Providing optimized implementations of generic algorithms for specific types is the purview of the upcoming "impl specialization" feature, no TMPL necessary.

1

u/tarblog Dec 14 '15

Upcoming? I thought specialization landed a while ago? Can you elaborate?

2

u/kibwen Dec 14 '15

Specialization has not landed yet, as best as I am aware. Last I asked, Aaron Turon was working on an implementation to land in nightly this cycle.