Posted Nervosfans

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了相关的知识,希望对你有一定的参考价值。

鏂囩珷鏈夌偣闀匡紝灞炰簬rust璇█寮€鍙戠▼搴忕殑璇︾粏鏁欑▼锛屽浜巖ust鏂版墜鑰岃█锛岄渶瑕佽姳鐐规椂闂寸爺璇伙紝鑰愬績鎸佺画瀛︿範锛屽繀鏈夋敹鑾峰摝 馃槃


璁╂垜浠竴璧峰姩鎵嬬敤rust瀹屾垚涓€涓」鐩紝鏉ュ揩閫熶笂鎵?棰嗕細Rust璇█鐨勭壒鑹诧紒杩欎袱澶╀竴鐩村湪浠嬬粛 Rust 涓父鐢ㄧ殑涓€浜涙蹇碉紝骞堕€氳繃鐪熷疄鐨勭▼搴忔潵灞曠ず濡備綍杩愮敤瀹冧滑銆?/p>

 

鎴戜滑浼氬疄鐜颁竴涓粡鍏哥殑鏂版墜缂栫▼闂锛氱寽鐚滅湅娓告垙銆傚畠鏄繖涔堝伐浣滅殑锛氱▼搴忓皢浼氶殢鏈虹敓鎴愪竴涓?1鍒?100 涔嬮棿鐨勯殢鏈烘暣鏁般€傛帴鐫€瀹冧細璇风帺瀹剁寽涓€涓暟骞惰緭鍏ワ紝鐒跺悗鎻愮ず鐚滄祴鏄ぇ浜嗚繕鏄皬浜嗐€傚鏋滅寽瀵逛簡锛屽畠浼氭墦鍗扮璐轰俊鎭苟閫€鍑恒€?/p>

 

鍑嗗涓€涓柊椤圭洰鍙婄洰褰?/p>

瑕佸垱寤轰竴涓柊椤圭洰锛岃繘鍏ョ涓€绔犱腑鍒涘缓鐨?projects 鐩綍锛屼娇鐢?Cargo 鏂板缓涓€涓」鐩紝濡備笅锛?/p>

 

$ cargo new guessing_game --bin

$ cd guessing_game


绗竴涓懡浠わ紝cargo new锛屽畠鑾峰彇椤圭洰鐨勫悕绉帮紙guessing_game锛変綔涓虹涓€涓弬鏁般€?-bin 鍙傛暟鍛婅瘔 Cargo 鍒涘缓涓€涓簩杩涘埗椤圭洰銆傜浜屼釜鍛戒护杩涘叆鍒版柊鍒涘缓鐨勯」鐩洰褰曘€?/p>

 

鐪嬬湅鐢熸垚鐨?Cargo.toml 鏂囦欢锛?/p>

 

鏂囦欢鍚? Cargo.toml

 

[package]

name = "guessing_game"

version = "0.1.0"

authors = ["Your Name<you@example.com>"]

 

[dependencies]

濡傛灉 Cargo 浠庣幆澧冧腑鑾峰彇鐨勫紑鍙戣€呬俊鎭笉姝g‘锛屼慨鏀硅繖涓枃浠跺苟鍐嶆淇濆瓨銆?/p>

 

cargo new 鐢熸垚浜嗕竴涓?鈥淗ello, world!鈥?绋嬪簭銆?/p>

鏌ョ湅 src/main.rs 鏂囦欢锛?/p>

 

鏂囦欢鍚? src/main.rs

 

fn main() {

   println!("Hello, world!");

}

鐜板湪缂栬瘧 鈥淗ello, world!鈥?绋嬪簭锛屼娇鐢?cargo run 缂栬瘧杩愯涓€姝ュ埌浣嶏細

 

