[TOC]
Rust鍩烘湰鏁版嵁绫诲瀷
绫诲瀷绯荤粺姒傝堪
浠€涔堟槸绫诲瀷锛熺被鍨嬫槸瀵逛簩杩涘埗鏁版嵁鐨勪竴绉嶇害鏉熻涓恒€傜被鍨嬫瘮璧风洿鎺ヤ娇鐢ㄤ簩杩涘埗鏁版嵁锛屾湁璁稿浼樺娍锛?/p>
- 鍑忓皯寮€鍙戣€呭績鏅鸿礋鎷?/li>
- 瀹夊叏
- 瀹规槗浼樺寲
甯歌鐨勭被鍨嬪垎绫伙細
- 闈欐€佺被鍨嬶細鍦ㄧ紪璇戞湡瀵圭被鍨嬭繘琛屾鏌?/li>
- 鍔ㄦ€佺被鍨嬶細鍦ㄨ繍琛屾湡瀵圭被鍨嬭繘琛屾鏌?/li>
- 寮虹被鍨嬶細涓嶅厑璁搁殣寮忕被鍨嬭浆鎹?/li>
- 寮辩被鍨嬶細鍏佽杩涜闅愬紡绫诲瀷杞崲
C 璇█鐢变簬鍏佽闅愬紡绫诲瀷杞崲鍥犳鏄潤鎬佸急绫诲瀷璇█锛岃澶氫汉鏄撳皢 C 璇█璇涓洪潤鎬佸己绫诲瀷锛岄渶瑕佺壒鍒敞鎰忥細
int main() {
long a = 10;
return a;
}
- Rust 鏄潤鎬佸己绫诲瀷璇█
鍙橀噺鍜屽彲鍙樻€?/h2>
鍒涘缓鍜屼娇鐢ㄥ彉閲?/h3>
鍦?Rust 浠g爜涓紝鍙互浣跨敤 let
鍏抽敭瀛楀皢鍊肩粦瀹氬埌鍙橀噺锛?/p>
fn main() {
let x = 5;
println!("The value of x is: {}", x);
}
println
鏄竴涓畯锛屽畠鏄渶甯哥敤鐨勫皢鏁版嵁鎵撳嵃鍦ㄥ睆骞曚笂鐨勬柟娉曘€傜洰鍓嶏紝鎴戜滑鍙互绠€鍗曞湴灏嗗畠瑙嗕负涓€涓嫢鏈夊彲鍙樺弬鏁版暟閲忕殑鍑芥暟锛屽湪鍚庨潰鐨勭珷鑺備腑鎴戜滑浼氬瀹忚繘琛岃缁嗙殑璁ㄨ銆?/p>
鍙彉鎬?/h3>
鍦?Rust 涓紝鍙橀噺榛樿鏄笉鍙彉鐨勶紝涓€鏃︿竴涓€肩粦瀹氬埌涓€涓悕绉帮紝灏变笉鑳芥洿鏀硅鍊硷細
fn main() {
let x = 5;
println!("The value of x is: {}", x);
x = 6; // cannot assign twice to immutable variable `x`
println!("The value of x is: {}", x);
}
浣嗘湁鏃跺€欏厑璁稿彉閲忓彲鍙樻槸闈炲父鏈夌敤鐨勩€傞€氳繃鍦ㄥ彉閲忓悕鍓嶉潰娣诲姞 mut
鏉ヤ娇瀹冧滑鍙彉锛?/p>
fn main() {
let mut x = 5;
println!("The value of x is: {}", x);
x = 6;
println!("The value of x is: {}", x);
}
甯搁噺鍜屽彉閲?/h3>
涓嶅彲鍙樺彉閲忓鏄撹浣犺仈鎯冲埌鍙︿竴涓蹇碉細甯搁噺銆傚湪 Rust 涓紝甯搁噺浣跨敤 const
瀹氫箟锛岃€屽彉閲忎娇鐢?let
瀹氫箟锛?/p>
- 涓嶅厑璁稿甯搁噺浣跨敤淇グ璇?
mut
锛屽父閲忓缁堟槸涓嶅彲鍙樼殑 - 蹇呴』鏄剧ず鏍囨敞甯搁噺鐨勭被鍨?/li>
- 甯搁噺鍙互鍦ㄤ换浣曚綔鐢ㄥ煙涓0鏄庯紝鍖呮嫭鍏ㄥ眬浣滅敤鍩?/li>
- 甯搁噺鍙兘璁剧疆涓哄父閲忚〃杈惧紡锛岃€屼笉鑳借缃负鍑芥暟璋冪敤鐨勭粨鏋滄垨鍙兘鍦ㄨ繍琛屾椂璁$畻鐨勪换浣曞叾浠栧€?/li>
const A_CONST: i32 = 1;
闅愯棌锛圫hadowing锛?/h3>
鍙互澹版槑涓€涓笌鍓嶄竴涓彉閲忓悓鍚嶇殑鏂板彉閲忥紝骞朵笖鏂板彉閲忎細闅愯棌鍓嶄竴涓彉閲忥紝杩欑鎿嶄綔琚О涓洪殣钘忥紙Shadowing锛夈€?/p>
fn main() {
let x = 5;
let x = x + 1;
let x = x * 2;
println!("The value of x is: {}", x);
}
鍩虹鏁版嵁绫诲瀷
Rust 鏄竴闂ㄩ潤鎬佺紪绋嬭瑷€锛屾墍鏈夊彉閲忕殑绫诲瀷蹇呴』鍦ㄧ紪璇戞湡灏辫鏄庣‘鍥哄畾銆?/p>
鏁存暟
Rust 涓湁 12 绉嶄笉鍚岀殑鏁存暟绫诲瀷锛?/p>
闀垮害 | 鏈夌鍙?/th> | 鏃犵鍙?/th> |
---|---|---|
8-bit | i8 | u8 |
16-bit | i16 | u16 |
32-bit | i32 | u32 |
64-bit | i64 | u64 |
128-bit | i128 | u128 |
arch | isize | usize |
- 瀵逛簬鏈槑纭爣娉ㄧ被鍨嬬殑鏁存暟锛孯ust 榛樿閲囩敤 i32.
- isize 鍜?usize 鏍规嵁绯荤粺鐨勪笉鍚岃€屾湁涓嶅悓鐨勯暱搴?
娴偣鏁?/h3>
Rust 鏈変袱绉嶆诞鐐规暟绫诲瀷锛屼负 f32
鍜?f64
锛屽悗鑰呯簿搴︽洿楂樸€傚浜庢湭鏄庣‘鏍囨敞绫诲瀷鐨勫皬鏁帮紝Rust 榛樿閲囩敤 f64
.
fn main() {
let x = 2.0; // f64
let y: f32 = 3.0; // f32
}
甯冨皵鍊?/h3>
涓庡ぇ澶氭暟鍏朵粬缂栫▼璇█涓€鏍凤紝Rust 涓殑甯冨皵绫诲瀷鏈変袱涓彲鑳界殑鍊硷細true
鍜?false
銆傚竷灏斿€肩殑澶у皬鏄竴涓瓧鑺傘€?/p>
fn main() {
let t = true;
let f: bool = false;
}
瀛楃
Rust 鏀寔鍗曚釜瀛楃锛屽瓧绗︿娇鐢ㄥ崟寮曞彿鍖呰銆?/p>
fn main() {
let c = 'z';
let z = '鈩?;
let heart_eyed_cat = '馃樆';
}
浣滀笟: 姹備袱涓棤绗﹀彿鏁扮殑骞冲潎鏁?/h2>
缂栧啓涓€涓嚱鏁帮紝瀹冩帴鏀朵袱涓?u32
绫诲瀷鍙傛暟骞惰繑鍥炲畠浠殑骞冲潎鏁般€?/p>
fn avg(a: u32, b: u32) -> u32 {
// 琛ュ厖浣犵殑浠g爜
(a & b) + ((a ^ b) >> 1)
}
- 鎻愮ず锛氬繀椤昏€冭檻鏁存暟婧㈠嚭闂銆?/li>
涓€浜涙湁鐢ㄧ殑娴嬭瘯鐢ㄤ緥锛?/p>
fn main() {
assert_eq!(avg(4294967295, 4294967295), 4294967295);
assert_eq!(avg(0, 0), 0);
assert_eq!(avg(10, 20), 15);
assert_eq!(avg(4294967295, 1), 2147483648);
println!("passed")
}
鏁存暟婧㈠嚭
鍦ㄧ數鑴戦鍩熼噷鎵€鍙戠敓鐨勬孩鍑烘潯浠舵槸锛岃繍琛屽崟椤规暟鍊艰绠楁椂锛屽綋璁$畻浜х敓鍑烘潵鐨勭粨鏋滄槸闈炲父澶х殑锛屽ぇ浜庡瘎瀛樺櫒鎴栧瓨鍌ㄥ櫒鎵€鑳藉瓨鍌ㄦ垨琛ㄧず鐨勮兘鍔涢檺鍒跺氨浼氬彂鐢熸孩鍑恒€?/p>
鍦ㄤ笉鍚岀殑缂栫▼璇█涓紝瀵瑰緟婧㈠嚭閫氬父鏈変互涓嬪嚑绉嶄笉鍚岀殑鍋氭硶锛?/p>
- 宕╂簝锛氬綋婧㈠嚭琚睛娴嬪埌鏃讹紝绋嬪簭绔嬪嵆閫€鍑鸿繍琛?/li>
- 蹇界暐锛氳繖鏄渶鏅亶鐨勪綔娉曪紝蹇界暐浠讳綍绠楁暟婧㈠嚭
瀵逛簬婧㈠嚭鐨勫鐞嗘柟娉曪紝Rust 鍦?debug 涓?release 妯″紡涓嬫槸涓嶅悓鐨勩€傚湪 debug 妯″紡涓嬬紪璇戞椂锛孯ust 浼氭鏌ユ暣鏁版孩鍑猴紝濡傛灉鍙戠敓杩欑琛屼负锛屼細瀵艰嚧绋嬪簭鍦ㄨ繍琛屾椂缁堟骞舵姤鍑鸿繍琛屾椂閿欒銆傝€屽鏋滃湪 release 妯″紡涓嬬紪璇戞椂锛孯ust 涓嶄細瀵规暣鏁版孩鍑鸿繘琛屾鏌ャ€?/p>
瑕佹樉寮忓鐞嗘孩鍑猴紝鍙互浣跨敤鏍囧噯搴撴彁渚涚殑涓€浜?.overflowing_*
鏂规硶锛?/p>
fn main() {
let a: u32 = 4294967295;
let b: u32 = 1;
let (r, is_overflow) = a.overflowing_add(b);
println!("r={} is_overflow={}", r, is_overflow);
}
鍏冪粍
鍏冪粍鏄皢澶氫釜鍏锋湁鍚勭绫诲瀷鐨勫€肩粍鍚堟垚涓€涓鍚堢被鍨嬬殑閫氱敤鏂规硶銆傚厓缁勬湁鍥哄畾鐨勯暱搴︼細涓€鏃﹀0鏄庯紝瀹冧滑鐨勫ぇ灏忓氨涓嶈兘澧為暱鎴栨敹缂┿€?/p>
鎴戜滑閫氳繃鍦ㄦ嫭鍙峰唴鍐欎竴涓€楀彿鍒嗛殧鐨勫€煎垪琛ㄦ潵鍒涘缓涓€涓厓缁勩€傚厓缁勪腑鐨勬瘡涓綅缃兘鏈変竴涓被鍨嬶紝鍏冪粍涓笉鍚屽€肩殑绫诲瀷涓嶅繀鐩稿悓銆?/p>
fn main() {
let a: i32 = 10;
let b: char = 'A';
// 鍒涘缓涓€涓厓缁?
let mytuple: (i32, char) = (a, b);
// 浠庡厓缁勪腑璇诲彇涓€涓€?
println!(".0={:?}", mytuple.0);
println!(".1={:?}", mytuple.1);
// 瑙e皝瑁?
let (c, d) = mytuple;
println!("c={} d={}", c, d);
}
鏁扮粍
鍙︿竴绉嶆嫢鏈夊涓暟鎹泦鍚堢殑鏂规硶鏄娇鐢ㄦ暟缁勩€備笌鍏冪粍涓嶅悓锛屾暟缁勪腑鐨勬瘡涓厓绱犻兘蹇呴』鍏锋湁鐩稿悓鐨勭被鍨嬨€俁ust 涓殑鏁扮粍涓嶅悓浜庡叾浠栦竴浜涜瑷€涓殑鏁扮粍锛孯ust 涓殑鏁扮粍鍏锋湁鍥哄畾闀垮害銆?/p>
鏁扮粍涓嬫爣浠?0 寮€濮嬶紝鍚屾椂 Rust 瀛樺湪瓒婄晫妫€鏌ワ細
fn main() {
// 鍒涘缓鏁扮粍, [i32; 3] 鏄暟缁勭殑绫诲瀷鎻愮ず, 琛ㄧず鍏冪礌鐨勭被鍨嬫槸 i32, 鍏辨湁 3 涓厓绱?
let myarray: [i32; 3] = [1, 2, 3];
// 鏍规嵁绱㈠紩鑾峰彇涓€涓€? 鏁扮粍涓嬫爣浠?0 寮€濮?
println!("{:?}", myarray[1]);
// 绱㈠紩涓嶈兘瓒婄晫
println!("{:?}", myarray[3]);
// 濡傛灉鏁扮粍鐨勬瘡涓厓绱犻兘鏈夌浉鍚岀殑鍊? 鎴戜滑杩樺彲浠ョ畝鍖栨暟缁勭殑鍒濆鍖?
let myarray: [i32; 3] = [0; 3];
println!("{:?}", myarray[1]);
}
鍒囩墖绫诲瀷
鍒囩墖绫诲瀷鏄涓€涓暟缁勶紙鍖呮嫭鍥哄畾澶у皬鏁扮粍鍜屽姩鎬佹暟缁勶級鐨勫紩鐢ㄧ墖娈碉紝鏈夊埄浜庡畨鍏ㄦ湁鏁堝湴璁块棶鏁扮粍鐨勪竴閮ㄥ垎锛岃€屼笉闇€瑕佹嫹璐濇暟缁勬垨鏁扮粍涓殑鍐呭銆傚垏鐗囧湪缂栬瘧鐨勬椂鍊欏叾闀垮害鏄湭鐭ョ殑锛屽湪搴曞眰瀹炵幇涓婏紝涓€涓垏鐗囦繚瀛樼潃涓や釜 uszie
鎴愬憳锛岀涓€涓?usize
鎴愬憳鎸囧悜鍒囩墖璧峰浣嶇疆鐨勬寚閽堬紝绗簩涓?usize
鎴愬憳琛ㄧず鍒囩墖闀垮害锛?/p>
fn main() {
let arr: [i32; 5] = [1, 2, 3, 4, 5];
let slice = &arr[0..3]; // 鍙栧墠 3 涓厓绱狅紝.. 鏄?Rust Range 璇硶锛?amp; 鏄紩鐢ㄧ鍙?
println!("slice[0]={}, len={}", slice[0], slice.len());
}
缁撴瀯浣?/h2>
缁撴瀯浣撴槸澶氱涓嶅悓鏁版嵁绫诲瀷鐨勭粍鍚堛€傚畠涓庡厓缁勭被浼硷紝浣嗗尯鍒湪浜庢垜浠彲浠ヤ负姣忎釜鎴愬憳鍛藉悕銆傚彲浠ヤ娇鐢?struct
鍏抽敭瀛楀垱寤轰笁绉嶇被鍨嬬殑缁撴瀯锛?/p>
- 鍏冪粍缁撴瀯
- 缁忓吀鐨?C 缁撴瀯
- 鏃犲瓧娈电殑鍗曞厓缁撴瀯
缁撴瀯浣撲娇鐢ㄩ┘宄板懡鍚嶏細
// 鍏冪粍缁撴瀯
struct Pair(i32, f32);
// 缁忓吀鐨?C 缁撴瀯
struct Person {
name: String,
age: u8,
}
// 鏃犲瓧娈电殑鍗曞厓缁撴瀯, 鍦ㄦ硾鍨嬩腑杈冧负甯哥敤
struct Unit;
fn main() {
// 缁撴瀯浣撶殑瀹炰緥鍖?
let pair = Pair(10, 4.2);
let person = Persion {
name: String::from("jack"),
age: 21,
};
let unit = Unit;
// 浠庣粨鏋勪綋涓幏鍙栨垚鍛?
println!("{}", pari.0);
println!("{}", persion.name);
}
鏋氫妇
enum
鍏抽敭瀛楀彲鍒涘缓鏋氫妇绫诲瀷銆傛灇涓剧被鍨嬪寘鍚簡鍙栧€肩殑鍏ㄩ儴鍙兘鐨勬儏鍐点€傚湪 Rust 涓紝鏈夊绉嶄笉鍚屽舰寮忕殑鏋氫妇鍐欐硶銆?/p>
鏃犲弬鏁扮殑鏋氫妇
enum Planet {
Mars,
Earth,
}
涓婇潰鐨勪唬鐮佸畾涔変簡鏋氫妇 Planet锛屽寘鍚簡涓や釜鍊?Mars 鍜?Earth銆?/p>
甯︽灇涓惧€肩殑鏋氫妇
enum Color {
Red = OxffOOOO,
Green = OxOOffOO,
Blue = OxOOOOff,
}
甯﹀弬鏁扮殑鏋氫妇
Rust 杩樻敮鎸佹惡甯︾被鍨嬪弬鏁扮殑鏋氫妇锛?/p>
enum IpAddr {
IPv4(u8, u8, u8, u8),
IPv6(u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8),
}
妯″紡鍖归厤
鏋氫妇閫氬父涓?match
妯″紡鍖归厤涓€璧蜂娇鐢細
enum IpAddr {
IPv4(u8, u8, u8, u8),
IPv6(u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8),
}
fn main() {
let localhost: IpAddr = IpAddr::IPv4(127, 0, 0, 1);
match localhost {
IpAddr::IPv4(a, b, c, d) => {
println!("{} {} {} {}", a, b, c, d);
}
_ => {} // 浠讳綍闈?IPv4 绫诲瀷璧拌繖鏉″垎鏀?
}
}
鍚勭娉ㄩ噴绫诲瀷
涓庤澶氱幇浠h瑷€涓€鏍凤紝Rust 涔熸敮鎸佷赴瀵岀殑娉ㄩ噴绉嶇被锛屾垜浠彲浠ラ€氳繃娉ㄩ噴鏉ヤ簡瑙d竴娈典唬鐮佸共浜嗕粈涔堝伐浣滐紝鐢氳嚦鍙互鐩存帴閫氳繃娉ㄩ噴鐢熸垚鏂囨。銆?/p>
鏅€氱殑娉ㄩ噴
// 浣跨敤 // 娉ㄩ噴鍗曡
/*
涔熷彲浠ヤ娇鐢?/* */ 娉ㄩ噴澶氳, 杩欎竴鐐逛笌 C 璇█鏄竴鏍风殑
*/
鏂囨。娉ㄩ噴
鏂囨。娉ㄩ噴鏄竴绉?Markdown 鏍煎紡鐨勬敞閲婏紝鐢ㄤ簬瀵规枃妗d腑鐨勪唬鐮佺敓鎴愭枃妗c€傚彲浠ヤ娇鐢?cargo doc 宸ュ叿鐢熸垚 HTML 鏂囨尅銆?/p>
//! 杩欐槸妯″潡绾у埆鐨勬枃妗f敞閲? 涓€鑸敤浜庢ā鍧楁枃浠剁殑澶撮儴
/// 杩欐槸鏂囨。娉ㄩ噴, 涓€鑸敤浜庡嚱鏁版垨缁撴瀯浣撶殑璇存槑, 缃簬璇存槑瀵硅薄鐨勪笂鏂?
struct Person;
渚嬪瓙
涓嬮潰鐨勪唬鐮佹紨绀轰簡鏂愭尝閭e鍑芥暟鍙婂叾娉ㄩ噴锛屼娇鐢?cargo doc 鏋勫缓 HTML 鏂囨。锛?/p>
//! A main project provides fibonacci function
/// In mathematics, the Fibonacci numbers, commonly denoted Fn form a sequence, called the Fibonacci sequence, such that
/// each number is the sum of the two preceding ones, starting from 0 and 1. That is
/// ```
/// F(0) = 0
/// F(1) = 1
/// F(n) = F(n 鈭?1) + F(n 鈭?2)
/// ```
fn fibo(n: u32) -> u32 {
if n== 0 || n == 1 {
n
} else {
fibo(n - 1) + fibo(n - 2)
}
}
fn main() {
// Calculate fibo(10)
println!("fibo(10) = {}", fibo(10));
/*
The result should be 55
*/
}
println鍑芥暟
println!
鐢ㄤ簬灏嗘暟鎹墦鍗板埌鏍囧噯杈撳嚭锛屼笖鍦ㄦ暟鎹湯灏捐嚜鍔ㄥ甫涓婃崲琛岀銆傚湪鎵€鏈夊钩鍙颁笂锛屾崲琛岀閮芥槸鎹㈣绗︼紙娌℃湁棰濆鐨勫洖杞︾锛夈€?/p>
浣跨敤 println!
鐢ㄤ簬绋嬪簭鐨勬甯歌緭鍑猴紝浣跨敤 eprintln!
鎵撳嵃閿欒鎴栬€呰繘搴︽潯銆傚墠鑰呮暟鎹鍐欏叆 stdout
锛屽悗鑰呭垯鏄?stderr
銆?code>println! 瀹忓父鐢ㄧ殑鏍煎紡鍖栬娉曞涓嬫墍绀猴細
fn main() {
// `{}` 浼氳鍙橀噺鍐呭鏇挎崲, 杩欐槸鏈€甯歌鐨勪竴绉嶇敤娉?
println!("{}", 42);
// 鍙互浣跨敤棰濆鐨勪綅缃弬鏁?
println!("{0}{1}{0}", 4, 2);
// 浣跨敤鍛藉悕鍙傛暟.
println!("name={name} age={age}", name="jack", age=6);
// 鍙互鍦?`:` 鍚庨潰鎸囧畾鐗规畩鐨勬牸寮?
println!("{} of {:b} people know binary, the other half don't", 1, 2);
// 鍙互鎸夋寚瀹氬搴︽潵鍙冲榻愭枃鏈?
println!("{number:>width$}", number=1, width=6);
// 鍦ㄦ暟瀛楀乏杈硅ˉ 0.涓嬮潰璇彞杈撳嚭 "000001".
println!("{number:>0width$}", number=1, width=6);
// println! 浼氭鏌ヤ娇鐢ㄥ埌鐨勫弬鏁版暟閲忔槸鍚︽纭?
println!("My name is {0}, {1} {0}", "Bond");
// 缂栬瘧灏嗕細鎶ラ敊, 璇疯ˉ涓婃紡鎺夌殑鍙傛暟锛?James"
}
鍦ㄤ笉鍚岀被鍨嬩箣闂磋浆鎹?/h2>
Rust 鏄竴闂ㄥ己绫诲瀷璇█锛屽洜姝や笉鏀寔闅愬紡绫诲瀷杞崲銆俁ust 涓轰簡瀹炵幇绫诲瀷涔嬮棿鐨勮浆鎹㈡彁渚涗簡鍑犵涓嶅悓鐨勬柟娉曘€?/p>
as 璇硶
as
璇硶鏄?Rust 鏈€鍩虹鐨勪竴绉嶇被鍨嬭浆鎹㈡柟娉曪紝瀹冮€氬父鐢ㄤ簬鏁存暟锛屾诞鐐规暟鍜屽瓧绗︽暟鎹箣闂寸殑绫诲瀷杞崲锛?/p>
fn main() {
let a: i8 = -10;
let b = a as u8;
println!("a={} b={}", a, b);
}
鏁板€艰浆鎹㈢殑璇箟鏄細
- 涓や釜鐩稿悓澶у皬鐨勬暣鍨嬩箣闂达紙渚嬪锛歩32 -> u32锛夌殑杞崲鏄竴涓?no-op
- 浠庝竴涓ぇ鐨勬暣鍨嬭浆鎹负涓€涓皬鐨勬暣鍨嬶紙渚嬪锛歶32 -> u8锛変細鎴柇
- 浠庝竴涓皬鐨勬暣鍨嬭浆鎹负涓€涓ぇ鐨勬暣鍨嬶紙渚嬪锛歶8 -> u32锛変細
- 濡傛灉婧愮被鍨嬫槸鏃犵鍙风殑浼氳ˉ闆讹紙zero-extend锛?/li>
- 濡傛灉婧愮被鍨嬫槸鏈夌鍙风殑浼氱鍙凤紙sign-extend锛?/li>
- 浠庝竴涓诞鐐硅浆鎹负涓€涓暣鍨嬩細鍚?0 鑸嶅叆
- 浠庝竴涓暣鍨嬭浆鎹负涓€涓诞鐐逛細浜х敓鏁村瀷鐨勬诞鐐硅〃绀猴紝濡傛湁蹇呰浼氳垗鍏ワ紙鏈寚瀹氳垗鍏ョ瓥鐣ワ級
- 浠?f32 杞崲涓?f64 鏄畬缇庢棤缂虹殑
- 浠?f64 杞崲涓?f32 浼氫骇鐢熸渶鎺ヨ繎鐨勫彲鑳藉€硷紙鏈寚瀹氳垗鍏ョ瓥鐣ワ級
transmute
as
鍙厑璁稿畨鍏ㄧ殑杞崲锛屼緥濡備細鎷掔粷渚嬪灏濊瘯灏?4 涓瓧鑺傝浆鎹负涓€涓?u32
锛?/p>
let a = [0u8, 0u8, 0u8, 0u8];
let b = a as u32; // Four u8s makes a u32.
浣嗘槸鎴戜滑鐭ラ亾 u32
鍦ㄥ唴瀛樹腑琛ㄧず涓?4 涓繛缁殑 u8
锛屽洜姝ゆ垜浠彲浠ヤ娇鐢ㄤ竴绉嶅嵄闄╃殑鏂规硶锛氬憡璇夌紪璇戝櫒鐩存帴浠ュ彟涓€绉嶆暟鎹被鍨嬪寰呭唴瀛樹腑鐨勬暟鎹€傜紪璇戝櫒浼氭棤鏉′欢淇′换浣狅紝浣嗘槸锛岄櫎闈炰綘鐭ラ亾鑷繁鍦ㄥ共浠€涔堬紝涓嶇劧骞朵笉鎺ㄨ崘浣跨敤 transmute
銆傝浣跨敤 transmute
锛岄渶瑕佸皢浠g爜鍐欏叆 unsafe
鍧椾腑锛?/p>
fn main() {
unsafe {
let a = [0u8, 1u8, 0u8, 0u8];
let b = mem::transmute::<[u8; 4], u32>(a);
println!("{}", b); // 256
// Or, more concisely:
let c: u32 = mem::transmute(a);
println!("{}", c); // 256
}
}