Skip to main content

What Are Rust Raw Pointers and static variables?

 What Are Rust Raw Pointers and static variables?

What Are Rust Raw Pointers and static variables?

Raw Pointers

Unsafe Rust has two new kinds called raw pointers, which are similar to references. They may be immutable or mutable and can be written as:

  • *const T (Immutable)
  • *mut T (Mutable)

Note: Asterisk "*" is not a dereferencing operator, its part of type name.

Let’s know how to make an immutable and mutable raw pointer from reference.

let mut = 5;

let r1 = &num as *const i32;

let r2 = &mut num as *i32;

Is something wrong! We didn’t include an unsafe keyword here? Well, it’s okay to create raw pointers outside unsafe but it’s must to include while dereferencing them.

Now, let’s generate a raw pointer whose validity can’t be sure.

fn main ( ) {

let address = 0x012345usize;

let r = address * const i32;


The overhead method to access memory is not optional; however, we might see or write this kind of code.

How raw pointers different from references?

  • Remain permissible to ignore the borrowing rules by having both immutable and mutable pointers or multiple mutable pointers to the same location
  • Aren’t guaranteed to point to valid memory
  • Are allowed to be null
  • Don’t implement any automatic cleanup

Static variables

  • In Rust, global variables are also called static variables
  • Static variables are similar to Constants
  • Identification style of static variables is in SCREAMING_SNAKE_CASE
  • Type footnote for static variables is a must (like constants).
  • Rust permits access to read-only static variables (immutable static variables), however doesn’t allow access to immutable static variables.
  • Static variables continuously have fixed addresses in memory.


  • Static variables must be threading safe.
  • This means that their type must implement the SyncThis links to the official Rust documentation trait.
  • For example, this links to official Rust documentation or RefCellThis links to official Rust documentation can't be used in a static variable.
  • They are not thread-safe as having multiple threads access them at once causes a data race.
  • Static variables may mention to other static variables, but only through a reference, not by value.
  • Constants can't refer to a static variable.


fn get(n: usize) -> &'static str {

static STRS: &[&str] = &[

"zero", "one", "two", "three", "four",

"five", "six", "seven", "eight", "nine",




Using statics or constants​

It may be confusing whether or not we should use a constant item or a static item., In general, Constants should be preferred over statics unless one of the following is true:

  • Big amounts of data are being stored
  • Required the single-address property of statics.
  • Inner mutability is obligatory

Mutable static variables


Popular posts from this blog

What is Internet of Things (IoT)?

 What is Internet of Things (IoT)?    The internet of Things, or IoT, refers to the billions of physical devices  round the  world that are now connected to  the web  , collecting, sharing and analyses  of knowledge  . It presents and provides the explanation of the network of physical things which is embedded with software , sensors,  and other technologies for the benefit of connecting and exchanging data with other devices and systems on the internet   . Due to the cheap processors and wireless networks, it's possible to  means  anything, from a pill to an aero plane to a self-driving car into  an area  of the IoT. Overview IoT will control the Fourth  technological revolution  . The Fourth  technological revolution  is changing the very software-defined automation allows manufacturers to link all stages of  the worth  chain, rapidly adapt to changing markets , and make highly personalized products on a mass scale. The opportunities provided by this revolution are authenticated

What is Rust Borrowing?

What is Rust Borrowing? Borrowing We call having references as function parameters borrowing. As in real world , if an individual owns something, we 'll borrow it from them . Whenever we’re done, we 've got to offer it back. Referencing  The reference is that the act of consulting somebody or something so as to urge information We would  declare reference in terms of Rust programing as if we are taking a replica of it without damaging or taking its ownership. ‘&’ symbol is employed to pass the reference Have a look that  how ‘&’ symbol is employed in code. fn main() { let s1 = String::from("hello"); let len = calculate_length(&s1); println!("The length of {} is {}.", s1, len); } fn calculate_length(s: &String) -> usize { s.len()} Mutable References The actual concept of mutable reference is same as we use mutable variable When  we  need to  modify  mutable reference is employed once   the worth we make regard to.

Kubernetes Readiness Probes

 Kubernetes Readiness Probes Description We understand that liveness probes and the way they assist keep our apps healthy by ensuring unhealthy containers are restarted automatically. Same like liveness probes, Kubernetes permit us to also define a readiness looked for our pod. The readiness probe is deployed periodically and examines whether the precise pod should receive client requests or not. Whenever a container’s readiness probe returns back success, it’s signaling that the container is ready to simply accept requests. This notion of being prepared is clearly something that’s specific to every container. Almost as liveness probe Kubernetes sends requests to container and supported the result either successful or unsuccessful response it decides container is prepared to require traffic or still preparing for that. The liveness probes not like, if a container fails the readiness check, it won’t be killed or restarted.   This is a very good practice to