Hone logo
Hone
Problems

Structuring Data with Tuple Structs in Rust

Tuple structs in Rust provide a concise way to define simple data structures that group together a fixed number of fields. This challenge will guide you through creating and using tuple structs to represent data in a structured manner, a fundamental concept in Rust programming. Understanding tuple structs is crucial for organizing related data and building more complex data types.

Problem Description

You are tasked with creating a tuple struct named Point that represents a 2D point in a Cartesian coordinate system. The Point struct should have two fields: x and y, both of type i32. You need to implement a function create_point that takes two i32 arguments, x_coordinate and y_coordinate, and returns a Point instance initialized with these values. Finally, you should write a main function that calls create_point with specific coordinates and prints the x and y values of the resulting Point instance.

Key Requirements:

  • Define a tuple struct named Point with two fields: x and y, both of type i32.
  • Implement a function create_point that takes two i32 arguments and returns a Point instance.
  • The main function should create a Point instance using create_point and print its x and y values.

Expected Behavior:

The program should compile and run without errors. The output should display the x and y coordinates of the Point instance created in the main function.

Edge Cases to Consider:

  • While the problem doesn't explicitly require handling negative coordinates, ensure your code functions correctly with both positive and negative integer values for x and y.

Examples

Example 1:

Input: x_coordinate = 5, y_coordinate = 10
Output: x: 5, y: 10
Explanation: The `create_point` function is called with 5 and 10. A `Point` instance is created with x = 5 and y = 10. The `main` function then prints these values.

Example 2:

Input: x_coordinate = -2, y_coordinate = 3
Output: x: -2, y: 3
Explanation: The `create_point` function is called with -2 and 3. A `Point` instance is created with x = -2 and y = 3. The `main` function then prints these values.

Constraints

  • The x and y coordinates will be represented as i32 integers.
  • The program should compile and run successfully.
  • The create_point function must return a Point instance.

Notes

  • Tuple structs are similar to regular structs but have a fixed number of fields defined by the tuple.
  • You can access the fields of a tuple struct using the dot (.) operator, just like with regular structs.
  • Consider the simplicity and conciseness of tuple structs when designing your solution. They are ideal for representing simple data aggregates.
Loading editor...
rust