Closure as struct member
The following is an example with a closure and a struct that uses it: In this example, we have a Calculator struct that takes a generic type parameter T, which is expected to be a closure. The closure should take two f32 arguments and return an f32. The struct has a single field named operation […]
Modify structs – test with assert_eq! macro
This Rust code defines a simple struct called Coord and implements some methods for it. The Coord struct has two fields, x and y, representing coordinates in a two-dimensional space. Create the struct Make an implementation The Coord::new() function is a constructor method that creates a new Coord instance with initial values of x set […]
I asked ChatGPT for Rust code – does it work?
Did the code from ChatGPT compile straight away? No. Keep reading to see what I had to fix… The Cargo file is the main reason why it didn’t compile, and interestingly the screenshot here uses the Coinbase API, but an earlier question to ChatGPT replied with code using CoinGecko. Each time the issues were with […]
Saylor.org Study Sessions
If you have any interest computer science or own any money at all then learning about Bitcoin is time well spent. The best training resource that you can start with is the “Saylor Academy : Bitcoin for developers“ The video below was first broadcast 8th March 2023 and just after halfway through has a great […]
Learning Rust and improving with Codewars solutions
One of the frustrations with learning from some text books is that they set vague exercises at the end of a chapter without being explicit in the nature of the task and therefore no solution either…. For example “Write a function to convert text to ascii” Codewars Replace With Alphabet Position I’ve started to use […]
Factorial example in Rust
Whilst it’s not the best use of recursion it’s a common tutorial and interview example. As Al Sweigart says “The Recursive Factorial Algorithm Is Terrible” ~ https://inventwithpython.com/recursion/chapter2.html# Nevertheless, as a learning exercise, it’s a good way to get your feet wet. One of they key concepts to understand is “stack unwinding” – which you can […]
Traits #1
This is an initial example of Traits in Rust. There are more complicated examples than this but to get started this is a nice way to understand how to create a Trait and how to use it. Try the code: Try this code in Rust Playground : https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=6e50061eff5ac64ceed63bd55ff767e6 Note how the definition part on line […]