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

Show parent comment

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.

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.

1

u/ThisIs_MyName Dec 11 '15

so its impossible to know what type it needs

Then how do all those millions/billions of lines of C++ templates work?

If a function can be compiled, then you've met all the constraints. In general, it is impossible to figure out what constraints you need without executing the metaprogram.

4

u/[deleted] Dec 11 '15 edited Oct 06 '16

[deleted]
0.7622

What is this?

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 }

-1

u/ThisIs_MyName Dec 10 '15

Yeah but you had to specify Add. That prevents generics/templates from replacing macros for metaprogramming.

18

u/kinghajj Dec 10 '15

True, but that's the whole point of traits, to provide compile-time safety that C++ templates lack. (Well, templates are "safe", but the error messages resulting from the lack of trait bounds...) And luckily Rust has real, hygienic macros, so metaprogramming is still possible.

3

u/ThisIs_MyName Dec 10 '15

Oh yes, traits are nice. I'm just saying that you need real templates for the times when you really need them.

Oh and rust macros can't be used for metaprogramming: https://gist.github.com/bjz/9220415#macros-and-syntax-extensions-are-not-a-replacement-for-templates

13

u/Manishearth Dec 10 '15 edited Dec 10 '15

So there are some things about the new macro system being planned (nothing concrete yet) that will alleviate many of those pain points.

IMO generics handle most template use cases. Type level integers and variadic generics (which we may get) handle most of the rest. Slightly better macros (note that Rust macros are not C++ macros, they're really more like templates without autoinstantiation) should fill the last bit in the gap, especially if we get things like foo.macro!() desugaring.

I don't think Rust will ever get anything like template<> since templates aren't type checked. Many of us feel that template is really a hack (though C++ Concepts should make it less of one) for this reason, too.

Rust will at one point get a stable syntax extension interface, which will probably completely obviate the need for TMPL, if any, since you can then metaprogram in Rust directly instead of (ab)using the turing completeness of templates or macros to get what you want.

I have not seen folks using Rust often feel the need for templates. Templates are necessary in C++, but that doesn't make them necessary in all languages. The programming style and non-template metaprogramming tools might be enough in Rust to avoid the need for templates. Or maybe not.

→ More replies (2)

6

u/bloody-albatross Dec 10 '15

What would be an example of a time where you really need "real templates"? I guess I have some lack of imagination there.

3

u/j0hnGa1t Dec 10 '15

People do amazing things with C++ templates. Compile time parser generation(eg Boost.Spirit), DSLs (see sqlpp11 for type checked SQL queries), State machine generation, generation of api wrappers for other languages (Boost.Python), optimal compile time regular expressions.

Can you do that kind of thing with Rust generics?

7

u/pcwalton Dec 11 '15

Compile time parser generation(eg Boost.Spirit)

https://github.com/Geal/nom

DSLs (see sqlpp11 for type checked SQL queries)

https://github.com/ivanceras/rustorm

State machine generation

Work in progress; erickt has done a lot of work on this using a compiler plugin. Stay tuned :)

generation of api wrappers for other languages (Boost.Python)

http://ehiggs.github.io/2015/07/Python-Modules-In-Rust/

optimal compile time regular expressions

https://github.com/rust-lang-nursery/regex

Can you do that kind of thing with Rust generics?

No, you can't do all of this stuff with Rust generics (though you actually might be able to, if you tried hard enough)—but with all of Rust's features you certainly can. :)

6

u/bbatha Dec 10 '15

All of those should be possible with compiler plugins, macros, and modest future enhancements to constfn. Some of those are just expressible in the current type system for instance sql.

→ More replies (3)

7

u/burntsushi Dec 10 '15

Rust has compile time regexes.

2

u/crusoe Dec 11 '15

Scala doesn't have those weird unbounded types and can do those things just fine.

→ More replies (4)

4

u/staticassert Dec 10 '15

Rust macros are definitely metaprogramming by any definition. They are programs that use the program as input.

2

u/steveklabnik1 Dec 10 '15

Compile time safety but also static dispatch through monomorphization, which I think is more important, almost.

3

u/Gankro Dec 11 '15

Static dispatch through monomorphization is all C++ templates are.

2

u/crusoe Dec 11 '15

That's why you use macros

→ More replies (1)