Question: Part 3 : Coding problem ( 2 0 p t s ) Feel free to use the following extra page to answer part 3 .
Part : Coding problem
Feel free to use the following extra page to answer part
This coding section consists of two task.
a Define a struct type called Triplet that represents a "triplet" of fields ie three different fields where each field can have a different generic type. Each field should implement the Clone trait, however you should not require that the fields implement copy.
b Write a complete and syntactically correct generic Rust function called
"swapfirstandlast" that accepts as input a Triplet object, as defined in part a and returns a new Triplet object, except where the first field of the Triplet and the last ie third field of the Triplet are swapped with each other.
Your function should be a standalone function, and should not be part of a struct impl block. In other words, you are not implementing a method, and you should not use self. Accept an instance of your struct type as an explicit parameter.
The original Triplet passed in as input must remain unchanged by your function.
Your function should not deallocate the original Triplet that it receives as a parameter. The parameter should be borrowed, not moved.
You should require that the fields within the Triplet implement Clone, but you should not assume that they implement Copy.
Example usage :
Input : a Triplet containing values helloa
Return value : a new triplet containing values "hello"
Note that, in this case, the first and third fields of the triplet swapped types.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
