Hone logo
Hone
Problems

Implementing Public Visibility in Rust

Rust's module system controls visibility, allowing you to define which parts of your code are accessible from other modules. This challenge focuses on understanding and implementing pub (public) visibility to control access to structs, enums, functions, and modules. Mastering public visibility is crucial for building well-organized and maintainable Rust libraries and applications.

Problem Description

You are tasked with creating a small Rust library that defines a Circle struct with associated methods. The goal is to ensure that only the necessary components of the Circle are publicly accessible, while internal details remain private.

Specifically, you need to:

  • Define a Circle struct.
  • Make the radius field of the Circle struct publicly accessible.
  • Implement a new constructor function for Circle that is publicly accessible.
  • Implement a area method for Circle that is publicly accessible.
  • Define a separate module to contain the Circle definition and its associated items.
  • Demonstrate how to use the public components of the Circle from outside its module.

Expected Behavior:

  • Users of your library should be able to create a Circle instance by providing a radius.
  • Users should be able to access and modify the radius of a Circle instance.
  • Users should be able to calculate the area of a Circle instance using the area method.
  • Internal implementation details of the Circle (if any were made private) should not be accessible from outside its defining module.

Edge Cases:

  • Consider what happens if a negative radius is provided to the new constructor (though for this challenge, you can assume valid positive radii for simplicity).

Examples

Example 1: Creating and accessing a Circle

// In main.rs (or another external module)

mod shapes; // Assume shapes module contains Circle

use shapes::Circle;

fn main() {
    let my_circle = Circle::new(5.0);

    // Accessing the public radius
    println!("Radius: {}", my_circle.radius);

    // Modifying the public radius
    let mut mutable_circle = Circle::new(10.0);
    mutable_circle.radius = 12.5;
    println!("New Radius: {}", mutable_circle.radius);
}

Expected Output for Example 1:

Radius: 5
New Radius: 12.5

Explanation:

The Circle::new(5.0) call successfully creates a Circle instance because new is public. my_circle.radius is accessible because the radius field is marked pub. A mutable Circle is created, and its radius is successfully updated because the field is public.

Example 2: Calculating the area of a Circle

// In main.rs (or another external module)

mod shapes; // Assume shapes module contains Circle

use shapes::Circle;

fn main() {
    let my_circle = Circle::new(5.0);
    let circle_area = my_circle.area();
    println!("Area: {}", circle_area);
}

Expected Output for Example 2:

Area: 78.53981633974483

Explanation:

The my_circle.area() call successfully calculates and returns the area because the area method is public.

Constraints

  • The Circle struct and its associated items must be defined within a module named shapes.
  • The radius field must be a f64.
  • The area method should return a f64.
  • The new constructor should take a f64 and return a Circle.

Notes

  • Remember that by default, all items in Rust are private to their module. You'll need to explicitly use the pub keyword to make them accessible from outside.
  • Consider how you would make the Circle struct itself public, while potentially keeping some of its internal implementation details private if the problem were more complex.
  • The std::f64::consts::PI constant will be useful for calculating the area.
Loading editor...
rust