Rust 中使用 :: 这种语法的几种情况

文章目录

        • 1. 访问模块成员:
        • 2. 访问关联函数或静态方法:
        • 3. 访问 trait 的关联类型或关联常量
        • 4. 指定泛型类型参数

1. 访问模块成员:
mod utils {
    pub fn do_something() { /* ... */ }
}

let result = utils::do_something();
2. 访问关联函数或静态方法:
struct MyStruct;

impl MyStruct {
    fn new() -> Self {
        MyStruct
    }
}

let instance = MyStruct::new();
3. 访问 trait 的关联类型或关联常量
trait MyTrait {
    type Output;
    const VALUE: u32 = 42;
}

impl MyTrait for i32 {
    type Output = String;
}

let value = <i32 as MyTrait>::VALUE;
4. 指定泛型类型参数
fn test3<T: AsRef<str>>(s: T) -> T {
    s
}

let result = test3::<&str>("hello");

你可能感兴趣的:(rust,rust,区块链)