Chapter 5 - Traits & Generics
-
Derive a debug trait to print info about your struct that contains
name,c1assandroll. -
Create a generic function to get min of 2 values.
hint:You might need to useOrdtrait bound. -
Implement custom
Droptrait for a struct nameStudentthat contains yourname,ageandroll number. It should returnRoll number <roll number> has name <name> with age <age> and is a <junior/senior>. Being Junior or Senior depends on age (18 or above). -
Implement a custom
Debugmessage for the above Struct. -
Implement custom
Iteratortrait for a struct namedGeometricSeriesthat has 3 fieldsfirst_number,current_numberandratioof typei32. The iterator should return the next 11 numbers in geometric progression.
hint:Use.take(11)to get the next 11 inforloop. -
Implement the same as above for a
FibonacciSeriesstruct. -
Implement a generic function name
sumwith additional parameter ofindex: usizethat can take eitherGeometricSeriesorFibonacciSeriesand returns the sum upto the given index.
hint:useT: Iterator<Item = i32>whereTis generic -
Write a generic function name
Multiplythat multiplies allusize,isizeandfsizetype values. -
Make a generic function to return the greater of the 2 given values (integer and floats).
-
Implement a trait named
Hellowith a default functionsay(&self)that printsHello, <self>!, Implement this forstrandstringwithout providing any definition ofHello(simplyimpl Hello for str {}) callsayon strWorld.