当前位置: 首页>后端>正文

Rust基本数据类型

[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
    */
}
Rust基本数据类型,第1张

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
    }
}

https://www.xamrdz.com/backend/3zw1933878.html

相关文章: