瀛︿範绗旇锛屼粎姝よ€屽凡
Rust鏄潤鎬佺紪璇戣瑷€锛屽湪缂栬瘧鏃跺繀椤荤煡閬撴墍鏈夊彉閲忕殑绫诲瀷锛屽叾涓璕ust鍐呴儴鏈?濂楁満鍒?/p>
- 鍩轰簬瀹氫箟鐨勫彉閲忓€硷紝缂栬瘧鍣ㄨ嚜宸辨帹鏂叾绫诲瀷锛岀被浼糡ypescript.
- 浣嗘槸濡傛灉鍙兘鐨勭被鍨嬫瘮杈冨锛堜緥濡傛妸String杞负鏁存暟鐨刾arse鏂规硶锛夛紝灏卞繀椤绘彁娣诲姞绫诲瀷鏍囨敞锛屽惁鍒欑紪璇戞姤閿欍€?/strong>
fn main() {
let value = "110"; 榛樿鏄?amp;str绫诲瀷锛屼笉闇€瑕佸0鏄庣被鍨?
let new_se: u32 = value.parse().expect("error"); 瀛樺湪澶氱鍙兘锛屽繀椤诲0鏄庣被鍨?
println!("count value is {}", new_se);
}
Rust鏁版嵁绫诲瀷-鏍囬噺绫诲瀷
鏍囬噺绫诲瀷浠h〃涓€涓?strong>鍗曚竴鐨勫€?/strong>銆俁ust鏍囬噺绫诲瀷鍒嗕负
- 鏁存暟绫诲瀷
- 娴偣绫诲瀷
- 甯冨皵绫诲瀷
- 瀛楃绫诲瀷
涓€銆佹暣鏁扮被鍨?/h2>
鏁存暟绫诲瀷鍒嗕负鏃犵鍙锋暣鏁帮紙浠?i 寮€澶达級鍜屾湁绗﹀彿鏁存暟锛堜互 u 寮€澶达級锛屼緥濡?u32琛ㄧず涓€涓棤绗﹀彿鏁存暟绫诲瀷锛屽崰鎹?2浣嶇┖闂淬€?/p>
1.1鏁存暟绫诲瀷缁嗗垎
鏈夌鍙锋暣鏁扮被鍨嬶細鍗冲甫姝h礋鍗曚綅鐨勬暟鎹被鍨嬶紝鍏朵腑缁嗗垎绫诲瀷鏈塱8銆乮6銆乮32銆乮64銆乮128
鏃犵鍙锋暣鏁扮被鍨嬶細鍗充笉甯︽璐熷彿鐨勬暟鎹紝鍗冲彧浼氬ぇ浜?锛岀粏鍒嗙被鍨嬫湁u8銆乽16銆乽32銆乽64銆乽128
鍏朵腑绗﹀彿鍚庨潰璺熺殑鏁板瓧琛ㄧず鍏跺崰澶氬皯浣嶇┖闂达紝涔熸爣璇嗗叾鍙栧€肩殑鑼冨洿锛屼緥濡?br>
u8绫诲瀷锛屽叾鑼冨洿涓猴細0鍒?鐨?娆℃柟-1锛屽嵆0~255
i8绫诲瀷锛屽叾鑼冨洿涓猴細-255~255
娉ㄦ剰鐐癸細鏁存暟绫诲瀷榛樿涓篿32绫诲瀷
鏁存暟绫诲瀷鐨勫€硷紝鍙互浣跨敤涓嬪垝绾垮鍔犲彲璇绘€э紝姣斿锛歭et count = 100_0000 琛ㄧず100涓囩殑鏁板€?/p>
1.2鏁存暟婧㈠嚭
鍋囧锛氬畾涔塽8绫诲瀷鏄暣鏁帮紝浣嗘槸鍊肩粰浜?66锛岃秴杩噓8绫诲瀷鐨勬渶澶ц寖鍥?55锛岄偅涔?/p>
- 鍦ㄥ紑鍙戞ā寮忎笅锛孯ust浼氭鏌ュ嚭鏁存暟婧㈠嚭锛岀▼搴忎細鎶ラ敊
- 鐢熶骇鐗堟湰涓嬶紝Rust涓嶄細妫€鏌ユ暣鏁版孩鍑猴紝濡傛灉鍙戠敓婧㈠嚭锛屽叾浼氭寚鍚?code>鐜粫鎿嶄綔
- 256鍥炲彉鎴?锛?57鍙樻垚1,浠ユ绫绘帹銆?/li>
- 绋嬪簭涓嶄細鎶ラ敊
浜屻€佹诞鐐圭被鍨?/h2>
Rust鏈?绉嶅熀纭€鐨勬诞鐐圭被鍨?/p>
- f32锛?2浣嶏紝鍗曠簿搴?/li>
- f64锛?4浣嶏紝鍙岀簿搴?/li>
Rust榛樿浣跨敤f64绫诲瀷.
Rust鏁板€兼搷浣?/h4>
Rust閽堝鏁板€肩被鍨嬬殑鏁版嵁锛屾敮鎸侊細
- 鍔犲噺涔橀櫎浣?/li>
fn main() {
let sum = 1+1;
let sum = 1.1-1.1;
let sum = 2.1*1.1;
let sum = 2/1;
let sum = 2%1;
println!("count value is {}", sum);
}
涓夈€佸竷灏旂被鍨?/h2>
Rust甯冨皵绫诲瀷鏈?涓€硷細true鍜宖alse锛屽崰涓€涓瓧鑺傚ぇ灏忥紝绫诲瀷绗﹀彿鏄痓ool銆?/p>
fn main() {
let f = true;
let p: bool = false;
}
鍥涖€佸瓧绗︾被鍨?/h2>
Rust鐨勭殑鏍囬噺绫诲瀷鐨勫瓧绗︾被鍨嬩负char绫诲瀷锛?/p>
- 瀛楃绫诲瀷鐨勫€间娇鐢ㄥ崟寮曞彿
- 鍗犵敤4涓瓧鑺傚ぇ灏?/li>
- 鏀寔鎷奸煶銆佷腑鏃ラ煩鏂囥€乪moji琛ㄦ儏
fn main() {
let f =鈥榮鈥?
let p:char ='a';
let p:char ='馃槃';
}
杩欓噷鎸囩殑鏄崟涓€鍊硷紝鍗砪har绫诲瀷鐨勫€煎彧鏈変竴涓紝姣斿鈥榮鈥欙紝澶氫釜瀛楃涓嶈兘浣跨敤char绫诲瀷銆?/p>
Rust鏁版嵁绫诲瀷-澶嶅悎绫诲瀷
澶嶅悎绫诲瀷鍗冲彲浠ュ皢澶氫釜鍊兼斁鍒颁竴涓€奸噷闈€俁ust澶嶅悎绫诲瀷鍒嗕负2绉嶏細
Tuple 鍏冪粍
- 鍙互瀛樻斁澶氱鏁版嵁绫诲瀷
- 闀垮害鏄浐瀹氱殑锛屼竴鏃﹀0鏄庡氨鏃犳硶鏀瑰彉
Tuple鐨勫垱寤猴紝鍙互鏄剧ず鐨勫0鏄庣被鍨嬶紝涔熷彲浠ョ渷鐣ャ€?/p>
let tup = (1,2,3,4.1)
Tuple鍊肩殑鑾峰彇锛屽彲浠ヤ娇鐢ㄨВ鏋勶紝涔熷彲浠ヤ娇鐢ㄧ偣璁块棶涓嬫爣鐨勬柟寮?/p>
let tup = (1,2,3,4.1)
let (x,y,z) = tup; 浣跨敤瑙f瀯鏂瑰紡
let s = tup.0; 浣跨敤璁块棶涓嬫爣鐨勬柟寮?
鏁扮粍
- 鍙兘瀛樻斁鍗曚竴鏁版嵁绫诲瀷
- 闀垮害鏄浐瀹氱殑锛屼竴鏃﹀0鏄庡氨鏃犳硶鏀瑰彉
- 鏁扮粍鏄瓨鍦ㄦ爤鍐呭瓨涓殑
鏁扮粍鐨勫垱寤猴紝鍙互鏄剧ず鐨勫0鏄庣被鍨嬶紝涔熷彲浠ョ渷鐣ャ€?/p>
let a = [1,2,3]; rust鎺ㄦ柇涓篬i32;3]
鏁扮粍绫诲瀷鐨勫0鏄庝娇鐢細[绫诲瀷;闀垮害]
let a: [u8; 3] = [1, 2, 3];
閽堝鏁扮粍鍐呭涓€鏍风殑鎯呭喌锛屽彲浠ョ畝鍐?/strong>
let a = [3;5]; 鐩稿綋浜?let a = [3,3,3,3,3]
鏁扮粍鍊肩殑鑾峰彇锛屽彲浠ヤ娇鐢ㄨ闂笅鏍囩殑鏂瑰紡
let a = [0,1,2,3];
let x = a[0]; 0
let y = a[1]; 1
娉ㄦ剰鐨勬槸锛氬浜庢暟缁勭储寮曞彇鍊艰秴杩囨渶澶у€兼椂锛孯ust缂栬瘧浼氶€氳繃锛屼絾鏄細鍦ㄨ繍琛屾椂鎶ラ敊銆?/p>
鏁扮粍鍊肩殑鑾峰彇锛屼篃鍙互浣跨敤瑙f瀯锛屽鏋滄暟缁勫唴瀹瑰锛屼綘鍙兂鍙栧墠鍑犱釜鍏冪礌锛屽悗闈㈠彲浠ュ啓..璇硶
let a = [0,1,2,3];
let [x, y, ..] = a; 鍙В鏋勫嚭绗竴涓厓绱犲拰绗簩涓厓绱?