LifenLearn
  • Home
  • Psychology
  • Finance
  • Make Money Online
  • Programming
  • Self help
  • Videos
  • Privacy Policy
  • हिंदी में

Archives

  • March 2025
  • July 2023
  • May 2023
  • February 2023
  • January 2023
  • September 2022

Categories

  • Artificial intelligence
  • Finance
  • Hindi
  • JavaScript
  • Programming
  • Psychology
  • Rust
  • Rust
  • Self help
  • Home
  • Psychology
  • Finance
  • Make Money Online
  • Programming
  • Self help
  • Videos
  • Privacy Policy
  • हिंदी में
0
0
0
LifenLearn
LifenLearn
  • Home
  • Psychology
  • Finance
  • Make Money Online
  • Programming
  • Self help
  • Videos
  • Privacy Policy
  • हिंदी में
  • Rust

An Insight into Rust 1.70.0: Introducing OnceCell and OnceLock

  • July 4, 2023
  • admin
Total
0
Shares
0
0
0

Hello, tech enthusiasts! We at LifenLearn are here to delve into the latest enhancements that the Rust 1.70.0 release has brought into the limelight. This update comes with the stabilization of two noteworthy types, OnceCell and OnceLock, aimed at one-time initialization of shared data. Let’s examine these new types, their potential use-cases, and how they can make your Rust programming more efficient.

A Closer Look at OnceCell and OnceLock

The advent of Rust 1.70.0 has paved the way for the stabilization of OnceCell and OnceLock. These two types are perfectly suited to scenarios where immediate construction of data is neither desirable nor feasible, such as non-const data in global variables.

These types are unique in that they allow initialization to happen exactly once and prevent re-initialization, making them ideal for use-cases where expensive operations need to be performed only once. This design is very much in line with Rust’s overall philosophy of explicitness and control over system resources.

Let’s consider two examples that demonstrate the use of OnceLock:

In the first code snippet:

use std::sync::OnceLock;

static WINNER: OnceLock<&str> = OnceLock::new();

fn main() {
    let winner = std::thread::scope(|s| {
        s.spawn(|| WINNER.set("Alice"));
        std::thread::yield_now(); // give them a chance...

        WINNER.get_or_init(|| "Bob")
    });

    println!("{} wins!", winner);
}

Here, a OnceLock global variable WINNER is created, which is intended to hold a static string (&str). Within a scoped thread created using std::thread::scope, a new thread is spawned that attempts to set the WINNER to “Alice”. The main thread yields, allowing the spawned thread a chance to run. If WINNER remains uninitialized, it gets set to “Bob”.

And in the second code snippet:

use std::sync::OnceLock;
use crossbeam::scope;

static WINNER: OnceLock<String> = OnceLock::new();

fn main() {
    scope(|s| {
        s.spawn(|_| {
            let _ = WINNER.set("Alice".to_string());
        });
        
        std::thread::yield_now(); // give them a chance...
        
        let winner = WINNER.get_or_init(|| "Bob".to_string());
        println!("{} wins!", winner);
    }).unwrap();
}

Here also, a OnceLock global variable WINNER is created, but this time the WINNER is intended to hold an owned string (String). The main function creates a scoped thread using crossbeam::scope, a more advanced mechanism for spawning threads when shared state is involved. This function guarantees that all threads it spawns will finish before it returns, which can be useful when handling shared mutable state.

Within this scope, a new thread is spawned that tries to set WINNER to “Alice”. Notice here that we use let _ = WINNER.set("Alice".to_string()); to handle the Result returned by the set method. The underscore _ is a placeholder for a variable whose value we don’t care about. In this case, we’re ignoring the Result value. If WINNER remains uninitialized, it gets set to “Bob” and the winner is printed out.

Conclusion

The introduction of OnceCell and OnceLock in Rust 1.70.0 is a breakthrough that promises greater efficiency and control in shared data initialization. Their uniqueness lies in their one-time initialization capability, preventing re-initialization and catering perfectly to scenarios requiring expensive operations to be performed only once. Stay tuned to Life’n’Learn for more updates from the dynamic world of programming and technology. Join us in our journey of exploring, learning, and mastering the Rust language and its powerful updates!

The addition of OnceCell and OnceLock in Rust 1.70.0 presents exciting possibilities for developers to further enhance their Rust coding prowess. At Life’n’Learn, we’re thrilled about these new features and we’re eager to see how they will be utilized by the Rust community.

Stay connected to our blog for more insights into the ever-evolving world of programming, technology updates, and of course, more Rust updates. Let’s keep coding and learning together!

Total
0
Shares
Share 0
Tweet 0
Pin it 0
Related Topics
  • OnceCell
  • OnceLock
  • Rust
  • Rust 1.70.0
admin

Previous Article
  • Programming
  • Rust

Best Practices for Using Variables in Rust: A Guide to Memory Safety

  • May 18, 2023
  • admin
View Post
Next Article
  • Programming
  • Rust

Unraveling Rust 1.70.0: Exploring Sparse Protocol, OnceCell, and More!

  • July 4, 2023
  • admin
View Post
You May Also Like
View Post
  • Rust

Embrace the Future with Rustup 1.26.0: A New Chapter in Rust Installation

  • admin
  • July 5, 2023
View Post
  • Programming
  • Rust

Unraveling Rust 1.70.0: Exploring Sparse Protocol, OnceCell, and More!

  • admin
  • July 4, 2023
View Post
  • Programming
  • Rust

Best Practices for Using Variables in Rust: A Guide to Memory Safety

  • admin
  • May 18, 2023
View Post
  • Programming
  • Rust

Beginner’s Guide to Rust 1.69.0 update: What’s New and Improved?

  • admin
  • May 17, 2023
View Post
  • Hindi
  • Rust
  • Rust

रस्ट (Rust) में एरे (Arrays) और वेक्टर (Vectors): एक अवलोकन

  • admin
  • February 10, 2023
View Post
  • Programming
  • Rust

Arrays and Vectors in Rust: An Overview

  • admin
  • February 10, 2023
View Post
  • Hindi
  • Rust
  • Rust

रस्ट (Rust) में वेरिएबल्स का परिचय

  • admin
  • February 8, 2023
View Post
  • Hindi
  • Rust
  • Rust

रस्ट (Rust) के साथ आरंभ: डायनामिकली-टाइप्ड भाषाओं से आने वाले डेवलपर्स के लिए सुझाव

  • admin
  • February 8, 2023

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Social Links
Twitter 0
Instagram 0
Pinterest 0
YouTube 5K
RSS 0

Recent Posts

  • M.Phil. vs. M.A. Clinical Psychology: Clearing the Confusion
  • M.Phil. vs. M.A. Clinical Psychology: NEP 2020’s Big Shift Explained
  • M.Phil. Clinical Psychology Is Gone in India! Discover the New RCI-Approved Path
  • Embrace the Future with Rustup 1.26.0: A New Chapter in Rust Installation
  • Unraveling Rust 1.70.0: Exploring Sparse Protocol, OnceCell, and More!

Recent Comments

No comments to show.
Pages
  • Privacy Policy
  • YouTube Channel

Subscribe

Subscribe now to our newsletter

LifenLearn
Learn for a better life

Input your search keywords and press Enter.