Rust - 輸入輸出



本章討論如何從標準輸入(鍵盤)接受值並向標準輸出(控制檯)顯示值。在本章中,我們還將討論傳遞命令列引數。

讀取器和寫入器型別

Rust 的標準庫中用於輸入和輸出的功能圍繞兩個特徵組織:

  • 讀取
  • 寫入
序號 特徵 & 描述 示例
1

讀取

實現 Read 的型別具有面向位元組的輸入方法。它們被稱為讀取器。

標準輸入,檔案
2

寫入

實現 Write 的型別支援面向位元組和 UTF-8 文字輸出。它們被稱為寫入器。

標準輸出,檔案

Read 特徵

讀取器是程式可以從中讀取位元組的元件。例如,從鍵盤、檔案等讀取輸入。此特徵的 read_line() 方法可用於一次讀取一行資料,來自檔案或標準輸入流。

序號 特徵 方法 & 描述
1 讀取

read_line(&mut line)->Result

讀取一行文字並將其附加到 line,line 是一個字串。返回值是 io::Result,讀取的位元組數。

示例 - 從控制檯讀取 - stdin()

Rust 程式可能需要在執行時接受使用者的輸入。以下示例從標準輸入(鍵盤)讀取值並將其列印到控制檯。

fn main(){
   let mut line = String::new();
   println!("Enter your name :");
   let b1 = std::io::stdin().read_line(&mut line).unwrap();
   println!("Hello , {}", line);
   println!("no of bytes read , {}", b1);
}

stdin() 函式返回當前程序的標準輸入流的控制代碼,read_line 函式可以應用於該控制代碼。此函式嘗試在遇到換行符時讀取輸入緩衝區中存在的所有字元。

輸出

Enter your name :
Mohtashim
Hello , Mohtashim
no of bytes read , 10

Write 特徵

寫入器是程式可以向其寫入位元組的元件。例如,將值列印到控制檯,寫入檔案等。此特徵的 write() 方法可用於將資料寫入檔案或標準輸出流。

序號 特徵 方法 & 描述
1 寫入

write(&buf)->Result

將切片 buf 中的一些位元組寫入底層流。它返回 io::Result,寫入的位元組數。

示例 - 寫入控制檯 - stdout()

print!println! 宏可用於在控制檯上顯示文字。但是,您也可以使用 write() 標準庫函式將一些文字顯示到標準輸出。

讓我們考慮一個示例來理解這一點。

use std::io::Write;
fn main() {
   let b1 = std::io::stdout().write("Tutorials ".as_bytes()).unwrap();
   let b2 = std::io::stdout().write(String::from("Point").as_bytes()).unwrap();
   std::io::stdout().write(format!("\nbytes written {}",(b1+b2)).as_bytes()).unwrap();
}

輸出

Tutorials Point
bytes written 15

stdout() 標準庫函式返回當前程序的標準輸出流的控制代碼,write 函式可以應用於該控制代碼。write() 方法返回一個列舉,Result。unwrap() 是一個輔助方法,用於從列舉中提取實際結果。如果發生錯誤,unwrap 方法將傳送 panic。

注意 - 檔案 IO 在下一章中討論。

命令列引數

命令列引數在執行程式之前傳遞給程式。它們就像傳遞給函式的引數。命令列引數可用於將值傳遞給 main() 函式。std::env::args() 返回命令列引數。

示例

以下示例將值作為命令列引數傳遞給 main() 函式。程式在名為 main.rs 的檔案中建立。

//main.rs
fn main(){
   let cmd_line = std::env::args();
   println!("No of elements in arguments is :{}",cmd_line.len()); 
   //print total number of values passed
   for arg in cmd_line {
      println!("[{}]",arg); //print all values passed 
      as commandline arguments
   }
}

編譯後,程式將生成一個名為 main.exe 的檔案。多個命令列引數應以空格分隔。從終端執行 main.exe,例如 main.exe hello tutorialspoint

注意 - hellotutorialspoint 是命令列引數。

輸出

No of elements in arguments is :3
[main.exe]
[hello]
[tutorialspoint]

輸出顯示 3 個引數,因為 main.exe 是第一個引數。

示例

以下程式計算作為命令列引數傳遞的值之和。將以空格分隔的整數列表傳遞給程式。

fn main(){
   let cmd_line = std::env::args();
   println!("No of elements in arguments is 
   :{}",cmd_line.len()); 
   // total number of elements passed

   let mut sum = 0;
   let mut has_read_first_arg = false;

   //iterate through all the arguments and calculate their sum

   for arg in cmd_line {
      if has_read_first_arg { //skip the first argument since it is the exe file name
         sum += arg.parse::<i32>().unwrap();
      }
      has_read_first_arg = true; 
      // set the flag to true to calculate sum for the subsequent arguments.
   }
   println!("sum is {}",sum);
}

在執行程式 main.exe 1 2 3 4 時,輸出將為:

No of elements in arguments is :5
sum is 10
廣告

© . All rights reserved.