Write a Rust program to sort a vector of objects based on a specific property.
Write a Rust program to sort a vector of objects based on a specific property.
353
20-Jun-2023
Aryan Kumar
23-Jun-2023Sure, here is a Rust program to sort a vector of objects based on a specific property:
Rust
This program first defines a struct called
Personthat has two properties:nameandage. Thesort_by_agefunction takes a mutable reference to aVecofPersonand sorts the vector by theageproperty. Themainfunction of the program creates a vector ofPersonobjects and then calls thesort_by_agefunction with the vector as input. The result of the function call is then printed to the console.To run the program, you can save it as a file called
sort_by_age.rsand then compile it using the following command:Code snippet
Once the program is compiled, you can run it using the following command:
Code snippet
This will print the following output to the console:
Code snippet