Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

rust

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?

like image 712
Henning Koehler Avatar asked Mar 15 '26 11:03

Henning Koehler


1 Answers

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

like image 108
Akiner Alkan Avatar answered Mar 18 '26 01:03

Akiner Alkan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!