$ cargo run

  Compiling guessing_game v0.1.0 (file:///projects/guessing_game)

    Running `target/debug/guessing_game`

Hello, world!


run 鍛戒护閫傚悎鐢ㄤ簬闇€瑕佸揩閫熻凯浠g殑椤圭洰锛岃€岃繖涓父鎴忎究鏄繖鏍风殑椤圭洰锛氭垜浠渶瑕佸湪涓嬩竴姝ヨ凯浠d箣鍓嶅揩閫熸祴璇曟瘡涓€姝ャ€?/p>

 

閲嶆柊鎵撳紑 src/main.rs 鏂囦欢銆傛垜浠皢浼氬湪杩欎釜鏂囦欢涓紪鍐欏叏閮ㄧ殑浠g爜銆?/p>

 

澶勭悊涓€娆$寽娴?/p>

绋嬪簭鐨勭涓€閮ㄥ垎璇锋眰鍜屽鐞嗙敤鎴疯緭鍏ワ紝骞舵鏌ヨ緭鍏ユ槸鍚︾鍚堥鏈熺殑鏍煎紡銆傞鍏堬紝鍏佽鐢ㄦ埛杈撳叆鐚滄祴銆傚湪 src/main.rs 涓緭鍏ョず渚?濡備笅浠g爜銆?/p>

 

鏂囦欢鍚? src/main.rs

 

use std::io;

 

fn main() {

   println!("Guess the number!");

 

   println!("Please input your guess.");

 

   let mut guess = String::new();

 

   io::stdin().read_line(&mut guess)

       .expect("Failed to read line");

 

   println!("You guessed: {}", guess);

}

鑾峰彇鐢ㄦ埛鐚滄祴骞舵墦鍗扮殑浠g爜

 

杩欎簺浠g爜鍖呭惈寰堝淇℃伅锛屾垜浠竴琛屼竴琛屽湴杩囦竴閬嶃€備负浜嗚幏鍙栫敤鎴疯緭鍏ュ苟鎵撳嵃缁撴灉浣滀负杈撳嚭锛屾垜浠渶瑕佸皢 io锛堣緭鍏?杈撳嚭锛夊簱寮曞叆褰撳墠浣滅敤鍩熴€俰o搴撴潵鑷簬鏍囧噯搴擄紙涔熻绉颁负std锛夛細

 

use std::io;

Rust 榛樿鍙湪姣忎釜绋嬪簭鐨刾relude 涓紩鍏ュ皯閲忕被鍨嬨€傚鏋滈渶瑕佺殑绫诲瀷涓嶅湪 prelude 涓紝浣犲繀椤讳娇鐢ㄤ竴涓?use 璇彞鏄惧紡鍦板皢鍏跺紩鍏ヤ綔鐢ㄥ煙銆?/span>std::io 搴撴彁渚涘緢澶?io 鐩稿叧鐨勫姛鑳斤紝姣斿鎺ュ彈鐢ㄦ埛杈撳叆鐨勫姛鑳姐€?/span>

 

濡備箣鍓嶆墍鎻愬強锛宮ain 鍑芥暟鏄▼搴忕殑鍏ュ彛鐐癸細

 

fn main() {

fn 璇硶澹版槑浜嗕竴涓柊鍑芥暟锛?) 琛ㄦ槑娌℃湁鍙傛暟锛寋 浣滀负鍑芥暟浣撶殑寮€濮嬨€?/p>

 

println! 鏄竴涓湪灞忓箷涓婃墦鍗板瓧绗︿覆鐨勫畯锛?/span>

 

println!("Guess the number!");

 

println!("Please input yourguess.");


杩欎簺浠g爜浠呬粎鎵撳嵃鎻愮ず锛屼粙缁嶆父鎴忕殑鍐呭鐒跺悗璇锋眰鐢ㄦ埛杈撳叆銆?/p>

 

浣跨敤鍙橀噺鍌ㄥ瓨鍊?/p>

鎺ヤ笅鏉ワ紝鍒涘缓涓€涓湴鏂瑰偍瀛樼敤鎴疯緭鍏ワ紝鍍忚繖鏍凤細

 

let mut guess = String::new();

鐜板湪绋嬪簭寮€濮嬪彉寰楁湁鎰忔€濅簡锛佽繖涓€灏忚浠g爜鍙戠敓浜嗗緢澶氫簨銆傛敞鎰忚繖鏄竴涓?let 璇彞锛岀敤鏉ュ垱寤哄彉閲忋€傝繖閲屾槸鍙﹀涓€涓緥瀛愶細

 

let foo = bar;

杩欒浠g爜鏂板缓浜嗕竴涓彨鍋?foo 鐨勫彉閲忓苟鎶婂畠缁戝畾鍒板€?bar 涓娿€傚湪 Rust 涓紝鍙橀噺榛樿鏄笉鍙彉鐨勩€?/span>涓嬮潰鐨勪緥瀛愬睍绀轰簡濡備綍鍦ㄥ彉閲忓悕鍓嶄娇鐢?mut 鏉ヤ娇涓€涓彉閲忓彲鍙橈細

 

let foo = 5; // immutable

let mut bar = 5; // mutable

娉ㄦ剰锛?/ 璇硶寮€濮嬩竴涓寔缁埌鏈鐨勭粨灏剧殑娉ㄩ噴銆俁ust 蹇界暐娉ㄩ噴涓殑鎵€鏈夊唴瀹广€?/span>

 

璁╂垜浠洖鍒扮寽鐚滅湅绋嬪簭涓€傜幇鍦ㄦ垜浠煡閬撲簡 let mut guess 浼氬紩鍏ヤ竴涓彨鍋?guess 鐨勫彲鍙樺彉閲忋€傜瓑鍙凤紙=锛夌殑鍙宠竟鏄?guess 鎵€缁戝畾鐨勫€硷紝瀹冩槸 String::new 鐨勭粨鏋滐紝杩欎釜鍑芥暟浼氳繑鍥炰竴涓?String 鐨勬柊瀹炰緥銆係tring 鏄竴涓爣鍑嗗簱鎻愪緵鐨勫瓧绗︿覆绫诲瀷锛屽畠鏄?UTF-8 缂栫爜鐨勫彲澧為暱鏂囨湰鍧椼€?/p>

 

::new 閭d竴琛岀殑 :: 璇硶琛ㄦ槑 new 鏄?String 绫诲瀷鐨勪竴涓?鍏宠仈鍑芥暟锛坅ssociated function锛夈€傚叧鑱斿嚱鏁版槸閽堝绫诲瀷瀹炵幇鐨勶紝鍦ㄨ繖涓緥瀛愪腑鏄疭tring锛岃€屼笉鏄?String 鐨勬煇涓壒瀹氬疄渚嬨€備竴浜涜瑷€涓妸瀹冪О涓?闈欐€佹柟娉曪紙static method锛夈€?/p>

 

new 鍑芥暟鍒涘缓浜嗕竴涓柊鐨勭┖String锛屼綘浼氬湪寰堝绫诲瀷涓婂彂鐜?new 鍑芥暟锛岃繖鏄垱寤虹被鍨嬪疄渚嬬殑鎯敤鍑芥暟鍚嶃€?/p>

 

鎬荤粨涓€涓嬶紝let mut guess = String::new(); 杩欎竴琛屽垱寤轰簡涓€涓彲鍙樺彉閲忥紝褰撳墠瀹冪粦瀹氬埌涓€涓柊鐨?String 绌哄疄渚嬩笂銆?/p>

 

鍥炲繂涓€涓嬶紝鎴戜滑鍦ㄧ▼搴忕殑绗竴琛屼娇鐢?use std::io; 浠庢爣鍑嗗簱涓紩鍏ヤ簡杈撳叆/杈撳嚭鍔熻兘銆傜幇鍦ㄨ皟鐢?io 鐨勫叧鑱斿嚱鏁皊tdin锛?/p>

 

io::stdin().read_line(&mut guess)

   .expect("Failed to read line");


濡傛灉绋嬪簭鐨勫紑澶存病鏈?use std::io 杩欎竴琛岋紝鍙互鎶婂嚱鏁拌皟鐢ㄥ啓鎴?std::io::stdin銆俿tdin 鍑芥暟杩斿洖涓€涓?std::io::Stdin 鐨勫疄渚嬶紝杩欎唬琛ㄧ粓绔爣鍑嗚緭鍏ュ彞鏌勭殑绫诲瀷銆?/p>

 

浠g爜鐨勪笅涓€閮ㄥ垎锛?read_line(&mut guess)锛岃皟鐢?read_line 鏂规硶浠庢爣鍑嗚緭鍏ュ彞鏌勮幏鍙栫敤鎴疯緭鍏ャ€傛垜浠繕鍚?read_line() 浼犻€掍簡涓€涓弬鏁帮細&mut guess銆?/p>

 

read_line 鐨勫伐浣滄槸锛屾棤璁虹敤鎴峰湪鏍囧噯杈撳叆涓敭鍏ヤ粈涔堝唴瀹癸紝閮藉皢鍏跺瓨鍏ヤ竴涓瓧绗︿覆涓紝鍥犳瀹冮渶瑕佸瓧绗︿覆浣滀负鍙傛暟銆傝繖涓瓧绗︿覆鍙傛暟搴旇鏄彲鍙樼殑锛屼互渚?read_line 灏嗙敤鎴疯緭鍏ラ檮鍔犱笂鍘汇€?/p>

 

& 琛ㄧず杩欎釜鍙傛暟鏄竴涓?寮曠敤锛坮eference锛夛紝瀹冨厑璁稿澶勪唬鐮佽闂悓涓€澶勬暟鎹紝鑰屾棤闇€鍦ㄥ唴瀛樹腑澶氭鎷疯礉銆傚紩鐢ㄦ槸涓€涓鏉傜殑鐗规€э紝Rust 鐨勪竴涓富瑕佷紭鍔垮氨鏄畨鍏ㄨ€岀畝鍗曠殑鎿嶇旱寮曠敤銆傚畬鎴愬綋鍓嶇▼搴忓苟涓嶉渶瑕佷簡瑙e姝ゅ缁嗚妭銆傜幇鍦紝鎴戜滑鍙渶鐭ラ亾瀹冨儚鍙橀噺涓€鏍凤紝榛樿鏄笉鍙彉鐨勶紝闇€瑕佸啓鎴?&mut guess 鑰屼笉鏄?&guess 鏉ヤ娇鍏跺彲鍙樸€?/p>

 

浣跨敤 Result 绫诲瀷鏉ュ鐞嗘綔鍦ㄧ殑閿欒

鎴戜滑杩樻病鏈夊畬鍏ㄥ垎鏋愬畬杩欒浠g爜銆傝櫧鐒惰繖鏄崟鐙竴琛屼唬鐮侊紝浣嗗畠鏄竴涓€昏緫琛岋紙铏界劧鎹㈣浜嗕絾浠嶆槸涓€涓鍙ワ級鐨勭涓€閮ㄥ垎銆傜浜岄儴鍒嗘槸杩欎釜鏂规硶锛?/p>

 

.expect("Failed to read line");

褰撲娇鐢?.foo() 璇硶璋冪敤鏂规硶鏃讹紝閫氳繃鎹㈣骞剁缉杩涙潵鎶婇暱琛屾媶寮€鏄槑鏅虹殑銆傛垜浠畬鍏ㄥ彲浠ヨ繖鏍峰啓锛?/p>

 

io::stdin().read_line(&mutguess).expect("Failed to read line");

涓嶈繃锛岃繃闀跨殑琛岄毦浠ラ槄璇伙紝鎵€浠ユ渶濂芥媶寮€鏉ュ啓锛屼袱琛屼唬鐮佷袱涓柟娉曡皟鐢ㄣ€傜幇鍦ㄦ潵鐪嬬湅杩欒浠g爜骞蹭簡浠€涔堛€?/p>

 

涔嬪墠鎻愬埌浜?read_line 灏嗙敤鎴疯緭鍏ラ檮鍔犲埌浼犻€掔粰瀹冪殑瀛楃涓蹭腑锛屼笉杩囧畠涔熻繑鍥炰竴涓€尖€斺€斿湪杩欎釜渚嬪瓙涓槸 io::Result銆俁ust 鏍囧噯搴撲腑鏈夊緢澶氬彨鍋?Result 鐨勭被鍨嬨€備竴涓?Result 娉涘瀷浠ュ強瀵瑰簲瀛愭ā鍧楃殑鐗瑰畾鐗堟湰锛屾瘮濡?io::Result銆?/p>

 

Result 绫诲瀷鏄?鏋氫妇锛坋numerations锛夛紝閫氬父涔熷啓浣?enums銆傛灇涓剧被鍨嬫寔鏈夊浐瀹氶泦鍚堢殑鍊硷紝杩欎簺鍊艰绉颁负鏋氫妇鐨?鎴愬憳锛坴ariants锛夈€?/p>

 

瀵逛簬 Result锛屽畠鐨勬垚鍛樻槸 Ok 鎴?Err锛孫k 鎴愬憳琛ㄧず鎿嶄綔鎴愬姛锛屽唴閮ㄥ寘鍚垚鍔熸椂浜х敓鐨勫€笺€侲rr 鎴愬憳鍒欐剰鍛崇潃鎿嶄綔澶辫触锛屽苟涓斿寘鍚け璐ョ殑鍓嶅洜鍚庢灉銆?/p>

 

杩欎簺 Result 绫诲瀷鐨勪綔鐢ㄦ槸缂栫爜閿欒澶勭悊淇℃伅銆俁esult 绫诲瀷鐨勫€硷紝鍍忓叾浠栫被鍨嬩竴鏍凤紝鎷ユ湁瀹氫箟浜庡叾涓婄殑鏂规硶銆俰o::Result 鐨勫疄渚嬫嫢鏈?expect 鏂规硶銆傚鏋?io::Result 瀹炰緥鐨勫€兼槸 Err锛宔xpect 浼氬鑷寸▼搴忓穿婧冿紝骞舵樉绀哄綋鍋氬弬鏁颁紶閫掔粰 expect 鐨勪俊鎭€傚鏋?read_line 鏂规硶杩斿洖 Err锛屽垯鍙兘鏄潵婧愪簬搴曞眰鎿嶄綔绯荤粺閿欒鐨勭粨鏋溿€傚鏋?io::Result 瀹炰緥鐨勫€兼槸 Ok锛宔xpect 浼氳幏鍙?Ok 涓殑鍊煎苟鍘熸牱杩斿洖銆傚湪鏈緥涓紝杩欎釜鍊兼槸鐢ㄦ埛杈撳叆鍒版爣鍑嗚緭鍏ヤ腑鐨勫瓧鑺傜殑鏁伴噺銆?/p>

 

濡傛灉涓嶈皟鐢?expect锛岀▼搴忎篃鑳界紪璇戯紝涓嶈繃浼氬嚭鐜颁竴涓鍛婏細

 

$ cargo build

  Compiling guessing_game v0.1.0 (file:///projects/guessing_game)

warning: unused `std::result::Result` whichmust be used

 --> src/main.rs:10:5

   |

10 |    io::stdin().read_line(&mut guess);

  |    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

   |

   =note: #[warn(unused_must_use)] on by default


Rust 璀﹀憡鎴戜滑娌℃湁浣跨敤read_line 鐨勮繑鍥炲€?Result锛岃鏄庢湁涓€涓彲鑳界殑閿欒娌℃湁澶勭悊銆?/span>

 

娑堥櫎璀﹀憡鐨勬纭仛娉曟槸瀹為檯缂栧啓閿欒澶勭悊浠g爜锛屼笉杩囩敱浜庢垜浠氨鏄笇鏈涚▼搴忓湪鍑虹幇闂鏃剁珛鍗冲穿婧冿紝鎵€浠ョ洿鎺ヤ娇鐢?expect銆傜涔濈珷浼氬涔犲浣曚粠閿欒涓仮澶嶃€?/p>

 

浣跨敤 println! 鍗犱綅绗︽墦鍗板€?/p>

闄や簡浣嶄簬缁撳熬鐨勫ぇ鎷彿锛岀洰鍓嶄负姝㈠氨鍙湁涓€琛屼唬鐮佸€煎緱璁ㄨ涓€涓嬩簡锛屽氨鏄繖涓€琛岋細

 

println!("You guessed: {}",guess);


杩欒浠g爜鎵撳嵃瀛樺偍鐢ㄦ埛杈撳叆鐨勫瓧绗︿覆銆傜涓€涓弬鏁版槸鏍煎紡鍖栧瓧绗︿覆锛岄噷闈㈢殑 {} 鏄鐣欏湪鐗瑰畾浣嶇疆鐨勫崰浣嶇銆備娇鐢?{} 涔熷彲浠ユ墦鍗板涓€硷細绗竴瀵?{} 浣跨敤鏍煎紡鍖栧瓧绗︿覆涔嬪悗鐨勭涓€涓€硷紝绗簩瀵瑰垯浣跨敤绗簩涓€硷紝渚濇绫绘帹銆傝皟鐢ㄤ竴娆?println! 鎵撳嵃澶氫釜鍊肩湅璧锋潵鍍忚繖鏍凤細

 

let x = 5;

let y = 10;

 

println!("x = {} and y = {}", x,y);

杩欒浠g爜浼氭墦鍗板嚭 x = 5 and y = 10銆?/p>

 

娴嬭瘯绗竴閮ㄥ垎浠g爜

璁╂垜浠潵娴嬭瘯涓嬬寽鐚滅湅娓告垙鐨勭涓€閮ㄥ垎銆備娇鐢?cargo run 杩愯锛?/span>

 

$ cargo run

  Compiling guessing_game v0.1.0 (file:///projects/guessing_game)

   Finished dev [unoptimized + debuginfo] target(s) in 2.53 secs

    Running `target/debug/guessing_game`

Guess the number!

Please input your guess.

6

You guessed: 6


鑷虫涓烘锛屾父鎴忕殑绗竴閮ㄥ垎宸茬粡瀹屾垚锛氭垜浠粠閿洏鑾峰彇杈撳叆骞舵墦鍗颁簡鍑烘潵銆?/p>

 

鐢熸垚涓€涓瀵嗘暟瀛?/strong>

鎺ヤ笅鏉ワ紝闇€瑕佺敓鎴愪竴涓瀵嗘暟瀛楋紝濂借鐢ㄦ埛鏉ョ寽銆傜瀵嗘暟瀛楀簲璇ユ瘡娆¢兘涓嶅悓锛岃繖鏍烽噸澶嶇帺鎵嶄笉浼氫箯鍛筹紱鑼冨洿搴旇鍦?1 鍒?100 涔嬮棿锛岃繖鏍锋墠涓嶄細澶洶闅俱€俁ust 鏍囧噯搴撲腑灏氭湭鍖呭惈闅忔満鏁板姛鑳姐€傜劧鑰岋紝Rust 鍥㈤槦杩樻槸鎻愪緵浜嗕竴涓?rand crate銆?/p>

 

浣跨敤 crate 鏉ュ鍔犳洿澶氬姛鑳?/strong>

璁颁綇 crate 鏄竴涓?Rust 浠g爜鐨勫寘銆傛垜浠鍦ㄦ瀯寤虹殑椤圭洰鏄竴涓簩杩涘埗 crate锛屽畠鐢熸垚涓€涓彲鎵ц鏂囦欢銆?rand crate 鏄竴涓簱 crate锛屽簱 crate 鍙互鍖呭惈浠绘剰鑳借鍏朵粬绋嬪簭浣跨敤鐨勪唬鐮併€?/p>

 

Cargo 瀵瑰閮?crate 鐨勮繍鐢ㄦ槸鍏剁湡姝i棯鍏夌殑鍦版柟銆傚湪鎴戜滑浣跨敤 rand 缂栧啓浠g爜涔嬪墠锛岄渶瑕佺紪杈?Cargo.toml 锛屽0鏄?rand 浣滀负涓€涓緷璧栥€傜幇鍦ㄦ墦寮€杩欎釜鏂囦欢骞跺湪搴曢儴鐨?[dependencies] 閮ㄥ垎鏍囬涔嬩笅娣诲姞锛?/p>

 

鏂囦欢鍚? Cargo.toml

 

[dependencies]

 

rand = "0.3.14"

鍦?Cargo.toml 鏂囦欢涓紝鏍囬浠ュ強涔嬪悗鐨勫唴瀹瑰睘鍚屼竴涓儴鍒嗭紝鐩村埌閬囧埌涓嬩竴涓爣棰樻墠寮€濮嬫柊鐨勯儴鍒嗐€俒dependencies] 閮ㄥ垎鍛婅瘔 Cargo 鏈」鐩緷璧栦簡鍝簺澶栭儴 crate 鍙婂叾鐗堟湰銆傛湰渚嬩腑锛屾垜浠娇鐢ㄨ涔夊寲鐗堟湰 0.3.14 鏉ユ寚瀹?rand crate銆侰argo 鐞嗚В璇箟鍖栫増鏈紙Semantic Versioning锛夛紙鏈夋椂涔熺О涓?SemVer锛夛紝杩欐槸涓€绉嶅畾涔夌増鏈彿鐨勬爣鍑嗐€?.3.14 浜嬪疄涓婃槸 ^0.3.14 鐨勭畝鍐欙紝瀹冭〃绀?鈥滀换浣曚笌 0.3.14 鐗堟湰鍏湁API 鐩稿吋瀹圭殑鐗堟湰鈥濄€?/p>

 

鐜板湪锛屼笉淇敼浠讳綍浠g爜锛屾瀯寤洪」鐩紝濡備笅渚嬫墍绀猴細

 

$ cargo build

   Updating registry `https://github.com/rust-lang/crates.io-index`

 Downloading rand v0.3.14

 Downloading libc v0.2.14

  Compiling libc v0.2.14

  Compiling rand v0.3.14

  Compiling guessing_game v0.1.0 (file:///projects/guessing_game)

   Finished dev [unoptimized + debuginfo] target(s) in 2.53 secs

澧炲姞 randcrate 浣滀负渚濊禆涔嬪悗杩愯 cargo build 鐨勮緭鍑?/p>

 

鍙兘浼氬嚭鐜颁笉鍚岀殑鐗堟湰鍙凤紙澶氫簭浜嗚涔夊寲鐗堟湰锛屽畠浠笌浠g爜鏄吋瀹圭殑锛侊級锛屽悓鏃舵樉绀洪『搴忎篃鍙兘浼氭湁鎵€涓嶅悓銆?/p>

 

鐜板湪鎴戜滑鏈変簡涓€涓閮ㄤ緷璧栵紝Cargo 浠?registry 涓婅幏鍙栨墍鏈夊寘鐨勬渶鏂扮増鏈俊鎭紝杩欐槸涓€浠芥潵鑷?Crates.io 鐨勬暟鎹嫹璐濄€侰rates.io 鏄?Rust 鐢熸€佺幆澧冧腑鐨勫紑鍙戣€呬滑鍚戜粬浜鸿础鐚?Rust 寮€婧愰」鐩殑鍦版柟銆?/p>

 

鍦ㄦ洿鏂板畬 registry 鍚庯紝Cargo妫€鏌?[dependencies] 娈佃惤骞朵笅杞界己澶辩殑create 銆傛湰渚嬩腑锛岃櫧鐒跺彧澹版槑浜?rand 涓€涓緷璧栵紝鐒惰€孋argo 杩樻槸棰濆鑾峰彇浜?libc 鐨勬嫹璐濓紝鍥犱负 rand 渚濊禆 libc 鏉ユ甯稿伐浣溿€備笅杞藉畬鎴愬悗锛孯ust 缂栬瘧渚濊禆锛岀劧鍚庝娇鐢ㄨ繖浜涗緷璧栫紪璇戦」鐩€?/p>

 

濡傛灉涓嶅仛浠讳綍淇敼锛岀珛鍒诲啀娆¤繍琛?cargo build锛屽垯涓嶄細鐪嬪埌浠讳綍闄や簡 Finished 瀹屾垚鎻愮ず涔嬪鐨勮緭鍑恒€侰argo 鐭ラ亾瀹冨凡缁忎笅杞藉苟缂栬瘧浜嗕緷璧栵紝鍚屾椂 Cargo.toml 鏂囦欢涔熸病鏈夊彉鍔ㄣ€侰argo 杩樼煡閬撲唬鐮佷篃娌℃湁浠讳綍淇敼锛屾墍浠ュ畠涓嶄細閲嶆柊缂栬瘧浠g爜銆傚洜涓烘棤浜嬪彲鍋氾紝瀹冪畝鍗曠殑閫€鍑轰簡銆?/p>

 

濡傛灉鎵撳紑 src/main.rs 鏂囦欢锛屽仛涓€浜涙棤鍏崇揣瑕佺殑淇敼锛屼繚瀛樺苟鍐嶆鏋勫缓锛屽彧浼氬嚭鐜颁袱琛岃緭鍑猴細

 

$ cargo build

  Compiling guessing_game v0.1.0 (file:///projects/guessing_game)

   Finished dev [unoptimized + debuginfo] target(s) in 2.53 secs

杩欎竴琛岃〃绀?Cargo 鍙拡瀵箂rc/main.rs 鏂囦欢鐨勫井灏忎慨鏀硅€屾洿鏂版瀯寤恒€備緷璧栨病鏈夊彉鍖栵紝鎵€浠?Cargo 鐭ラ亾瀹冨彲浠ュ鐢ㄥ凡缁忎负姝や笅杞藉苟缂栬瘧鐨勪唬鐮併€傚畠鍙槸閲嶆柊鏋勫缓浜嗛儴鍒嗭紙椤圭洰锛変唬鐮併€?/p>

 

Cargo.lock 鏂囦欢纭繚鏋勫缓鏄彲閲嶇幇鐨?/strong>

Cargo 鏈変竴涓満鍒舵潵纭繚浠讳綍浜哄湪浠讳綍鏃跺€欓噸鏂版瀯寤轰唬鐮侊紝閮戒細浜х敓鐩稿悓鐨勭粨鏋滐細Cargo 鍙細浣跨敤浣犳寚瀹氱殑渚濊禆鐨勭増鏈紝闄ら潪浣犲張鎵嬪姩鎸囧畾浜嗗埆鐨勩€備緥濡傦紝濡傛灉涓嬪懆 randcrate 鐨?v0.3.15 鐗堟湰鍑烘潵浜嗭紝瀹冧慨澶嶄簡涓€涓噸瑕佺殑bug锛屽悓鏃朵篃鍚湁涓€涓細鐮村潖浠g爜杩愯鐨勭己闄凤紝杩欐椂浼氬彂鐢熶粈涔堝憿锛?/p>

 

杩欎釜闂鐨勭瓟妗堟槸 Cargo.lock 鏂囦欢銆傚畠鍦ㄧ涓€娆¤繍琛?cargo build 鏃跺垱寤猴紝骞舵斁鍦?guessing_game 鐩綍銆傚綋绗竴娆℃瀯寤洪」鐩椂锛孋argo 璁$畻鍑烘墍鏈夌鍚堣姹傜殑渚濊禆鐗堟湰骞跺啓鍏?Cargo.lock 鏂囦欢銆傚綋灏嗘潵鏋勫缓椤圭洰鏃讹紝Cargo 浼氬彂鐜?Cargo.lock 瀛樺湪骞朵娇鐢ㄥ叾涓寚瀹氱殑鐗堟湰锛岃€屼笉鏄啀娆¤绠楁墍鏈夌殑鐗堟湰銆傝繖浣垮緱浣犳嫢鏈変簡涓€涓嚜鍔ㄥ寲鐨勫彲閲嶇幇鐨勬瀯寤恒€傛崲鍙ヨ瘽璇达紝椤圭洰浼氭寔缁娇鐢?0.3.14 鐩村埌浣犳樉寮忓崌绾э紝鎰熻阿 Cargo.lock 鏂囦欢銆?/p>

 

鏇存柊 crate 鍒颁竴涓柊鐗堟湰

褰撲綘 纭疄 闇€瑕佸崌绾?crate 鏃讹紝Cargo鎻愪緵浜嗗彟涓€涓懡浠わ紝update锛屽畠浼氬拷鐣?Cargo.lock鏂囦欢锛屽苟璁$畻鍑烘墍鏈夌鍚?Cargo.toml 澹版槑鐨勬渶鏂扮増鏈€傚鏋滄垚鍔熶簡锛孋argo 浼氭妸杩欎簺鐗堟湰鍐欏叆 Cargo.lock 鏂囦欢銆?/p>

 

涓嶈繃锛孋argo 榛樿鍙細瀵绘壘澶т簬0.3.0 鑰屽皬浜?0.4.0 鐨勭増鏈€傚鏋?randcrate 鍙戝竷浜嗕袱涓柊鐗堟湰锛?.3.15 鍜?0.4.0锛屽湪杩愯 cargo update 鏃朵細鍑虹幇濡備笅鍐呭锛?/p>

 

$ cargo update

   Updating registry `https://github.com/rust-lang/crates.io-index`

   Updating rand v0.3.14 -> v0.3.15

杩欐椂锛屼綘涔熶細娉ㄦ剰鍒扮殑 Cargo.lock 鏂囦欢涓殑鍙樺寲鏃犲涔?rand crate 鐜板湪浣跨敤鐨勭増鏈槸0.3.15

 

濡傛灉鎯宠浣跨敤 0.4.0 鐗堟湰鐨剅and 鎴栨槸浠讳綍 0.4.x 绯诲垪鐨勭増鏈紝蹇呴』鍍忚繖鏍锋洿鏂癈argo.toml 鏂囦欢锛?/p>

 

[dependencies]

 

rand = "0.4.0"

涓嬩竴娆¤繍琛?cargo build 鏃讹紝Cargo 浼氫粠 registry 鏇存柊鍙敤鐨?crate锛屽苟鏍规嵁浣犳寚瀹氱殑鏂扮増鏈噸鏂拌绠椼€?/p>

 

閫氳繃 Cargo 澶嶇敤搴撴枃浠堕潪甯稿鏄擄紝鍥犳 Rustacean 鑳藉缂栧啓鍑虹敱寰堝鍖呯粍瑁呰€屾垚鐨勬洿杞诲阀鐨勯」鐩€?/p>

 

鐢熸垚涓€涓殢鏈烘暟

浣犲凡缁忔妸 rand crate 娣诲姞鍒癈argo.toml 浜嗭紝璁╂垜浠紑濮?浣跨敤 rand 鍚с€備笅涓€姝ユ槸鏇存柊 src/main.rs锛屽绀轰緥 2-3 鎵€绀猴細

 

鏂囦欢鍚? src/main.rs

 

extern crate rand;

 

use std::io;

use rand::Rng;

 

fn main() {

   println!("Guess the number!");

 

   let secret_number = rand::thread_rng().gen_range(1, 101);

 

   println!("The secret number is: {}", secret_number);

 

   println!("Please input your guess.");

 

   let mut guess = String::new();

 

   io::stdin().read_line(&mut guess)

       .expect("Failed to read line");

 

   println!("You guessed: {}", guess);

}

姝や緥涓轰簡鐢熸垚闅忔満鏁拌€屽仛鐨勪慨鏀?/p>

 

棣栧厛锛岃繖閲屽湪椤堕儴澧炲姞涓€琛?extern crate rand; 閫氱煡 Rust 鎴戜滑瑕佷娇鐢ㄥ閮ㄤ緷璧栥€傝繖涔熶細璋冪敤鐩稿簲鐨?use rand锛屾墍浠ョ幇鍦ㄥ彲浠ヤ娇鐢?rand:: 鍓嶇紑鏉ヨ皟鐢?rand crate 涓殑浠讳綍鍐呭銆?/p>

 

鎺ヤ笅鏉ュ鍔犱簡鍙︿竴琛?use锛歶serand::Rng銆俁ng 鏄竴涓?trait锛屽畠瀹氫箟浜嗛殢鏈烘暟鐢熸垚鍣ㄥ簲瀹炵幇鐨勬柟娉曪紝鎯充娇鐢ㄨ繖浜涙柟娉曠殑璇濇 trait 蹇呴』鍦ㄤ綔鐢ㄥ煙涓€傜鍗佺珷浼氳缁嗕粙缁?trait銆?/p>

 

鍙﹀锛屼腑闂磋繕鏂板鍔犱簡涓よ銆俽and::thread_rng 鍑芥暟鎻愪緵瀹為檯浣跨敤鐨勯殢鏈烘暟鐢熸垚鍣細瀹冧綅浜庡綋鍓嶆墽琛岀嚎绋嬫湰鍦帮紝骞朵粠鎿嶄綔绯荤粺鑾峰彇 seed銆傛帴涓嬫潵锛岃皟鐢ㄩ殢鏈烘暟鐢熸垚鍣ㄧ殑 gen_range 鏂规硶銆傝繖涓柟娉曠敱鍒氭墠寮曞叆鍒颁綔鐢ㄥ煙鐨?Rng trait 瀹氫箟銆俫en_range 鏂规硶鑾峰彇涓や釜鏁板瓧浣滀负鍙傛暟锛屽苟鐢熸垚涓€涓寖鍥村湪涓よ€呬箣闂寸殑闅忔満鏁般€傚畠鍖呭惈涓嬮檺浣嗕笉鍖呭惈涓婇檺锛屾墍浠ラ渶瑕佹寚瀹?1 鍜?101 鏉ヨ姹備竴涓?1 鍜?100 涔嬮棿鐨勬暟銆?/p>

 

娉ㄦ剰锛氫綘涓嶅彲鑳藉嚟绌哄氨鐭ラ亾搴旇 use 鍝釜 trait 浠ュ強璇ヤ粠 crate 涓皟鐢ㄥ摢涓柟娉曘€俢rate 鐨勪娇鐢ㄨ鏄庝綅浜庡叾鏂囨。涓€侰argo 鏈変竴涓緢妫掔殑鍔熻兘鏄細杩愯 cargo doc --open 鍛戒护鏉ユ瀯寤烘墍鏈夋湰鍦颁緷璧栨彁渚涚殑鏂囨。锛屽苟鍦ㄦ祻瑙堝櫒涓墦寮€銆備緥濡傦紝鍋囪浣犲 rand crate 涓殑鍏朵粬鍔熻兘鎰熷叴瓒o紝浣犲彲浠ヨ繍琛?cargo doc --open骞剁偣鍑诲乏渚у鑸爮涓殑 rand銆?/span>

 

鏂板鍔犵殑绗簩琛屼唬鐮佹墦鍗板嚭浜嗙瀵嗘暟瀛椼€傝繖鍦ㄥ紑鍙戠▼搴忔椂寰堟湁鐢紝鍥犱负鍙互娴嬭瘯瀹冿紝涓嶈繃鍦ㄦ渶缁堢増鏈腑浼氬垹鎺夊畠銆傛父鎴忎竴寮€濮嬪氨鎵撳嵃鍑虹粨鏋滃氨娌′粈涔堝彲鐜╃殑浜嗭紒

 

灏濊瘯杩愯绋嬪簭鍑犳锛?/p>

 

$ cargo run

  Compiling guessing_game v0.1.0 (file:///projects/guessing_game)

   Finished dev [unoptimized + debuginfo] target(s) in 2.53 secs

    Running `target/debug/guessing_game`

Guess the number!

The secret number is: 7

Please input your guess.

4

You guessed: 4

$ cargo run

    Running `target/debug/guessing_game`

Guess the number!

The secret number is: 83

Please input your guess.

5

You guessed: 5


浣犲簲璇ヨ兘寰楀埌涓嶅悓鐨勯殢鏈烘暟锛屽悓鏃跺畠浠簲璇ラ兘鏄湪 1 鍜?100 涔嬮棿鐨勩€?/p>

 

姣旇緝鐚滄祴涓庣瀵嗘暟瀛?/strong>

鐜板湪鏈変簡鐢ㄦ埛杈撳叆鍜屼竴涓殢鏈烘暟锛屾垜浠彲浠ユ瘮杈冨畠浠€傝繖涓楠ゅ涓嬩緥鎵€绀恒€傛敞鎰忚繖娈典唬鐮佽繕涓嶈兘閫氳繃缂栬瘧锛屾垜浠◢鍚庝細瑙i噴銆?/p>

 

鏂囦欢鍚? src/main.rs

 

extern crate rand;

 

use std::io;

use std::cmp::Ordering;

use rand::Rng;

 

fn main() {

 

   // ---snip---

 

   println!("You guessed: {}", guess);

 

   match guess.cmp(&secret_number) {

       Ordering::Less => println!("Too small!"),

       Ordering::Greater => println!("Too big!"),

       Ordering::Equal => println!("You win!"),

    }

}

姝や緥澶勭悊姣旇緝涓や釜鏁板瓧鍙兘鐨勮繑鍥炲€?/p>

 

鏂颁唬鐮佺殑绗竴琛屾槸鍙︿竴涓?use锛屼粠鏍囧噯搴撳紩鍏ヤ簡涓€涓彨鍋?std::cmp::Ordering 鐨勭被鍨嬨€傚悓 Result 涓€鏍凤紝 Ordering 涔熸槸涓€涓灇涓撅紝涓嶈繃瀹冪殑鎴愬憳鏄?Less銆丟reater 鍜?Equal銆傝繖鏄瘮杈冧袱涓€兼椂鍙兘鍑虹幇鐨勪笁绉嶇粨鏋溿€?/p>

 

鎺ョ潃锛屽簳閮ㄧ殑浜旇鏂颁唬鐮佷娇鐢ㄤ簡 Ordering 绫诲瀷锛宑mp 鏂规硶鐢ㄦ潵姣旇緝涓や釜鍊煎苟鍙互鍦ㄤ换浣曞彲姣旇緝鐨勫€间笂璋冪敤銆傚畠鑾峰彇涓€涓姣旇緝鍊肩殑寮曠敤锛氳繖閲屾槸鎶?guess 涓?secret_number 鍋氭瘮杈冦€?鐒跺悗瀹冧細杩斿洖涓€涓垰鎵嶉€氳繃 use 寮曞叆浣滅敤鍩熺殑 Ordering 鏋氫妇鐨勬垚鍛樸€備娇鐢ㄤ竴涓?match 琛ㄨ揪寮忥紝鏍规嵁瀵?guess 鍜?secret_number 璋冪敤 cmp 杩斿洖鐨?Ordering 鎴愬憳鏉ュ喅瀹氭帴涓嬫潵鍋氫粈涔堛€?/p>

 

涓€涓?match 琛ㄨ揪寮忕敱 鍒嗘敮锛坅rms锛夋瀯鎴愩€備竴涓垎鏀寘鍚竴涓?妯″紡锛坧attern锛夊拰琛ㄨ揪寮忓紑澶寸殑鍊间笌鍒嗘敮妯″紡鐩稿尮閰嶆椂搴旇鎵ц鐨勪唬鐮併€俁ust 鑾峰彇鎻愪緵缁?match 鐨勫€煎苟鎸ㄤ釜妫€鏌ユ瘡涓垎鏀殑妯″紡銆俶atch 缁撴瀯鍜屾ā寮忔槸 Rust 涓己澶х殑鍔熻兘锛屽畠浣撶幇浜嗕唬鐮佸彲鑳介亣鍒扮殑澶氱鎯呭舰锛屽苟甯姪浣犵‘淇濇病鏈夐仐婕忓鐞嗐€傝繖浜涘姛鑳藉皢鍒嗗埆鍦ㄧ鍏珷鍜岀鍗佸叓绔犺缁嗕粙缁嶃€?/p>

 

璁╂垜浠湅鐪嬩娇鐢?match 琛ㄨ揪寮忕殑渚嬪瓙銆傚亣璁剧敤鎴风寽浜?50锛岃繖鏃堕殢鏈虹敓鎴愮殑绉樺瘑鏁板瓧鏄?38銆傛瘮杈?50 涓?38 鏃讹紝鍥犱负 50 姣?38 瑕佸ぇ锛宑mp 鏂规硶浼氳繑鍥濷rdering::Greater銆侽rdering::Greater 鏄?match 琛ㄨ揪寮忓緱鍒扮殑鍊笺€傚畠妫€鏌ョ涓€涓垎鏀殑妯″紡锛孫rdering::Less 涓?Ordering::Greater骞朵笉鍖归厤锛屾墍浠ュ畠蹇界暐浜嗚繖涓垎鏀殑鍔ㄤ綔骞舵潵鍒颁笅涓€涓垎鏀€備笅涓€涓垎鏀殑妯″紡鏄?Ordering::Greater锛屾纭?鍖归厤锛佽繖涓垎鏀叧鑱旂殑浠g爜琚墽琛岋紝鍦ㄥ睆骞曟墦鍗板嚭Too big!銆俶atch 琛ㄨ揪寮忓氨姝ょ粓姝紝鍥犱负璇ュ満鏅笅娌℃湁妫€鏌ユ渶鍚庝竴涓垎鏀殑蹇呰銆?/p>

 

鐒惰€岋紝绀轰緥 2-4 鐨勪唬鐮佸苟涓嶈兘缂栬瘧锛屽彲浠ュ皾璇曚竴涓嬶細

 

$ cargo build

  Compiling guessing_game v0.1.0 (file:///projects/guessing_game)

error[E0308]: mismatched types

 --> src/main.rs:23:21

   |

23 |    match guess.cmp(&secret_number) {

  |                    ^^^^^^^^^^^^^^ expected struct `std::string::String`, found integralvariable

   |

   =note: expected type `&std::string::String`

   =note:    found type `&{integer}`

 

error: aborting due to previous error

Could not compile `guessing_game`.

閿欒鐨勬牳蹇冭〃鏄庤繖閲屾湁 涓嶅尮閰嶇殑绫诲瀷锛坢ismatched types锛夈€俁ust 鏈変竴涓潤鎬佸己绫诲瀷绯荤粺锛屽悓鏃朵篃鏈夌被鍨嬫帹鏂€傚綋鎴戜滑鍐欏嚭 let guess =String::new() 鏃讹紝Rust 鎺ㄦ柇鍑?guess 搴旇鏄竴涓猄tring锛屼笉闇€瑕佹垜浠啓鍑虹殑绫诲瀷銆傚彟涓€鏂归潰锛宻ecret_number锛屾槸涓€涓暟瀛楃被鍨嬨€傚绉嶆暟瀛楃被鍨嬫嫢鏈?1 鍒?100 涔嬮棿鐨勫€硷細32 浣嶆暟瀛?i32锛?2 浣嶆棤绗﹀彿鏁板瓧 u32锛?4 浣嶆暟瀛?i64 绛夌瓑銆俁ust 榛樿浣跨敤 i32锛屾墍浠ュ畠鏄?secret_number 鐨勭被鍨嬶紝闄ら潪澧炲姞绫诲瀷淇℃伅锛屾垨浠讳綍鑳借 Rust 鎺ㄦ柇鍑轰笉鍚屾暟鍊肩被鍨嬬殑淇℃伅銆傝繖閲岄敊璇殑鍘熷洜鍦ㄤ簬 Rust 涓嶄細姣旇緝瀛楃涓茬被鍨嬪拰鏁板瓧绫诲瀷銆?/p>

 

鎵€浠ユ垜浠繀椤绘妸浠庤緭鍏ヤ腑璇诲彇鍒扮殑 String 杞崲涓轰竴涓湡姝g殑鏁板瓧绫诲瀷锛屾墠濂戒笌绉樺瘑鏁板瓧杩涜姣旇緝銆傝繖鍙互閫氳繃鍦?main 鍑芥暟浣撲腑澧炲姞濡備笅涓よ浠g爜鏉ュ疄鐜帮細

 

鏂囦欢鍚? src/main.rs

 

// --snip--

 

   let mut guess = String::new();

 

   io::stdin().read_line(&mut guess)

       .expect("Failed to read line");

 

   let guess: u32 = guess.trim().parse()

       .expect("Please type a number!");

 

   println!("You guessed: {}", guess);

 

   match guess.cmp(&secret_number) {

       Ordering::Less => println!("Too small!"),

       Ordering::Greater => println!("Too big!"),

       Ordering::Equal => println!("You win!"),

    }

}

杩欎袱琛屾柊浠g爜鏄細

 

let guess: u32 = guess.trim().parse()

   .expect("Please type a number!");

杩欓噷鍒涘缓浜嗕竴涓彨鍋?guess 鐨勫彉閲忋€備笉杩囩瓑绛夛紝涓嶆槸宸茬粡鏈変簡涓€涓彨鍋歡uess鐨勫彉閲忎簡鍚楋紵纭疄濡傛锛屼笉杩?Rust 鍏佽 闅愯棌锛坰hadow锛夛紝鐢ㄤ竴涓柊鍊兼潵闅愯棌 guess 涔嬪墠鐨勫€笺€傝繖涓姛鑳藉父鐢ㄥ湪闇€瑕佽浆鎹㈠€肩被鍨嬩箣绫荤殑鍦烘櫙锛屽畠鍏佽鎴戜滑澶嶇敤 guess 鍙橀噺鐨勫悕瀛楋紝鑰屼笉鏄杩垱寤轰袱涓笉鍚屽彉閲忥紝璇稿 guess_str 鍜?guess 涔嬬被銆傦紙绗笁绔犱細浠嬬粛 shadowing 鐨勬洿澶氱粏鑺傘€傦級

 

guess 琚粦瀹氬埌guess.trim().parse() 琛ㄨ揪寮忋€傝〃杈惧紡涓殑 guess 鏄寘鍚緭鍏ョ殑鍘熷 String 绫诲瀷銆係tring 瀹炰緥鐨?trim 鏂规硶浼氬幓闄ゅ瓧绗︿覆寮€澶村拰缁撳熬鐨勭┖鐧姐€倁32 鍙兘鐢辨暟瀛楀瓧绗﹁浆鎹紝涓嶈繃鐢ㄦ埛蹇呴』杈撳叆 return 閿墠鑳借 read_line 杩斿洖锛岀劧鑰岀敤鎴锋寜涓?return 閿椂锛屼細鍦ㄥ瓧绗︿覆涓鍔犱竴涓崲琛岋紙newline锛夌銆備緥濡傦紝鐢ㄦ埛杈撳叆 5 骞舵寜涓?return锛実uess 鐪嬭捣鏉ュ儚杩欐牱锛?\n銆俓n 浠h〃 鈥滄崲琛屸€濓紝鍥炶溅閿€倀rim 鏂规硶娑堥櫎 \n锛屽彧鐣欎笅5銆?/p>

 

瀛楃涓茬殑 parse 鏂规硶 灏嗗瓧绗︿覆瑙f瀽鎴愭暟瀛椼€傚洜涓鸿繖涓柟娉曞彲浠ヨВ鏋愬绉嶆暟瀛楃被鍨嬶紝鍥犳闇€瑕佸憡璇?Rust 鍏蜂綋鐨勬暟瀛楃被鍨嬶紝杩欓噷閫氳繃 let guess: u32 鎸囧畾銆俫uess 鍚庨潰鐨勫啋鍙凤紙:锛夊憡璇塕ust 鎴戜滑鎸囧畾浜嗗彉閲忕殑绫诲瀷銆俁ust 鏈変竴浜涘唴寤虹殑鏁板瓧绫诲瀷锛泆32 鏄竴涓棤绗﹀彿鐨?32 浣嶆暣鍨嬨€傚浜庝笉澶х殑姝f暣鏁版潵璇达紝瀹冩槸涓嶉敊鐨勭被鍨嬶紝绗笁绔犺繕浼氳鍒板叾浠栨暟瀛楃被鍨嬨€傚彟澶栵紝绋嬪簭涓殑 u32 娉ㄨВ浠ュ強涓?secret_number 鐨勬瘮杈冿紝鎰忓懗鐫€ Rust 浼氭帹鏂嚭 secret_number 涔熸槸 u32 绫诲瀷銆傜幇鍦ㄥ彲浠ヤ娇鐢ㄧ浉鍚岀被鍨嬫瘮杈冧袱涓€间簡锛?/p>

 

parse 璋冪敤寰堝鏄撲骇鐢熼敊璇€備緥濡傦紝瀛楃涓蹭腑鍖呭惈 A馃憤%锛屽氨鏃犳硶灏嗗叾杞崲涓轰竴涓暟瀛椼€傚洜姝わ紝parse 鏂规硶杩斿洖涓€涓?Result 绫诲瀷銆傚儚涔嬪墠 鈥滀娇鐢≧esult 绫诲瀷鏉ュ鐞嗘綔鍦ㄧ殑閿欒鈥?璁ㄨ鐨剅ead_line 鏂规硶閭f牱锛屽啀娆℃寜閮ㄥ氨鐝殑鐢?expect 鏂规硶澶勭悊鍗冲彲銆傚鏋?parse 涓嶈兘浠庡瓧绗︿覆鐢熸垚涓€涓暟瀛楋紝杩斿洖涓€涓?Result::Err 鏃讹紝expect 浼氫娇娓告垙宕╂簝骞舵墦鍗伴檮甯︾殑淇℃伅銆傚鏋?parse 鎴愬姛鍦板皢瀛楃涓茶浆鎹负涓€涓暟瀛楋紝瀹冧細杩斿洖 Result::Ok锛岀劧鍚?expect 浼氳繑鍥?Ok 涓殑鏁板瓧銆?/p>

 

鐜板湪璁╂垜浠繍琛岀▼搴忥紒

 

$ cargo run

  Compiling guessing_game v0.1.0 (file:///projects/guessing_game)

   Finished dev [unoptimized + debuginfo] target(s) in 0.43 secs

    Running `target/debug/guessing_game`

Guess the number!

The secret number is: 58

Please input your guess.

  76

You guessed: 76

Too big!

婕備寒锛佸嵆渚挎槸鍦ㄧ寽娴嬩箣鍓嶆坊鍔犱簡绌烘牸锛岀▼搴忎緷鐒惰兘鍒ゆ柇鍑虹敤鎴风寽娴嬩簡 76銆傚杩愯绋嬪簭鍑犳鏉ユ楠屼笉鍚岀被鍨嬭緭鍏ョ殑鐩稿簲琛屼负锛氱寽涓€涓纭殑鏁板瓧锛岀寽涓€涓繃澶х殑鏁板瓧鍜岀寽涓€涓繃灏忕殑鏁板瓧銆?/p>

 

鐜板湪娓告垙宸茬粡澶т綋涓婅兘鐜╀簡锛屼笉杩囩敤鎴峰彧鑳界寽涓€娆°€傚鍔犱竴涓惊鐜潵鏀瑰彉瀹冨惂锛?/span>

 

浣跨敤寰幆鏉ュ厑璁稿娆$寽娴?/p>

loop 鍏抽敭瀛楀垱寤轰簡涓€涓棤闄愬惊鐜€傚皢鍏跺姞鍏ュ悗锛岀敤鎴峰彲浠ュ弽澶嶇寽娴嬶細

 

鏂囦欢鍚? src/main.rs

 

// --snip--

 

    println!("Thesecret number is: {}", secret_number);

 

   loop {

       println!("Please input your guess.");

 

       // --snip--

 

       match guess.cmp(&secret_number) {

           Ordering::Less => println!("Too small!"),

           Ordering::Greater => println!("Too big!"),

           Ordering::Equal => println!("You win!"),

       }

    }

}


濡備笂鎵€绀猴紝鎴戜滑灏嗘彁绀虹敤鎴风寽娴嬩箣鍚庣殑鎵€鏈夊唴瀹规斁鍏ヤ簡寰幆銆傜‘淇濊繖浜涗唬鐮侀澶栫缉杩涗簡涓€灞傦紝鍐嶆杩愯绋嬪簭銆傛敞鎰忚繖閲屾湁涓€涓柊闂锛屽洜涓虹▼搴忓繝瀹炲湴鎵ц浜嗘垜浠殑瑕佹眰锛氭案杩滃湴璇锋眰鍙︿竴涓寽娴嬶紝鐢ㄦ埛濂藉儚娌℃硶閫€鍑哄晩锛?/p>

 

鐢ㄦ埛鎬昏兘浣跨敤 ctrl-c 缁堟绋嬪簭銆備笉杩囪繕鏈夊彟涓€涓柟娉曡烦鍑烘棤闄愬惊鐜紝灏辨槸 鈥滄瘮杈冪寽娴嬩笌绉樺瘑鏁板瓧鈥?閮ㄥ垎鎻愬埌鐨刾arse锛氬鏋滅敤鎴疯緭鍏ヤ竴涓潪鏁板瓧绛旀锛岀▼搴忎細宕╂簝銆傜敤鎴峰彲浠ュ埄鐢ㄨ繖涓€鐐规潵閫€鍑猴紝濡備笅鎵€绀猴細

 

$ cargo run

  Compiling guessing_game v0.1.0 (file:///projects/guessing_game)

   Finished dev [unoptimized + debuginfo] target(s) in 1.50 secs

    Running `target/debug/guessing_game`

Guess the number!

The secret number is: 59

Please input your guess.

45

You guessed: 45

Too small!

Please input your guess.

60

You guessed: 60

Too big!

Please input your guess.

59

You guessed: 59

You win!

Please input your guess.

quit

thread 'main' panicked at 'Please type anumber!: ParseIntError { kind: InvalidDigit }', src/libcore/result.rs:785

note: Run with `RUST_BACKTRACE=1` for abacktrace.

error: Process didn't exit successfully:`target/debug/guess` (exit code: 101)


杈撳叆 quit 纭疄閫€鍑轰簡绋嬪簭锛屽悓鏃跺叾浠栦换浣曢潪鏁板瓧杈撳叆涔熶竴鏍枫€傜劧鑰岋紝杩欏苟涓嶇悊鎯筹紝鎴戜滑鎯宠褰撶寽娴嬫纭殑鏁板瓧鏃舵父鎴忚兘鑷姩閫€鍑恒€?/p>

 

鐚滄祴姝g‘鍚庨€€鍑?/p>

璁╂垜浠鍔犱竴涓?break锛屽湪鐢ㄦ埛鐚滃鏃堕€€鍑烘父鎴忥細

 

鏂囦欢鍚? src/main.rs

 

// --snip--

 

       match guess.cmp(&secret_number) {

           Ordering::Less => println!("Too small!"),

           Ordering::Greater => println!("Too big!"),

           Ordering::Equal => {

                println!("You win!");

                break;

           }

       }

    }

}


閫氳繃鍦?You win! 涔嬪悗澧炲姞涓€琛宐reak锛岀敤鎴风寽瀵逛簡绁炵鏁板瓧鍚庝細閫€鍑哄惊鐜€傞€€鍑哄惊鐜篃鎰忓懗鐫€閫€鍑虹▼搴忥紝鍥犱负寰幆鏄?main 鐨勬渶鍚庝竴閮ㄥ垎銆?/p>

 

澶勭悊鏃犳晥杈撳叆

涓轰簡杩涗竴姝ユ敼鍠勬父鎴忔€э紝涓嶈鍦ㄧ敤鎴疯緭鍏ラ潪鏁板瓧鏃跺穿婧冿紝闇€瑕佸拷鐣ラ潪鏁板瓧锛岃鐢ㄦ埛鍙互缁х画鐚滄祴銆傚彲浠ラ€氳繃淇敼 guess 灏?String 杞寲涓簎32 閭i儴鍒嗕唬鐮佹潵瀹炵幇锛屽绀轰緥 2-5 鎵€绀猴細

 

鏂囦欢鍚? src/main.rs

 

// --snip--

 

io::stdin().read_line(&mut guess)

   .expect("Failed to read line");

 

let guess: u32 = match guess.trim().parse(){

   Ok(num) => num,

   Err(_) => continue,

};

 

println!("You guessed: {}",guess);

 

// --snip--

姝や緥蹇界暐闈炴暟瀛楃殑鐚滄祴骞堕噸鏂拌姹傛暟瀛楄€屼笉鏄绋嬪簭宕╂簝

 

灏?expect 璋冪敤鎹㈡垚 match璇彞锛屾槸浠庨亣鍒伴敊璇氨宕╂簝杞崲鍒扮湡姝e鐞嗛敊璇殑鎯敤鏂规硶銆傞』鐭?parse 杩斿洖涓€涓?Result 绫诲瀷锛岃€?Result 鏄竴涓嫢鏈?Ok 鎴?Err 鎴愬憳鐨勬灇涓俱€傝繖閲屼娇鐢ㄧ殑 match 琛ㄨ揪寮忥紝鍜屼箣鍓嶅鐞?cmp 鏂规硶杩斿洖 Ordering 鏃剁敤鐨勪竴鏍枫€?/p>

 

濡傛灉 parse 鑳藉鎴愬姛鐨勫皢瀛楃涓茶浆鎹负涓€涓暟瀛楋紝瀹冧細杩斿洖涓€涓寘鍚粨鏋滄暟瀛楃殑 Ok銆傝繖涓?Ok 鍊间笌 match 绗竴涓垎鏀殑妯″紡鐩稿尮閰嶏紝璇ュ垎鏀搴旂殑鍔ㄤ綔杩斿洖 Ok 鍊间腑鐨勬暟瀛?num锛屾渶鍚庡鎰垮彉鎴愭柊鍒涘缓鐨?guess 鍙橀噺銆?/p>

 

濡傛灉 parse 涓?鑳藉皢瀛楃涓茶浆鎹负涓€涓暟瀛楋紝瀹冧細杩斿洖涓€涓寘鍚洿澶氶敊璇俊鎭殑 Err銆侲rr 鍊间笉鑳藉尮閰嶇涓€涓猰atch 鍒嗘敮鐨?Ok(num) 妯″紡锛屼絾鏄細鍖归厤绗簩涓垎鏀殑 Err(_) 妯″紡锛歘 鏄竴涓€氶厤绗﹀€硷紝鏈緥涓敤鏉ュ尮閰嶆墍鏈?Err 鍊硷紝涓嶇鍏朵腑鏈変綍绉嶄俊鎭€傛墍浠ョ▼搴忎細鎵ц绗簩涓垎鏀殑鍔ㄤ綔锛宑ontinue 鎰忓懗鐫€杩涘叆 loop 鐨勪笅涓€娆″惊鐜紝璇锋眰鍙︿竴涓寽娴嬨€傝繖鏍风▼搴忓氨鏈夋晥鐨勫拷鐣ヤ簡 parse 鍙兘閬囧埌鐨勬墍鏈夐敊璇紒

 

鐜板湪涓囦簨淇卞锛屽彧闇€杩愯 cargo run锛?/p>

 

$ cargo run

  Compiling guessing_game v0.1.0 (file:///projects/guessing_game)

    Running `target/debug/guessing_game`

Guess the number!

The secret number is: 61

Please input your guess.

10

You guessed: 10

Too small!

Please input your guess.

99

You guessed: 99

Too big!

Please input your guess.

foo

Please input your guess.

61

You guessed: 61

You win!


澶浜嗭紒鍐嶆湁鏈€鍚庝竴涓皬鐨勪慨鏀癸紝灏辫兘瀹屾垚鐚滅寽鐪嬫父鎴忎簡锛氳繕璁板緱绋嬪簭渚濈劧浼氭墦鍗板嚭绉樺瘑鏁板瓧銆傚湪娴嬭瘯鏃惰繕濂斤紝浣嗘寮忓彂甯冩椂浼氭瘉浜嗘父鎴忋€傚垹鎺夋墦鍗扮瀵嗘暟瀛楃殑 println!銆?/p>


鏈€缁堜唬鐮佸涓嬶細

 

鏂囦欢鍚? src/main.rs

 

extern crate rand;

 

use std::io;

use std::cmp::Ordering;

use rand::Rng;

 

fn main() {

   println!("Guess the number!");

 

   let secret_number = rand::thread_rng().gen_range(1, 101);

 

   loop {

       println!("Please input your guess.");

 

       let mut guess = String::new();

 

       io::stdin().read_line(&mut guess)

           .expect("Failed to read line");

 

       let guess: u32 = match guess.trim().parse() {

           Ok(num) => num,

           Err(_) => continue,

       };

 

       println!("You guessed: {}", guess);

 

       match guess.cmp(&secret_number) {

           Ordering::Less => println!("Too small!"),

           Ordering::Greater => println!("Too big!"),

            Ordering::Equal => {

                println!("You win!");

                break;

           }

       }

    }

}

姝や緥涓虹寽鐚滅湅娓告垙鐨勫畬鏁翠唬鐮?/p>

 

鎬荤粨

姝ゆ椂姝ゅ埢锛岄『鍒╁畬鎴愪簡鐚滅寽鐪嬫父鎴忥紒瀵逛簬鍒氳捣姝ョ殑灏忕櫧鏉ヨ澶笉瀹规槗锛屽€煎緱搴嗚春锛?/p>

 

杩欐槸涓€涓€氳繃鍔ㄦ墜瀹炶返瀛︿範 Rust 鏂版蹇电殑椤圭洰锛歭et銆乵atch銆佹柟娉曘€佸叧鑱斿嚱鏁般€佷娇鐢ㄥ閮?crate 绛夌瓑锛屾帴涓嬫潵鐨勫嚑绔狅紝鎴戜滑灏嗕細缁х画娣卞叆銆?/p>


锛?/em>寰呯画锛屾槑鏅?鐐癸紝鎴戜滑鍦ㄧ埍濂借€呯ぞ缇ょ瓑浣犱竴鍚屽弬涓庛€傘€傘€傦級


寰€鏈熷共璐ц祫鏂欙細







rust璇█瀛︿範鏁欑▼锛歨ttps://kaisery.github.io/trpl-zh-cn/ch01-00-getting-started.html#%E5%85%A5%E9%97%A8%E6%8C%87%E5%8D%97




鐢╮ust 璇█寮€鍙戜釜灏忔父鎴?> 
   </section> 
   <section class=

鎴戜滑鏄鍥界殑鑺辨湹

鐢╮ust 璇█寮€鍙戜釜灏忔父鎴?> 
 </section> 
 <section class=

Nervos CKB 鍞竴瀹樼綉锛歂ervos.org

娆㈣繋鍏虫敞Nervos Fans

Nerovs CKB 鐖卞ソ鑰呯ぞ鍖?/span>

Nervos Fans濡備笅棰戦亾锛?/span>

NervosFans twitter锛欯nervosfans

NervosFans 寰崥锛欯NervosFans

NeervosFans 寰俊鍏彿锛?span class="mq-83">Nervosfans

鍏ョ兢璇峰姞涔愪箰寰俊锛歴ensus113

缇庢灉澶у啺寰俊锛歺j73226

澶囨敞鍏ョ兢锛?/span>璋㈣阿锛?/span>

鐢╮ust 璇█寮€鍙戜釜灏忔父鎴?> 
  <br> 
  <br> 
 </section> 
</section> 
<section class=

濂藉ソ瀛︿範锛屽ぉ澶╁悜涓婐煈嗮煈嗮煈?/p>


以上是关于的主要内容,如果未能解决你的问题,请参考以下文章

Python 操作Redis

python爬虫入门----- 阿里巴巴供应商爬虫

Python词典设置默认值小技巧

《python学习手册(第4版)》pdf

Django settings.py 的media路径设置

Python中的赋值,浅拷贝和深拷贝的区别