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

Igor Bukanov igor at mir2.org
Wed Jun 4 00:28:57 PDT 2014


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();
                    ^~~~

Note that in this case type inference does not work as removing <bool> gives:

s.rs:10:13: 10:31 error: cannot determine a type for this expression:
unconstrained type
s.rs:10     let t = Test::new().test();
                    ^~~~~~~~~~~~~~~~~~


More information about the Rust-dev mailing list