Rust 有問有答之 use 關鍵字

2021-10-06 12:42:35 字數 1145 閱讀 1378

use 是 rust 程式語言的關鍵字。using 是 程式語言 c# 的關鍵字。

關鍵字是預定義的保留識別符號,對編譯器有特殊意義。

using關鍵字有三個主要用途:

模擬using,use的用途有:

外部模組 a.rs,**內容如下

mod a

}

主函式 main.rs 想要呼叫 print_function,需要對 mod 標識訪問級別,使用關鍵字 pub。所以 a.rs 的內容變動如下

pub mod a

}

主函式 main.rs 呼叫 print_function 如下,使用關鍵字 use:

use a;

fn main()

enum status 

fn main()

; let status = poor;

}

上述**使用關鍵字use顯示宣告了列舉 status,所以在let status = poor;這行**中無需使用status::poor手動加上作用域的方式宣告poor

當然如果列舉值過多時,可以使用*宣告所有列舉值,即use status::*;

pub mod a 

pub fn other_funtion()

}}use a::b as ab;

use a::b::other_funtion as ab_funtion;

fn main()

如上述例子所示

use a::b as ab;使用關鍵字 use 為作用域建立別名。

use a::b::other_funtion as ab_funtion;為方法 other_funtion 建立別名 ab_funtion 。

參考:

php 中use關鍵字的用法

use最常用在給類取別名 use還可以用在閉包函式中,如下 php view plain copy function test b test b world 結果是hellohello 當執行test函式,test函式返回閉包函式,閉包函式中的use中的變數為test函式中的 a變數,當執行閉包函式...

PHP 關鍵字 use 的使用方法

比方說建三個檔案。第乙個檔案 a.php,裡邊有兩個類,命名空間是 a b c namespace a b c function get info class c 第二個檔案 b.php 命名空間 a b d namespace a b d function get info 比方說我們現在想例項化...

MySQL筆記3 USE和SHOW關鍵字

在你最初連線到mysql時,沒有任何資料庫開啟供你使用。在你能執行任意資料庫操作前,需要選擇乙個資料庫。為此,可使用use關鍵字。p.s.關鍵字 key word 作為mysql語言組成部分的乙個保留字。決不要用關鍵字命名乙個表或列 1 返回可用資料庫的乙個列表,可輸入 2 為了使用crashcou...