Skip to main content
TopMiniSite

Back to all posts

How to Declare A Public Static Pointer In Rust?

Published on
5 min read
How to Declare A Public Static Pointer In Rust? image

Best Rust Programming Guides to Buy in October 2025

1 The Rust Programming Language, 2nd Edition

The Rust Programming Language, 2nd Edition

BUY & SAVE
$30.13 $49.99
Save 40%
The Rust Programming Language, 2nd Edition
2 Programming Rust: Fast, Safe Systems Development

Programming Rust: Fast, Safe Systems Development

BUY & SAVE
$43.99 $79.99
Save 45%
Programming Rust: Fast, Safe Systems Development
3 Rust for Rustaceans: Idiomatic Programming for Experienced Developers

Rust for Rustaceans: Idiomatic Programming for Experienced Developers

BUY & SAVE
$29.99 $49.99
Save 40%
Rust for Rustaceans: Idiomatic Programming for Experienced Developers
4 Rust in Action

Rust in Action

BUY & SAVE
$51.42 $59.99
Save 14%
Rust in Action
5 Rust Programming: A Practical Guide to Fast, Efficient, and Safe Code with Ownership, Concurrency, and Web Programming (Rheinwerk Computing)

Rust Programming: A Practical Guide to Fast, Efficient, and Safe Code with Ownership, Concurrency, and Web Programming (Rheinwerk Computing)

BUY & SAVE
$47.04 $59.95
Save 22%
Rust Programming: A Practical Guide to Fast, Efficient, and Safe Code with Ownership, Concurrency, and Web Programming (Rheinwerk Computing)
6 Zero To Production In Rust: An introduction to backend development

Zero To Production In Rust: An introduction to backend development

BUY & SAVE
$49.99
Zero To Production In Rust: An introduction to backend development
7 The Rust Programming Language

The Rust Programming Language

BUY & SAVE
$16.92 $39.95
Save 58%
The Rust Programming Language
8 Rust Atomics and Locks: Low-Level Concurrency in Practice

Rust Atomics and Locks: Low-Level Concurrency in Practice

BUY & SAVE
$33.13 $55.99
Save 41%
Rust Atomics and Locks: Low-Level Concurrency in Practice
9 Asynchronous Programming in Rust: Learn asynchronous programming by building working examples of futures, green threads, and runtimes

Asynchronous Programming in Rust: Learn asynchronous programming by building working examples of futures, green threads, and runtimes

BUY & SAVE
$28.90 $49.99
Save 42%
Asynchronous Programming in Rust: Learn asynchronous programming by building working examples of futures, green threads, and runtimes
+
ONE MORE?

In Rust, you can declare a public static pointer using the static keyword. To declare a public static pointer, you first need to define the type of the pointer. For example, if you want to declare a public static pointer to an integer, you need to specify the type as follows:

pub static MY_POINTER: *const i32 = &42;

In this example, MY_POINTER is the name of the public static pointer, *const i32 is the type of the pointer (a raw pointer to an integer), and &42 is the address that the pointer points to.

You can then use the MY_POINTER identifier to access the value that the pointer points to. Note that when working with raw pointers in Rust, you need to be careful about memory safety and ensure that the pointer is not null before dereferencing it.

What is the scope of a public static pointer in rust?

In Rust, the scope of a public static pointer is determined by its visibility and lifetime. When a pointer is declared as public, it can be accessed from anywhere in the codebase. However, the lifetime of the data pointed to by the pointer is determined by the lifetime annotations specified in the pointer's type signature.

If the data pointed to is static (e.g. a string literal), then it will have a 'static lifetime, meaning it will exist for the entire duration of the program. If the data is dynamically allocated and managed, then its lifetime will be determined by the ownership and borrowing rules in Rust.

In general, public static pointers in Rust have global scope and can be accessed from any part of the program, but the lifetime of the data they point to must be carefully managed to avoid issues such as use-after-free errors or memory leaks.

What is a public static pointer in rust?

In Rust, there is no concept of a "public static pointer" like in other languages such as C or C++. In Rust, you can create static variables using the static keyword, but these variables are not pointers._static variables are essentially global variables that have a statically determined lifetime and can be accessed from anywhere in the program. They are initialized only once, and their value is shared between all threads.

Here is an example of declaring and initializing a static variable in Rust:

static MY_STATIC_VAR: i32 = 42;

You can access this static variable anywhere in your Rust program by using its name, MY_STATIC_VAR. But it is not a pointer, it is just a regular static variable.

How to cast a public static pointer to another type in rust?

In Rust, you can cast a public static pointer (e.g. &'static T) to another type by using the as keyword. Here is an example of how to do this:

use std::ptr;

fn main() { let x: &'static str = "Hello, world!"; let y: &'static[u8] = x.as_bytes();

// casting from &'static str to &'static\[u8\]
let z: &'static\[u8\] = unsafe {
    ptr::copy\_nonoverlapping(x.as\_ptr(), ptr::null(), x.len());
    std::slice::from\_raw\_parts(ptr::null(), x.len())
};

println!("{:?}", y); // prints \[72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33\]
println!("{:?}", z); // prints \[72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33\]

}

In the above example, we first cast the &'static str x to a &'static[u8] y using the as_bytes() method. Then, we perform an unsafe cast from &'static str to &'static[u8] by manually copying the memory contents and creating a new slice.

It is important to note that casting between types in Rust should be done with caution, as it can lead to undefined behavior if not done correctly. It is recommended to use safe and idiomatic Rust whenever possible.

How to pass a public static pointer to a closure in rust?

To pass a public static pointer to a closure in Rust, you can define the closure as a function that takes the pointer as an argument. Here's an example:

static mut MY_POINTER: *mut i32 = std::ptr::null_mut();

fn main() { unsafe { MY_POINTER = &mut 42 as *mut i32; }

let my\_closure = |ptr: \*mut i32| {
    unsafe {
        if !ptr.is\_null() {
            println!("Value at pointer: {}", \*ptr);
        }
    }
};

my\_closure(MY\_POINTER);

}

In this example, we have a static mutable pointer MY_POINTER that points to an i32. We initialize it with a value of 42 in the main function. We then define a closure my_closure that takes a pointer as an argument and prints the value at the pointer if it's not null. Finally, we call the closure with the MY_POINTER as an argument in the main function.

Remember that working with raw pointers in Rust is unsafe, so care should be taken when using them.

How to declare a public static pointer in rust?

Rust does not have pointers like C or C++, rather it uses references. To declare a public static reference in Rust, you can use the static keyword along with the mut keyword for mutable references. Here is an example:

static mut MY_POINTER: Option<&str> = None;

fn main() { unsafe { MY_POINTER = Some("Hello"); }

println!("{:?}", unsafe { MY\_POINTER });

}

In this example, we declare a mutable static pointer MY_POINTER that can point to a string slice &str. We use the unsafe keyword before assigning a value to the static pointer and before accessing it, since using static pointers in Rust is considered unsafe due to the possibility of causing undefined behavior if used incorrectly.