Raw Pointers क्या हैं
unsafe के अंदर, क्योंकि वे usual safety checks skip करते हैं।In this page:
let p = &value as *const Type;
let p = &mut value as *mut Type;
unsafe {
let v = *p;
}
Creating Raw Pointers
Raw pointers as *const T या as *mut T उपयोग करके references से बनाए जा सकते हैं। खुद एक raw pointer बनाना पूरी तरह safe है -- सिर्फ इसे dereference करने के लिए unsafe चाहिए।
उदाहरण: Creating Raw Pointers
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `value`, set to `100`
let value = 100;
// Declare an immutable variable `const_ptr`, set to `&value as *const i32`
let const_ptr = &value as *const i32;
// Print "Pointer created (not yet dereferenced)" to the console (with a trailing newline)
println!("Pointer created (not yet dereferenced)");
unsafe {
// Print "Value: {}" to the console, substituting in `*const_ptr` (with a trailing newline)
println!("Value: {}", *const_ptr);
}
}
Login to try C/C++/Java/PHP code in the editor
Mutable Raw Pointers
*mut T pointer के ज़रिए pointed-to data modify करने देता है, लेकिन सभी raw pointer dereferences की तरह, ऐसा करने के लिए एक unsafe block चाहिए।
उदाहरण: Mutable Raw Pointers
// Entry point: execution of the program starts here
fn main() {
// Declare a mutable variable `value`, initialized to `5`
let mut value = 5;
// Declare an immutable variable `mut_ptr`, set to `&mut value as *mut i32`
let mut_ptr = &mut value as *mut i32;
unsafe {
*mut_ptr += 1;
}
// Print "value = {}" to the console, substituting in `value` (with a trailing newline)
println!("value = {}", value);
}
Login to try C/C++/Java/PHP code in the editor
Raw Pointers Skip Borrow Checking
References के विपरीत, आपके पास उसी data के एक साथ कई raw pointers (यहां तक कि mutable वाले भी) हो सकते हैं, क्योंकि borrow checker raw pointers track नहीं करता -- यही बिल्कुल वजह है कि इन्हें dereference करना unsafe है।
उदाहरण: Raw Pointers Skip Borrow Checking
// Entry point: execution of the program starts here
fn main() {
// Declare a mutable variable `number`, initialized to `10`
let mut number = 10;
// Declare an immutable variable `p1`, set to `&mut number as *mut i32`
let p1 = &mut number as *mut i32;
// Declare an immutable variable `p2`, set to `&mut number as *mut i32`
let p2 = &mut number as *mut i32;
unsafe {
*p1 += 1;
*p2 += 1;
}
// Print "number = {}" to the console, substituting in `number` (with a trailing newline)
println!("number = {}", number);
}
Login to try C/C++/Java/PHP code in the editor
Why Raw Pointers Exist
Raw pointers foreign function interfaces (C code call करना) जैसे low-level tasks के लिए और internally safe abstractions बनाने के लिए ज़रूरी हैं, जहां implementer manually उन safety invariants की गारंटी देता है जिन्हें compiler जांच नहीं सकता।
उदाहरण: Why Raw Pointers Exist
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `array`, set to `[1, 2, 3, 4]`
let array = [1, 2, 3, 4];
// Declare an immutable variable `ptr`, set to `array.as_ptr()`
let ptr = array.as_ptr();
unsafe {
// Print "First element via raw pointer: {}" to the console, substituting in `*ptr` (with a trailing newline)
println!("First element via raw pointer: {}", *ptr);
// Print "Second element via offset: {}" to the console, substituting in `*ptr.offset(1)` (with a trailing newline)
println!("Second element via offset: {}", *ptr.offset(1));
}
}
Login to try C/C++/Java/PHP code in the editor
- एक
unsafeblock के बाहर एक raw pointer dereference करना, जो एक compile error है। - ऐसे data के लिए एक raw pointer बनाना जो पहले ही scope से बाहर जा चुका है, एक dangling pointer में परिणत होते हुए जिसे बनाना legal है लेकिन dereference करना unsafe है।
*const T(immutable raw pointer) को*mut T(mutable raw pointer) के साथ confuse करना -- उनकी अलग-अलग capabilities हैं।
*const Tऔर*mut Traw pointers हैं जो Rust के normal borrowing rules bypass करते हैं।- Raw pointer बनाना safe है; एक को dereference करने के लिए एक
unsafeblock चाहिए। - Raw pointers null, dangling, या unaligned हो सकते हैं -- Rust उनकी validity की गारंटी नहीं देता जैसे यह references के लिए देता है।
- Raw pointers मुख्य रूप से low-level interoperability (जैसे FFI) और custom safe abstractions बनाने के लिए उपयोग होते हैं।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: