Question: Part 3 : Coding problem ( 2 0 p t s ) Feel free to use the following extra page to answer part 3 .

Part 3 : Coding problem (20pts)
Feel free to use the following extra page to answer part 3.
This coding section consists of two task.
a.) Define a struct type called Triplet that represents a "triplet" of fields (i.e., 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
"swap_first_and_last" 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 (i.e., 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 3 values ("hello",-1,'a')
Return value : a new triplet containing 3 values ',-1, "hello")
Note that, in this case, the first and third fields of the triplet swapped types.
Part 3 : Coding problem ( 2 0 p t s ) Feel free

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!