1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
/// # Unsafe Rust介绍 /// /// - unsafe关键字和unsafe块 /// - 什么情况下需要用unsafe? /// - 什么情况下函数需要标记unsafe ? /// - 什么情况下trait需要标记unsafe ? /// - 什么情况下需要使用unsafe块 ? /// /// Basic usage: Unsafe中依旧会进行借用检查 /// /// /// `error[E0502]`: cannot borrow `a` as mutable because it is also borrowed as immutable /// /// ``` /// fn main(){ /// unsafe { /// let mut a = "hello"; /// let b = &a; /// let c = &mut a; /// } /// } /// ``` /// /// Basic usage: Unsafe块示意 /// /// ``` /// fn main() { /// let hello = vec![104, 101, 108, 108, 111]; /// let hello = unsafe { /// // 该函数为unsafe函数 /// String::from_utf8_unchecked(hello) /// }; /// assert_eq!("hello", hello); /// } /// ``` /// /// Basic usage: 访问和修改可变静态变量 /// /// ```rust /// static mut COUNTER: u32 = 0; /// fn main() { /// let inc = 3; /// unsafe { /// COUNTER += inc; /// println!("COUNTER: {}", COUNTER); /// } /// } /// ``` pub fn unsafe_intro(){ unimplemented!(); } /// # Union联合体 /// /// 也叫共用体、Untagged Union /// /// Basic usage: 使用Union和Struct来模拟Enum - V1 /// /// Error: 当前Union不支持Non-Copy类型 /// /// ```rust /// // #![feature(untagged_unions)] /// #[repr(C)] /// union U { /// i: i32, /// f: f32, /// } /// #[repr(C)] /// struct Value{ /// tag: u8, /// value: U, /// } /// #[repr(C)] /// union MyZero { /// i: Value, /// f: Value, /// } /// enum MyEnumZero { /// I(i32), /// F(f32), /// } /// fn main(){ /// let int_0 = MyZero{i: Value{tag: b'0', value: U { i: 0 } } }; /// let float_0 = MyZero{i: Value{tag: b'1', value: U { f: 0.0 } } }; /// } /// ``` /// /// Basic usage: 使用Union和Struct来模拟Enum - V2 /// /// /// ```rust /// #[repr(u32)] /// enum Tag { I, F } /// #[repr(C)] /// union U { /// i: i32, /// f: f32, /// } /// #[repr(C)] /// struct Value { /// tag: Tag, /// u: U, /// } /// fn is_zero(v: Value) -> bool { /// unsafe { /// match v { /// Value { tag: Tag::I, u: U { i: 0 } } => true, /// Value { tag: Tag::F, u: U { f: 0.0 } } => true, /// _ => false, /// } /// } /// } /// fn main() { /// let int_0 = Value{tag: Tag::I, u: U{i: 0}}; /// let float_0 = Value{tag: Tag::F, u: U{f: 0.0}}; /// assert_eq!(true, is_zero(int_0)); /// assert_eq!(true, is_zero(float_0)); /// assert_eq!(4, std::mem::size_of::<U>()); /// } /// ``` /// /// Basic usage: 访问Union中未初始化的字段 /// /// 虽然未报错,但该用法不安全 /// /// ```rust /// #[repr(C)] /// union U { /// i: i32, /// f: f32, /// } /// fn main() { /// let u = U{i: 1}; /// let i =unsafe{ /// u.f /// }; /// // 0.000000000000000000000000000000000000000000001 /// println!("{}", i); /// // 对于一个联合体来说,不能同时使用两个字段 /// // 当然也不能同时出借两个字段的可变借用 /// // unsafe{ /// // let i = &mut u.i; /// // let f = &mut u.f; /// // }; /// } /// ``` pub fn union_demo(){ unimplemented!(); }