[rust-dev] syntax for explicit generics when calling static method

Sebastian Gesemann s.gesemann at gmail.com
Wed Jun 4 00:52:57 PDT 2014


On Wed, Jun 4, 2014 at 9:28 AM, Igor Bukanov wrote:
> What is the syntax for calling a static method of a generic struct
> while selecting the the generic parameters explicitly? Apparently
> Struct<Type>::static_method does not work. For example, consider the
> following program:
>
> #[deriving(Show)]
> struct Test<T> { i: int }
>
> impl<T> Test<T> {
>     fn new() -> Test<T> { Test {i: 1} }
>     fn test(&self) -> int { self.i }
> }
>
> fn main() {
>     let t = Test<bool>::new().test();
>     println!("t={}", t);
> }
>
> The latest nightly compiler generates:
>
> s.rs:10:13: 10:17 error: `Test` is a structure name, but this
> expression uses it like a function name
> s.rs:10     let t = Test<bool>::new().test();
>                     ^~~~

Not sure if it helps because I can't test it right now and I'm still a
beginner, but here it goes: Try inserting a :: between Test and
<bool>. AFAIU this is sometimes needed for disambiguation. Otherwise
the compiler might think < is a less-than operator.

Cheers!
sg


More information about the Rust-dev mailing list