Rust provides a special kind of struct known as a tuple struct. Unlike a regular struct with named fields, a tuple struct groups fields together without names, making it useful for lightweight structures where naming each field isn't necessary.
A tuple struct looks like this:
struct Point(i32, i32, i32);You can create an instance of a tuple struct just like you would with a tuple and access its fields using dot notation.
Your task is to define a tuple struct called Rectangle with two fields:
f32.f32.Then, implement a function area that calculates and returns the area of the rectangle by multiplying its width and height.
pub struct Point(pub i32, pub i32, pub i32);Here's how your code will be tested:
let rect = Rectangle(4.0, 5.0);
assert_eq!(area(&rect), 20.0);
let square = Rectangle(10.0, 10.0);
assert_eq!(area(&square), 100.0);If you get stuck, consider these hints:
rect.0 for width and rect.1 for height).pub struct Rectangle (pub f32, pub f32 );pub fn area(rect: &Rectangle) -> f32 { // 2. Implement the area function rect.0 * rect.1}pub struct Rectangle (pub f32, pub f32);pub fn area(rect: &Rectangle) -> f32 { // 2. Implement the area function rect.1 * rect.0}pub struct Rectangle(pub f32, pub f32); // 1. Finish the structpub fn area(rect: &Rectangle) -> f32 { // 2. Implement the area function rect.0 * rect.1}pub struct Rectangle (pub f32, pub f32); // 1. Finish the structpub fn area(rect: &Rectangle) -> f32 { // 2. Implement the area function rect.0 * rect.1}pub struct Rectangle(pub f32, pub f32);pub fn area(rect: &Rectangle) -> f32 { rect.0 * rect.1}pub struct Rectangle(pub f32, pub f32); // 1. Finish the structpub fn area(rect: &Rectangle) -> f32 { // 2. Implement the area function rect.0 * rect.1}pub struct Rectangle (pub f32, pub f32);pub fn area(rect: &Rectangle) -> f32 { // 2. Implement the area function rect.0 * rect.1}pub struct Rectangle (pub f32, pub f32); // 1. Finish the structpub fn area(rect: &Rectangle) -> f32 { // 2. Implement the area function return rect.0 * rect.1;}pub struct Rectangle(pub f32,pub f32);pub fn area(rect: &Rectangle) -> f32 { return rect.0* rect.1 // 2. Implement the area function}pub struct Rectangle(pub f32, pub f32); // 1. Finish the structpub fn area(rect: &Rectangle) -> f32 { // 2. Implement the area function rect.0 * rect.1}pub struct Rectangle(pub f32, pub f32);pub fn area(rect: &Rectangle) -> f32 { // 2. Implement the area function rect.0 * rect.1}pub struct Rectangle (pub f32, pub f32); // 1. Finish the structpub fn area(rect: &Rectangle) -> f32 { // 2. Implement the area function rect.0 * rect.1}pub struct Rectangle(pub f32, pub f32); // 1. Finish the structpub fn area(rect: &Rectangle) -> f32 { // 2. Implement the area function rect.0 * rect.1}pub struct Rectangle(pub f32, pub f32);pub fn area(rect: &Rectangle) -> f32 { rect.0 * rect.1}pub struct Rectangle (pub f32, pub f32);// 1. Finish the structpub fn area(rect: &Rectangle) -> f32 { rect.0 * rect.1 // 2. Implement the area function}pub struct Rectangle(pub f32, pub f32);pub fn area(rect: &Rectangle) -> f32 { // 2. Implement the area function return rect.0 * rect.1;}pub struct Rectangle(pub f32, pub f32);pub fn area(rect: &Rectangle) -> f32 { rect.0 * rect.1}pub struct Rectangle(pub f32, pub f32);pub fn area(rect: &Rectangle) -> f32 { rect.0 * rect.1}pub struct Rectangle(pub f32, pub f32); // 1. Finish the structpub fn area(rect: &Rectangle) -> f32 { // 2. Implement the area function rect.0 * rect.1}pub struct Rectangle (pub f32, pub f32); // 1. Finish the structpub fn area(rect: &Rectangle) -> f32 { // 2. Implement the area function rect.0 * rect.1}pub struct Rectangle(pub f32, pub f32); // 1. Finish the structpub fn area(rect: &Rectangle) -> f32 { // 2. Implement the area function rect.0 * rect.1}pub struct Rectangle(pub f32, pub f32); // 1. Finish the structpub fn area(rect: &Rectangle) -> f32 { // 2. Implement the area function rect.0 * rect.1}pub struct Rectangle(pub f32, pub f32);pub fn area(rect: &Rectangle) -> f32 { rect.0 * rect.1}pub struct Rectangle(pub f32, pub f32); // 1. Finish the structpub fn area(rect: &Rectangle) -> f32 { // 2. Implement the area function rect.0 * rect.1}pub struct Rectangle (pub f32, pub f32); // 1. Finish the structpub fn area(rect: &Rectangle) -> f32 { // 2. Implement the area function let result :f32 = rect.0 * rect.1; return result;}pub struct Rectangle(pub f32, pub f32); // 1. Finish the structpub fn area(rect: &Rectangle) -> f32 { // 2. Implement the area function rect.0 * rect.1}pub struct Rectangle(pub f32, pub f32); // 1. Finish the structpub fn area(rect: &Rectangle) -> f32 { // 2. Implement the area function return rect.0*rect.1}pub struct Rectangle(pub f32, pub f32);pub fn area(rect: &Rectangle) -> f32 { rect.0 * rect.1}pub struct Rectangle(pub f32, pub f32); // 1. Finish the structpub fn area(rect: &Rectangle) -> f32 { return rect.0 * rect.1}pub struct Rectangle(pub f32, pub f32);// 1. Finish the structpub fn area(rect: &Rectangle) -> f32 { // 2. Implement the area function rect.0 * rect.1}pub struct Rectangle(pub f32, pub f32);pub fn area(rect: &Rectangle) -> f32 { rect.0 * rect.1}pub struct Rectangle(pub f32, pub f32);// 1. Finish the structpub fn area(rect: &Rectangle) -> f32 { // 2. Implement the area function rect.0 * rect.1}pub struct Rectangle(pub f32, pub f32);pub fn area(rect: &Rectangle) -> f32 { rect.0 * rect.1}pub struct Rectangle(pub f32, pub f32);pub fn area(rect: &Rectangle) -> f32 { rect.0 * rect.1}pub struct Rectangle(pub f32, pub f32); // 1. Finish the structpub fn area(rect: &Rectangle) -> f32 { rect.0 * rect.1 // 2. Implement the area function}pub struct Rectangle(pub f32, pub f32);pub fn area(rect: &Rectangle) -> f32 { return rect.0 * rect.1}pub struct Rectangle(pub f32, pub f32); // 1. Finish the structpub fn area(rect: &Rectangle) -> f32 { let Rectangle(width, height) = rect; width * height // 2. Implement the area function}pub struct Rectangle(pub f32, pub f32); // 1. Finish the structpub fn area(rect: &Rectangle) -> f32 { // 2. Implement the area function rect.0 * rect.1}pub struct Rectangle( pub f32, pub f32 ); // 1. Finish the structpub fn area(rect: &Rectangle) -> f32 { let res = rect.0 * rect.1; res}pub struct Rectangle(pub f32,pub f32 );// 1. Finish the structpub fn area(rect: &Rectangle) -> f32 { // 2. Implement the area function rect.0*rect.1}pub struct Rectangle(pub f32, pub f32);pub fn area(rect: &Rectangle) -> f32 { rect.0 * rect.1}pub struct Rectangle(pub f32,pub f32); // 1. Finish the structpub fn area(rect: &Rectangle) -> f32 { rect.0 * rect.1}pub struct Rectangle(pub f32, pub f32); // 1. Finish the structpub fn area(rect: &Rectangle) -> f32 { // 2. Implement the area function rect.0 * rect.1}pub struct Rectangle(pub f32, pub f32);pub fn area(rect: &Rectangle) -> f32 { rect.0 * rect.1}pub struct Rectangle(pub f32, pub f32); // 1. Finish the structpub fn area(rect: &Rectangle) -> f32 { // 2. Implement the area function rect.0 * rect.1}pub struct Rectangle(pub f32, pub f32);pub fn area(rect: &Rectangle) -> f32 { rect.0 * rect.1}pub struct Rectangle // 1. Finish the struct( pub f32, pub f32);pub fn area(rect: &Rectangle) -> f32 { rect.0 * rect.1 // 2. Implement the area function}pub struct Rectangle(pub f32,pub f32); // 1. Finish the structpub fn area(rect: &Rectangle) -> f32 { // 2. Implement the area function rect.0 * rect.1}pub struct Rectangle(pub f32, pub f32); // 1. Finish the structpub fn area(rect: &Rectangle) -> f32 { // 2. Implement the area function rect.0 * rect.1}pub struct Rectangle(pub f32, pub f32);pub fn area(rect: &Rectangle) -> f32 { // 2. Implement the area function let &Rectangle(w, h) = rect; w * h}