RSSAmplifier

Blog

BabylonCandle

The blog of Sanjiv Sahayam

blog.ssanj.netRSS feed ↗10 posts

Latest posts

Pi Hole Blocking Fortnite Gift Card Redemption

TL;DR: Turn off the Pi-Hole or use Mobile data to redeem Fortnite gift cards. I’ve been trying to redeem some Fornite gift cards for my son recently and I’ve found it to be so frustrating. I initially tried to use the QR on the gift card using my Android phone but that just did nothing. I later tried to enter the code on the redeem website manually. Same result - nothing. No error. No message.…

How to call trait functions in Rust

Traits define a group of functions that work towards some shared behaviour. There are a few ways to invoke trait functions in Rust and we’ll have a look at those ways in the sections below. Traits have two types of functions: Methods - These are functions that take in a self parameter Associated Functions - These are functions that do not take a self parameter How to invoke Functions of a Trait…

Working With Rust Result

Trying to learning how to use the Rust Result type can be daunting. In this “Working with Rust Result” series of short posts, I hope to make that more approachable. This series is for beginners who are finding it difficult to understand what a Result is and how to use it. The series is split into fourteen parts as listed below. What is a Result? - ( Construction ) Extracting Values - ( Pattern…

Working With Rust Result - Combining Results Some More - Part 9

There are even more ways to combine Results s! and and is similar to and_then except a default Result is returned on an Ok instance: pub fn and < U > ( self , res : Result < U , E > ) -> Result < U , E > { match self { Ok (_) => res , Err (e) => Err (e) , } } Notice that the value inside the Ok instance is never used: Ok (_) => res , Since res is eager it will get evaluated as soon as and is…

Working With Rust Result - Combining Results with the Question Mark Operator - Part 8

Lets try to perform a calculation on multiple numbers parsed from strings: let numbers_1 : Result < u32 , ParseIntError > = add_numbers( "10" , "20" , "30" ) ; // Ok(60) let numbers_2 = add_numbers( "ten" , "20" , "30" ) ; // Err(ParseIntError { kind: InvalidDigit }) let numbers_3 = add_numbers( "10" , "twenty" , "30" ) ; // Err(ParseIntError { kind: InvalidDigit }) let numbers_4 = add_numbers(…

Working With Rust Result - Chaining with Map - Part 7

What if we only wanted to parse two numbers and add them together and not return any errors? We can already solve this with and_then as before: parse_number( "10" ) . and_then( | ten | { // We have successfully parsed "10" into 10. parse_number( "20" ) . and_then( | twenty | { // We have successfully parsed "20" into 20. Ok (ten + twenty) } ) } ) We could also just map over the last function that…

Working With Rust Result - Combining Results - Part 6

Result gets interesting when you need to combine multiples to give you one final Result . and_then Sometimes when you have multiple functions that return Result s, you want to know if all of them succeeded or any of them failed. and_then can help you there. and_then is defined as: pub fn and_then < U , F : FnOnce (T) -> Result < U , E >> ( self , op : F) -> Result < U , E > { match self { Ok (t)…

Working With Rust Result - Tranforming Values - Part 5

When using functions like map_or_else , we extracted the success and error values out of a Result , thereby losing our Result wrapper. What if you could run a function on the value within a Result and stay within the Result wrappers? Then you wouldn’t have to do all this pesky unwrapping until you needed the value. map The map function lets you transform a value within a Result : pub fn map < U ,…

Working With Rust Result - Making Things Nicer with Fallbacks - Part 4

We can unwrap a Result in a nicer way, if we provide a default value of type T or call a function that returns a value of type T when given a type E : // pseudocode // Given: Result<T, E> Ok (T) -> T Err (_) -> T // Return value of `T` Err (E) -> T // Use a function of type `E` -> `T` unwrap_or unwrap_or is defined as: pub fn unwrap_or( self , default : T) -> T { match self { Ok (t) => t , Err (_)…

Working With Rust Result - Extracting Values That Can Panic - Part 3

Note: Do not use these functions if you have better/nicer alternatives. unwrap What if we want to fail (panic) our program if the supplied age is not twenty five? We can work forcibly by using unwrap . unwrap is defined as: pub fn unwrap( self ) -> T where E : fmt:: Debug , The above definition returns the success type T under all conditions. But how can it return a success value T if it’s an Err…