From, Into traits

Let’s say we have a user-defined type Point defined as follows struct Point { x: i32, y: i32, } and we want a String or &str of the form "(1,2)" to be converted into Point type. We need to implement the From trait on the Point type so that we can do something like let p: Point = Point::from(String::from("(1,2)")); or let p: Point = Point::from("(1,2)"); The rust std library documentation says only to implement From since From<T> for U implies Into<U> for T which means by implementing the From trait on the Point type, we can make use of the standard library implementation of the Into trait.
Read more

Iterators on user-defined types, Part-1

Let’s say we have a user-defined type Dictionary which is defined as follows: struct Dictionary { words: Vec<String>, } impl Dictionary { fn new() -> Self { Dictionary { words: Vec::new() } } fn insert(&mut self, val: String) { self.words.push(val); } } We would like to implement an iterator on our Dictionary type so that we can loop over all the words in the dictionary like let mut dict = Dictionary::new(); dict.
Read more

Operator overloading in Rust

Let’s say we have a user-defined type Rational defined as follows: #[derive(Debug)] struct Rational { num: i32, den: i32, } In order to support arithmetic operators like (+, -, /, *) on Rational, we would have to implement the Add, Sub, Div, Mul traits on Rational from the Rust standard library. To implement the Add trait on Rational, we need to provide an implementation for the add method from the Add trait.
Read more

How to compare user defined types in Rust

Let’s just say we have a user-defined type Person defined as follows: pub struct Person { name: String, age: i32 } We need to compare two Persons mat, mac defined as follows: let mat: Person = Person{name: String::from("Mat"), age: 30}; let mac: Person = Person{name: String::from("Mac"), age: 35}; We cannot just do mat == mac or mat != mac. We have to define what equality means for the type Person. We can do this in two ways.
Read more

Writing Man Pages

I am writing about my experience working with man pages today. I have never written/updated any man pages in the past. The man page i updated was intro(3) of the FreeBSD Library Functions Manual. Updating the man page involved going through the FreeBSD source code src/lib and figuring out the libraries that exist. I came across many libraries which i had never heard of e.g. Performance Counters Library pmc(3) 😲.And i had to find out which of those libraries have a man page so that they can be listed.
Read more