Henning Koehler
Henning Koehler

Reputation: 2657

Rust: specify template arguments during "use ... as" import

I'm trying to specify a template parameter of an imported class, so that I don't need to specify it each time I want to use it. Something like this:

use self::binary_heap_plus::BinaryHeap<T,MinComparator> as BinaryMinHeap<T>;

Is this possible?

Upvotes: 3

Views: 72

Answers (1)

Akiner Alkan
Akiner Alkan

Reputation: 6918

Is this possible?

Yes it is possible like following:

pub type CustomResult<T> = Result<T, MyError>;

#[derive(Debug)]
pub enum MyError {
    MyError1,
}

fn result_returner(prm: i32) -> CustomResult<i32> {
    if prm == 1 {
        Ok(5)
    } else {
        Err(MyError::MyError1)
    }
}

And also you can make such like type name changings on import as well:

use std::collections::HashMap as CustomNamedMap;

fn main() {
    let mut my_map = CustomNamedMap::new();
    my_map.insert(1, 2);

    println!("Value: {:?}", my_map[&1]);
}

Playground

Upvotes: 3

Related Questions