Hone logo
Hone
Problems

Mastering Rust's use Statement: Modular Imports

Rust's module system is a cornerstone of its code organization and reusability. The use statement allows you to bring items (functions, structs, modules, etc.) from other modules into the current scope, avoiding repetitive typing and improving code readability. This challenge will test your understanding of how to effectively utilize the use statement to import and access items from different modules.

Problem Description

You are tasked with creating a Rust program that demonstrates the proper use of the use statement to import and utilize items from a separate module named my_module. my_module contains a struct Point and a function distance that calculates the distance between two Point instances. Your program should create two Point instances, calculate their distance using the distance function from my_module, and print the result. The goal is to showcase how use simplifies accessing these items without needing to fully qualify their names.

Key Requirements:

  • Create a module named my_module.rs containing:
    • A struct named Point with two fields, x and y, both of type f64.
    • A function named distance that takes two Point instances as arguments and returns the distance between them (calculated using the Euclidean distance formula: sqrt((x2 - x1)^2 + (y2 - y1)^2)).
  • Create a main file (e.g., main.rs) that:
    • Imports the Point struct and the distance function from my_module using the use statement.
    • Creates two instances of the Point struct.
    • Calls the distance function with the two Point instances.
    • Prints the calculated distance to the console.

Expected Behavior:

The program should compile and run without errors. The output should be the calculated distance between the two points, formatted as a floating-point number.

Edge Cases to Consider:

  • Ensure the distance function correctly calculates the Euclidean distance.
  • Handle potential floating-point precision issues (though this is not a primary focus of the challenge).
  • Verify that the use statement correctly imports the desired items from the module.

Examples

Example 1:

my_module.rs:
struct Point {
    x: f64,
    y: f64,
}

fn distance(p1: Point, p2: Point) -> f64 {
    let dx = p2.x - p1.x;
    let dy = p2.y - p1.y;
    (dx * dx + dy * dy).sqrt()
}

main.rs:
use my_module::Point;
use my_module::distance;

fn main() {
    let p1 = Point { x: 0.0, y: 0.0 };
    let p2 = Point { x: 3.0, y: 4.0 };

    let dist = distance(p1, p2);
    println!("Distance: {}", dist);
}

Output:

Distance: 5

Explanation: The use statements import Point and distance directly into the main scope, allowing them to be used without qualification.

Example 2:

my_module.rs:
pub struct Point {
    x: f64,
    y: f64,
}

pub fn distance(p1: Point, p2: Point) -> f64 {
    let dx = p2.x - p1.x;
    let dy = p2.y - p1.y;
    (dx * dx + dy * dy).sqrt()
}

main.rs:
use my_module::{Point, distance};

fn main() {
    let p1 = Point { x: 1.0, y: 2.0 };
    let p2 = Point { x: 4.0, y: 6.0 };

    let dist = distance(p1, p2);
    println!("Distance: {}", dist);
}

Output:

Distance: 5

Explanation: This example uses a wildcard import (my_module::{Point, distance}) to bring both Point and distance into scope. The pub keyword in my_module.rs makes these items accessible from outside the module.

Constraints

  • The code must be written in Rust.
  • The my_module must be in a separate file named my_module.rs.
  • The main file should be named main.rs.
  • The distance function must correctly calculate the Euclidean distance.
  • The program must compile and run without errors.
  • The output must be a floating-point number representing the distance.

Notes

  • Consider using the pub keyword in my_module.rs to make the Point struct and distance function publicly accessible. Without pub, they are private to the module.
  • Experiment with different use statement variations (e.g., use my_module::Point;, use my_module::*;) to understand their effects on code readability and scope.
  • Think about how the use statement contributes to modularity and code organization in Rust.
Loading editor...
rust