1. 程式人生 > 實用技巧 >Rust學習筆記1

Rust學習筆記1

View Code

這段程式碼大概相當於c#裡這樣的:

 1 using System;
 2 
 3 namespace ConsoleApp1
 4 {
 5     class Program
 6     {
 7         static void Main(string[] args)
 8         {
 9             Console.WriteLine("請輸入你猜的數字。");
10             int guess = new Random().Next(1, 101);
11             while (true)
12             {
13 string input = Console.ReadLine(); 14 if (int.TryParse(input, out int num)) 15 { 16 if (num > guess) 17 { 18 Console.WriteLine("高了"); 19 } 20 else
if (num < guess) 21 { 22 Console.WriteLine("低了"); 23 } 24 else 25 { 26 Console.WriteLine("猜對了!"); 27 break; 28 } 29 }
30 else 31 { 32 Console.WriteLine("請輸入數字!"); 33 continue; 34 } 35 } 36 37 } 38 } 39 }
View Code

這是學習《Rust程式語言》第二章的內容記錄