2.計算字元數
我的實現
一個滿足題意的實現
use std::io::{self, Write};
fn main() {
print!("What is the input string?");
io::stdout().flush().unwrap();
let mut input = String::new();
std::io::stdin().read_line(&mut input).expect(
"Failed to read line",
);
println!("{} has {} characters.", input, input.len());
}
學到了什麼
您可以通過增加一個迴圈來保證使用者的確輸入了內容。
use std::io::{self, Write};
fn main() {
print!("What is the input string?");
io::stdout().flush().unwrap();
let mut input = String::new();
std::io::stdin().read_line(&mut input).expect(
"Failed to read line",
);
while input.len() == 0 {
std::io::stdin().read_line(&mut input).expect(
"Failed to read line",
);
}
println!("{} has {} characters.", input, input.len());
}
當然這也不完全是一個好的實踐。
use std::io::{self, Write};
fn main() {
print!("What is the input string?");
io::stdout().flush().unwrap();
let mut input = String::new();
loop {
std::io::stdin().read_line(&mut input).expect(
"Failed to read line",
);
if input.len() != 0 {
break;
}
println!("Please enter the content.");
}
println!("{} has {} characters.", input, input.len());
}
這裡使用了 loop
迴圈達到了一種好的實踐,因為到現在為止 Rust 中還沒有像 C 語言中那樣的 do
迴圈。後面的兩個程式都不能在 play 裡面得到滿意的結果。因為 play 裡面不能輸入。所以會導致死迴圈。
相關文章
- 2. 計算機怎麼表示數字計算機
- Dictionary計算字元出現的次數字元
- C語言:計算輸入字元的個數C語言字元
- C語言計算輸入字元的個數C語言字元
- JavaScript實時計算輸入文字框字元數量JavaScript字元
- 如何計算字串某個字元出現的次數字串字元
- 字元數統計字元
- 29:統計字元數字元
- python常見面試題講解(二)計算字元個數Python面試題字元
- js如何計算一個字元在字串中出現的次數JS字元字串
- 統計字串字元個數字串字元
- Linux下計算字元在檔案裡出現的次數Linux字元
- 040統計數字字元的個數字元
- 利用HashMap統計字元個數HashMap字元
- 計算機字元編碼的前世今生計算機字元
- 2.刪除字串中的某個字元。(藉助字元陣列實現)字串字元陣列
- Shell 程式設計 : 數值,字元,字串程式設計字元字串
- 計算機字元編碼詳盡講解計算機字元
- 計算素數【Java】Java
- Octave 數值計算
- JavaScript統計字元出現的次數JavaScript字元
- JS演算法——統計字元數量JS演算法字元
- linux shell中運算字元與數字的問題Linux字元
- LeetCode 2.兩數相加LeetCode
- leetcode 2. 兩數相加LeetCode
- windows store app 如何計算字元所佔的寬度WindowsAPP字元
- 計算返回單、雙位元組字元混合字串長度字元字串
- 如何用Python統計不同字元個數?Python字元
- C語言: 分類統計字元個數C語言字元
- JavaScript統計字串中字元出現次數JavaScript字串字元
- 計算Linux天數Linux
- 看板數量的計算
- 數值計算基礎
- 計算記錄個數
- 計算總頁數公式公式
- java計算間隔天數Java
- Java浮點數計算Java
- 2. 湍流的統計描述