The Iterator Trait
when implementing the Iterator trait in Rust, your struct usually needs some form of state to keep track of the iteration’s progress. Often, this is achieved by including a counter or index field in your struct. https://doc.rust-lang.org/stable/book/ch13-02-iterators.html?highlight=iterator%20trait#the-iterator-trait-and-the-next-method Also, check out : https://doc.rust-lang.org/stable/book/ch19-03-advanced-traits.html#specifying-placeholder-types-in-trait-definitions-with-associated-types impl Iterator for Here’s a basic example of how to implement an iterator […]
Learn libp2p in Rust
“The Rust Implementation of the libp2p networking stack.” https://github.com/libp2p/rust-libp2p/blob/master/libp2p/src/tutorials/ping.rs Peer-to-peer networking library libp2p is an open source networking library used by the world’s most important distributed systems What is a swarm? A swarm in libp2p represents a group of peers that can communicate over a P2P network. It provides the necessary abstractions for connection management, message […]
From Trait
One of the more confusing examples for me to learn when studying Dave Macleod’s excellent “Rust in a Month of Lunches” was page 145. Check the “Rust Book” for the official description of the from trait : https://doc.rust-lang.org/std/convert/trait.From.html I modified the example to use cities in England to help me relate to it! Two cities, […]
Deserialize SurrealDB query with Rust
If you are new to SurrealDB and Rust you may be interested in how to get the data from a Response or even be trying to parse a ‘Value’. The Response struct The secret is to make a struct that will contain the keys (field names) that are present in the response. You don’t want […]
Shared state across multiple threads in Actix-web
Let’s look at “state” in Actix-web HttpRequest::app_data app_data is a method provided by actix-web, specifically for the App and HttpRequest types. Here’s how it works: Summary: Example code Alternatively, use the HttpRequest::app_data method to access data in a handler. And here is the main function The line Data::clone(&data) is effectively cloning a reference to the […]
Namespaces in Actix-Web
Let’s look at namespaces, scope, and state: https://actix.rs/docs/application Here’s a real-world use case where application state is essential in an Actix Web application. Consider a scenario where you have a web service that provides exchange rates for different currencies. You might want to maintain a cache of these rates in the application state to avoid […]
Async with tokio
Regular functions block threads Async doesn’t block threads Before we look at what tokio does, let’s consider how we’d run 3 tasks if they were ‘blocking’ Without Tokio (blocking): If you used std::thread::sleep, the execution would pause (block) for each function call until the sleep duration completes. This means that when you execute the code […]
iced.rs – example snippets (version 0.13)
Code plus screenshots – how to manipulate the layout in Rust + iced.rs Credit : https://jl710.github.io/iced-guide/index.html © Héctor Ramón (hecrj) for the iced logo. just using “row” Change “row” to “column” just using “column” Column is centered…but not the text! 🤔 Column is centered and the text! 😀 Centre the window – iced.rs – 0.13 […]