-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Commit from GitHub Actions (Generate Post)
- Loading branch information
1 parent
1dc7cc0
commit 60ad770
Showing
3 changed files
with
155 additions
and
1 deletion.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
...erstanding-the-power-of-rust’s-standard-library:-exploring-the-'cell'-module.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
--- | ||
title: "Understanding the Power of Rust’s Standard Library: Exploring the 'cell' Module" | ||
date: 2023-09-18T01:26:16.640Z | ||
tags: ["rust","programming","advanced concepts"] | ||
authors: ["gpt-4"] | ||
--- | ||
|
||
|
||
## Introduction | ||
|
||
One of the core principles in Rust's design is ensuring safety through static, compile-time checks, particularly with mutable references. However, real-world problems sometimes require mutable state in certain contexts, and although Rust allows mutable borrow via `&mut T`, there are restrictions that come with it, such as the rule that we can’t have a mutable reference while we have an outstanding shared reference. To help with that, Rust's standard library offers special types for providing interior mutability, encapsulated within the `std::cell` module. This post explores patterns to leverage Rust's `cell` module, specifically the `RefCell` and `Cell` types. | ||
|
||
## Cell & RefCell | ||
|
||
The `std::cell::Cell` and `std::cell::RefCell` types represent single ownership over the data they contain, giving ability to mutate the value they contain, even when the type is not marked as mutable. | ||
|
||
```rust | ||
use std::cell::Cell; | ||
|
||
let x = Cell::new(1); | ||
let y = &x; | ||
let z = &x; | ||
x.set(2); | ||
println!("{}", x.get()); // prints: 2 | ||
``` | ||
|
||
In this code snippet, `x` can be updated even though `y` and `z` are referring to it. | ||
|
||
`RefCell`, on the other hand, employs run-time borrowing rules check as opposed to compile-time. It makes use of `std::cell::Ref` and `std::cell::RefMut` to represent shared and exclusive references and provides the `borrow()` and `borrow_mut()` methods to acquire them. | ||
|
||
```rust | ||
use std::cell::RefCell; | ||
|
||
let x = RefCell::new(vec![1, 2, 3, 4]); | ||
{ | ||
println!("{:?}", *x.borrow()); // prints: [1, 2, 3, 4] | ||
|
||
let mut y = x.borrow_mut(); | ||
y.push(5); | ||
} | ||
println!("{:?}", *x.borrow()); // prints: [1, 2, 3, 4, 5] | ||
``` | ||
|
||
In this snippet, `x` is first borrowed immutably, then mutably within a different scope, allowing list modification. | ||
|
||
## When to use Cell & RefCell | ||
|
||
`std::cell::Cell` is used when we want to alter some internal state that isn't visible from outside the type. | ||
|
||
`std::cell::RefCell` is used when we want the ability to have multiple references to an object and the flexibility to mutate it. | ||
|
||
In essence, both allow Rust’s borrowing rules to be subverted for complex situations, while still upholding the guarantees that make Rust safe. | ||
|
||
## Conclusion | ||
|
||
By exploring the `std::cell` module, specifically the `Cell` and `RefCell` types, we've seen how Rust provides mechanisms to manage mutable state while still adhering to principles of safety. In future posts, we'll delve deeper into techniques that employ these dynamic ways of handling mutability. |
86 changes: 86 additions & 0 deletions
86
...ions/understanding-the-power-of-rust’s-standard-library:-exploring-the-'cell'-module.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
{ | ||
"model": "gpt-4", | ||
"prompt": [ | ||
{ | ||
"role": "user", | ||
"content": "You are the author of a personal technical blog about software engineering and computer science subjects." | ||
}, | ||
{ | ||
"role": "user", | ||
"content": "New topics for blog posts should be based off of the technical skills, experience, and interests presented in the following resume:\n+++\ntitle = \"Résumé\"\ndescription = \"Derek Cuevas' Résumé\"\ndate = \"2023-06-11\"\nor = \"Derek Cuevas\"\n+++\n\n# Derek Cuevas\n\n[GitHub](https://github.com/derekcuevas) (contact email in bio) \n\n---\n\n## Summary\n\nSenior Software Engineer with 8 years of professional experience, specializing in systems software engineering and low-latency, real-time product development.\n\nExperienced in leading teams and delivering Emmy award-winning projects. Passionate about functional programming and building high performance real-time systems. Skilled in a variety of programming languages and technologies with expertise in [Rust](https://www.rust-lang.org/) and [TypeScript](https://www.typescriptlang.org/).\n\n---\n\n## Programming Languages\n\n- **Proficient:** Rust, TypeScript (& JavaScript), Clojure, Elm\n- **Experienced:** Swift, Elixir, Objective-C, SQL\n\n---\n\n## Technologies, Skills, and Interests\n\n- **Rust:** Tokio, Futures, Hyper, Actix, Warp\n- **Platforms and Services:** AWS, Docker, PostgreSQL, Redis, Apache Pulsar, RabbitMQ, Prometheus, GitLab CI/CD, Node.js, Deno, HTML & CSS\n- **Tooling:** Git, Vim, Bash\n- **Frameworks:** React, React Native, Redux, RxJs, SwiftUI, Phoenix\n- **Protocols:** HTTP, GraphQL, WebSockets, ØMQ, Pub/Sub, JSON, MessagePack, Protobuf, FlatBuffers\n- **Paradigms:** Functional Programming, Real-time Systems\n\n---\n\n## Professional Experience\n\n### Senior Software Engineer II (Lead), Second Spectrum (acquired by Genius Sports)\n(*January 2021 - Current*)\n\n[https://geniussports.com/](https://geniussports.com/)\n\n- Technical lead for the \"Real-time Augmentation Engine\" project's system team, developing low-latency and real-time augmented reality experiences for live TV broadcasters of sporting events.\n- Led the development and integration of core systems using `Rust`, `Tokio`, `MPSC channels`, `FFmpeg`, and `Pulsar`.\n- Oversaw operational management and monitoring systems, release management, simulation systems, and data ingestion/export pipelines.\n- Optimized the overall pipeline for 60fps real-time rendering over high definition broadcast feeds.\n- Key contributor to projects recognized with multiple Emmy awards, including the prestigious [George Wensel Technical Achievement Award](https://geniussports.com/customer-stories/cbs-and-genius-win-sports-emmy-for-romovision/) and Amazon's [Outstanding Interactive Experience](https://www.aboutamazon.com/news/entertainment/thursday-night-football-on-prime-video-wins-sports-emmy-award) at the `Sports Emmys`.\n\n### Senior Software Engineer I (Lead), Second Spectrum\n(*January 2019 - January 2021*)\n\n[https://www.secondspectrum.com/](https://www.secondspectrum.com/)\n\n- Led a team in designing and developing the company's first `GraphQL` service serving as a unified gateway for various microservices. Built upon the `Apollo` server framework leveraging `Node.js`, `TypeScript`, `PostgreSQL`, and `Redis` for caching and pub/sub.\n- Developed a dashboard built with `Three.js` for visualizing 3D player tracking datasets and highlighting data discrepancies.\n- Created a prototype `iOS` application for video highlights using `Swift`, `SwiftUI`, and the `Combine` framework.\n- Mentored interns and fellow engineers, fostering collaboration and knowledge sharing.\n\n### Software Engineer, Second Spectrum\n(*August 2016 - January 2019*)\n\n[https://www.secondspectrum.com/](https://www.secondspectrum.com/)\n\n- Developed the operational and quality assurance management application for the company's optical player tracking system. Stack included `Elixir` (with the `Phoenix` framework), `React`, and heavy use of `WebSockets` to sync and display corrections to operators in near real-time.\n- Improved operator efficiency by 100% through the implementation of an automated task selection system.\n- Led the development of the LA Clipper's live streaming web and mobile application \"Clippers CourtVision\". Stack included `Elm` for the web front-end and `React Native` for the mobile app.\n\n### Software Engineer Intern, MindGraph Medical\n(*September 2015 - January 2016*)\n\n- Designed, developed, and deployed the MindGraph Medical website using `Node.js`, `jQuery`, and `Bootstrap`.\n\n---\n\n## Education\n\n**B.S. Computer Science, San Diego State University, San Diego**\n\n(*Class of 2016*)\n\n- [Advanced Certificate in Web and Mobile Applications Development](https://cs.sdsu.edu/about/)\n\n---\n\n## Personal Projects\n\n- [FYGT](https://github.com/DerekCuevas/derekcuevas.github.io): An experimental blog built with `Hugo`, `Deno`, and `ChatGPT`.\n- [Advent of Code 2022](https://github.com/DerekCuevas/advent-of-code-2022): Solutions to Advent of Code programming problems using functional approaches in `Clojure`.\n- [Interview Cake Algorithms Problem Set](https://github.com/DerekCuevas/interview-cake-clj): Solutions to the interview cake programming problems also by exploring functional approaches in `Clojure`.\n- [Friend List](https://github.com/DerekCuevas/friend-list): A non-trivial programming challenge exploring asynchronous side effects with different programming patterns.\n- [Redux Meta Reducer](https://github.com/DerekCuevas/redux-meta-reducer): A `Redux` higher-order reducer for simplifying the state of fetched data.\n- [Avowal](https://github.com/DerekCuevas/avowal): An asynchronous form validation library for `JavaScript`.\n" | ||
}, | ||
{ | ||
"role": "user", | ||
"content": "Your previous 60 posts include: \"Advanced Runtime Polymorphism in Rust with `dyn Trait`\", \"---\", \"Optimizing High Performance Computing with Affinity in Rust\", \"Exploring Advanced Server-Side Event Streaming with Warp in Rust\", \"Deep Dive Into Postgres and JSONB: Advanced Performance Techniques\", \"Exploring Elm Architecture: Building a SPA with Elm\", \"Exploring Functional Programming with Rust: An Introduction to Frunk\", \"Streamlining High-Performance Concurrency with Rust's Mio Crate\", \"Implementing A Concurrency Safe CRDT Set in Rust\", \"Leveraging Actix for High-Performance Web Services in Rust\", \"Mastering Dependency Injection in Rust: A Comprehensive Guide\", \"Demystifying the World of NATS Messaging System\", \"Mastering Lenses in Swift: A Comprehensive Guide\", \"Deep Dive into Pulsar's Schema Registry\", \"Mastering the Decorator Pattern in TypeScript: Best Practices and Examples\", \"Exploring the Benefits of Declarative Programming in React\", \"Understanding CQRS and Event Sourcing: A Comprehensive Guide\", \"Mastering Apache Flink for Stream Processing\", \"Demystifying Rust's Ownership Model: A Comprehensive Guide\", \"Mastering Java Generics: A Comprehensive Guide\", \"Mastering Reactive Programming with RxJS: Advanced Techniques\", \"A Comprehensive Guide to Threading in Rust\", \"Mastering the Observer Pattern with Rust\", \"Exploring the Power of Rust's Ownership Model: A Comprehensive Guide\", \"The Power of Rust's Clippy: A Comprehensive Guide\", \"Exploring the Disadvantages of Promises in JavaScript\", \"Exploring the Fundamentals of Tail-Call Optimization in JavaScript\", \"Mastering Distributed Systems with Apache BookKeeper\", \"Exploring the Power of Rust's Type System: A Comprehensive Guide\", \"Mastering Asynchronous Event-Driven Architecture with Apache Kafka\", \"Advanced Techniques for Distributed System Design using Apache Cassandra\", \"Exploring the Underlying Architecture of TypeScript\", \"Functional Reactive Programming with Elm: A Comprehensive Guide\", \"Exploring the Power of Clojure's Persistent Data Structures\", \"The Power of Rust's Multithreading Library: rayon\", \"The Power of the `async` Keyword in Rust: A Comprehensive Guide\", \"Mastering Cache Invalidation Strategies: A Comprehensive Guide\", \"Mastering Rust's Serialization and Deserialization: A Comprehensive Guide\", \"Mastering AWS S3 for High Availability: A Comprehensive Guide\", \"Exploring the Fundamentals of Reactive Programming\", \"Mastering Rust's Pinning: An Advanced Guide\", \"Understanding Higher Order Functions in TypeScript\", \"Mastering Async/Await in TypeScript: A Comprehensive Guide\", \"Building High Availability Systems with Apache ZooKeeper\", \"Mastering Rust's Unsafe Keyword: A Comprehensive Guide\", \"The Power of Immutable Data Structures in JavaScript\", \"Building a Highly Available System with Apache ZooKeeper\", \"Mastering Persistent Connections in Java\", \"Mastering Real-Time Communication with WebSockets in Elixir\", \"Understanding the Reactive Stream Pattern\", \"Understanding the Power of Rust's Macros\", \"Mastering Rust's Futures: An Advanced Guide\", \"Advanced Techniques for Working with Rust's Borrow Checker\", \"Understanding the Difference Between Block and Inline Elements in HTML\", \"Mastering Kafka Connect: A Comprehensive Guide\", \"Effective Error Handling in Swift: Best Practices and Patterns\", \"The Power of Kotlin's Coroutines: A Comprehensive Guide\", \"Mastering Asynchronous Programming in Rust: A Comprehensive Guide\", \"Building Custom Collectors in Java: A Comprehensive Guide\", \"The Art of Domain-Driven Design\"" | ||
}, | ||
{ | ||
"role": "user", | ||
"content": "Come up with an advanced topic for an expert level reader that is about a particular single feature, fact, pattern, paradigm, convention, theory, framework, library, or best practice." | ||
}, | ||
{ | ||
"role": "user", | ||
"content": "Write a post about the chosen topic following the instructions given below:" | ||
}, | ||
{ | ||
"role": "user", | ||
"content": "Your writing style is academic, informative, and focused on detailed technical information." | ||
}, | ||
{ | ||
"role": "user", | ||
"content": "The topic should not be related to a previous post." | ||
}, | ||
{ | ||
"role": "user", | ||
"content": "Use computer code snippets (in the relevant programming language) in order to illustrate the information presented." | ||
}, | ||
{ | ||
"role": "user", | ||
"content": "Format the body of the post in the markdown markup language." | ||
}, | ||
{ | ||
"role": "user", | ||
"content": "Come up with a title for the post and include it on the first line." | ||
}, | ||
{ | ||
"role": "user", | ||
"content": "Include one to three tags for the post on the second line formatted as a comma separated list, for example: \"Tags: <tag1>, <tag2>\"." | ||
}, | ||
{ | ||
"role": "user", | ||
"content": "The post should be multiple sections in length." | ||
}, | ||
{ | ||
"role": "user", | ||
"content": "Do not include links to images in the post." | ||
}, | ||
{ | ||
"role": "user", | ||
"content": "Do not include contact information in the post." | ||
}, | ||
{ | ||
"role": "user", | ||
"content": "Do not add extra whitespace around lists." | ||
} | ||
], | ||
"completion": { | ||
"id": "chatcmpl-7zxIrJCIAcRHuS412GfmCJbFfuB3L", | ||
"object": "chat.completion", | ||
"created": 1695000325, | ||
"model": "gpt-4-0613", | ||
"choices": [ | ||
{ | ||
"index": 0, | ||
"message": { | ||
"role": "assistant", | ||
"content": "# Understanding the Power of Rust’s Standard Library: Exploring the 'cell' Module\n\nTags: Rust, Programming, Advanced Concepts\n\n## Introduction\n\nOne of the core principles in Rust's design is ensuring safety through static, compile-time checks, particularly with mutable references. However, real-world problems sometimes require mutable state in certain contexts, and although Rust allows mutable borrow via `&mut T`, there are restrictions that come with it, such as the rule that we can’t have a mutable reference while we have an outstanding shared reference. To help with that, Rust's standard library offers special types for providing interior mutability, encapsulated within the `std::cell` module. This post explores patterns to leverage Rust's `cell` module, specifically the `RefCell` and `Cell` types.\n\n## Cell & RefCell\n\nThe `std::cell::Cell` and `std::cell::RefCell` types represent single ownership over the data they contain, giving ability to mutate the value they contain, even when the type is not marked as mutable. \n\n```rust\nuse std::cell::Cell;\n\nlet x = Cell::new(1);\nlet y = &x;\nlet z = &x;\nx.set(2);\nprintln!(\"{}\", x.get()); // prints: 2\n```\n\nIn this code snippet, `x` can be updated even though `y` and `z` are referring to it. \n\n`RefCell`, on the other hand, employs run-time borrowing rules check as opposed to compile-time. It makes use of `std::cell::Ref` and `std::cell::RefMut` to represent shared and exclusive references and provides the `borrow()` and `borrow_mut()` methods to acquire them.\n\n```rust\nuse std::cell::RefCell;\n\nlet x = RefCell::new(vec![1, 2, 3, 4]);\n{\n println!(\"{:?}\", *x.borrow()); // prints: [1, 2, 3, 4]\n\n let mut y = x.borrow_mut();\n y.push(5);\n}\nprintln!(\"{:?}\", *x.borrow()); // prints: [1, 2, 3, 4, 5]\n```\n\nIn this snippet, `x` is first borrowed immutably, then mutably within a different scope, allowing list modification.\n\n## When to use Cell & RefCell\n\n`std::cell::Cell` is used when we want to alter some internal state that isn't visible from outside the type. \n\n`std::cell::RefCell` is used when we want the ability to have multiple references to an object and the flexibility to mutate it. \n\nIn essence, both allow Rust’s borrowing rules to be subverted for complex situations, while still upholding the guarantees that make Rust safe.\n\n## Conclusion\n\nBy exploring the `std::cell` module, specifically the `Cell` and `RefCell` types, we've seen how Rust provides mechanisms to manage mutable state while still adhering to principles of safety. In future posts, we'll delve deeper into techniques that employ these dynamic ways of handling mutability." | ||
}, | ||
"finish_reason": "stop" | ||
} | ||
], | ||
"usage": { | ||
"prompt_tokens": 2233, | ||
"completion_tokens": 637, | ||
"total_tokens": 2870 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters