Comprehensive Rust(Ukrainian) 202412потрібно unsafe: fn main() { let mut s = String::from("обережно!"); let r1 = &raw mut s; let r2 = r1 as *const String; // БЕЗПЕКА: r1 та r2 були отримані з посилань і тому // гарантовано є ненульовими та покажчики. unsafe { println!("r1 є: {}", *r1); *r1 = String::from("ууухооох"); println!("r2 є: {}", *r2); } // НЕБЕЗПЕЧНО. НЕ РОБІТЬ ЦЬОГО. /* let r3: &String = unsafe { &*r1 }; drop(s); println!("r3 is: також має бути правильно вирівняний. У розділі ”НЕ БЕЗПЕЧНО” наведено приклад поширеної помилки UB: *r1 має 'static час життя, тому r3 має тип &'static String, і таким чином переживає s. Створення 1960 码力 | 396 页 | 1.08 MB | 10 月前3
Comprehensive Rust(Persian ) 202412( } let mut s = String::from ) " � � � � � � � � ! " ( ; let r1 = &mut s as *mut String ; let r2 = r1 as *const String ; / / SAFETY: r1 and r2 were obtained from references and so are guaranteed to references or / / concurrently through any other pointers . unsafe } println!("r1 � � � � � � � � : } { " , * r1 ( ; * r1 = String::from ) " � � � � " ( ; println!("r2 � � � � � � � � : } { " , * r2 ( ( ; { / / NOT SAFE. DO NOT DO THIS . / * let r3: &String = unsafe { &*r1 { ; drop(s ( ; println!("r3 is: {}", *r3 ( ; * / { � � � � � � � � � � � � � � � ) � � � � � � � � � � � � � � Android0 码力 | 393 页 | 987.97 KB | 10 月前3
基于静态分析的Rust内存安全缺陷检测研究调用unsafe函数 ▪ 调用FFI(其它语言接口) ❑ 使用条件:必须标注unsafe let mut num = 5; let r1 = &num as *const i32; unsafe { println!("r1 is: {}", *r1); } 解引用裸指针 unsafe fn risky() { let address = 0x012345usize;0 码力 | 28 页 | 1.55 MB | 1 年前3
The Rust Programming Language,2nd EditionString::from("hello"); let r1 = &mut s; let r2 = &mut s; Here’s the error: error[E0499]: cannot borrow `s` as mutable more than once at a time --> borrow_twice.rs:5:19 | 4 | let r1 = &mut s; | - first mutable mutable references, just not simultaneous ones: let mut s = String::from("hello"); { let r1 = &mut s; } // r1 goes out of scope here, so we can make a new reference with no problems. let r2 = &mut s; mutable and immutable references. This code results in an error: let mut s = String::from("hello"); let r1 = &s; // no problem let r2 = &s; // no problem let r3 = &mut s; // BIG PROBLEM Here’s the error:0 码力 | 617 页 | 1.54 MB | 1 年前3
Comprehensive Rust(English) 202412requires unsafe: fn main() { let mut s = String::from("careful!"); let r1 = &raw mut s; let r2 = r1 as *const String; // SAFETY: r1 and r2 were obtained from references and so are guaranteed to // be non-null pointers. unsafe { println!("r1 is: {}", *r1); *r1 = String::from("uhoh"); println!("r2 is: {}", *r2); } // NOT SAFE. DO NOT DO THIS. /* let r3: &String = unsafe { &*r1 }; drop(s); println!("r3 is: {}" must also be properly aligned. The ”NOT SAFE” section gives an example of a common kind of UB bug: *r1 has the 'static lifetime, so r3 has type &'static String, and thus outlives s. Creating a reference0 码力 | 382 页 | 1.00 MB | 10 月前3
Comprehensive Rust ?unsafe: fn main() { let mut s = String::from("careful!"); let r1 = &mut s as *mut String; let r2 = r1 as *const String; // SAFETY: r1 and r2 were obtained from references and so are guaranteed to // pointers. unsafe { println!("r1 is: {}", *r1); *r1 = String::from("uhoh"); println!("r2 is: {}", *r2); } // NOT SAFE. DO NOT DO THIS. /* let r3: &String = unsafe { &*r1 }; drop(s); println!("r3 is: {}" must also be properly aligned. The ”NOT SAFE” section gives an example of a common kind of UB bug: *r1 has the 'static lifetime, so r3 has type &'static String, and thus outlives s. Creating a reference0 码力 | 378 页 | 1009.46 KB | 1 年前3
Rust 程序设计语言简体中文版程序设计语言 简体中文版 # fn main() { let mut s = String::from("hello"); let r1 = &mut s; let r2 = &mut s; println!("{}, {}", r1, r2); # } 错误如下: $ cargo run Compiling ownership v0.1.0 (file:///projects/ownership) let r1 = &mut s; | ------ first mutable borrow occurs here 5 | let r2 = &mut s; | ^^^^^^ second mutable borrow occurs here 6 | 7 | println!("{}, {}", r1, r2); error 这个报错说这段代码是无效的,因为我们不能在同一时间多次将 s 作为可变变量借用。第一 个可变的借入在 r1 中,并且必须持续到在 println! 中使用它,但是在那个可变引用的创建 和它的使用之间,我们又尝试在 r2 中创建另一个可变引用,该引用借用与 r1 相同的数据。 这一限制以一种非常小心谨慎的方式允许可变性,防止同一时间对同一数据存在多个可变引 用。新 Rustacean0 码力 | 600 页 | 12.99 MB | 1 年前3
Comprehensive Rust(简体中文) 202412unsafe 方法: fn main() { let mut s = String::from("careful!"); let r1 = &mut s as *mut String; let r2 = r1 as *const String; // SAFETY: r1 and r2 were obtained from references and so are guaranteed to // pointers. unsafe { println!("r1 is: {}", *r1); *r1 = String::from("uhoh"); println!("r2 is: {}", *r2); } // NOT SAFE. DO NOT DO THIS. /* let r3: &String = unsafe { &*r1 }; drop(s); println!("r3 is: {}" 用来访问 内存。 在大多数情况下,指针还必须正确对齐。 The ”NOT SAFE” section gives an example of a common kind of UB bug: *r1 has the 'static lifetime, so r3 has type &'static String, and thus outlives s. Creating a reference0 码力 | 359 页 | 1.33 MB | 10 月前3
Rust 程序设计语言 简体中文版 1.85.0的可变引用的代码会失败: 文件名:src/main.rs let mut s = String::from("hello"); let r1 = &mut s; let r2 = &mut s; println!("{}, {}", r1, r2); 错误如下: $ cargo run Compiling ownership v0.1.0 (file:///projects/ownership) let r1 = &mut s; | ------ first mutable borrow occurs here 5 | let r2 = &mut s; | ^^^^^^ second mutable borrow occurs here 6 | 7 | println!("{}, {}", r1, r2); previous error 这个报错说这段代码是无效的,因为我们不能在同一时间多次将 s 作为可变变量借用。第一个 可变的借入在 r1 中,并且必须持续到在 println! 中使用它,但是在那个可变引用的创建和它 的使用之间,我们又尝试在 r2 中创建另一个可变引用,该引用借用与 r1 相同的数据。 这一限制以一种非常小心谨慎的方式允许可变性,防止同一时间对同一数据存在多个可变引 用。新 Rustacean0 码力 | 562 页 | 3.23 MB | 26 天前3
Comprehensive Rust(Español) 202412unsafe: fn main() { let mut s = String::from("¡cuidado!"); let r1 = &mut s as *mut String; let r2 = r1 as *const String; // SAFETY: r1 and r2 were obtained from references and so are guaranteed to // pointers. unsafe { println!("r1 es: {}", *r1); *r1 = String::from("oh, oh"); println!("r2 es: {}", *r2); } // NO ES SEGURO. NO HAGAS ESTO. /* let r3: &String = unsafe { &*r1 }; drop(s); println!("r3 is: En la sección ”INSEGURO” se muestra un ejemplo de un tipo común de error comportamiento indefinido: *r1 tiene el tiempo de vida 'static, por lo que r3 tiene el tipo &'static String 190 y, por lo tanto0 码力 | 389 页 | 1.04 MB | 10 月前3
共 17 条
- 1
- 2













