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
Circlestruct. - Make the
radiusfield of theCirclestruct publicly accessible. - Implement a
newconstructor function forCirclethat is publicly accessible. - Implement a
areamethod forCirclethat is publicly accessible. - Define a separate module to contain the
Circledefinition and its associated items. - Demonstrate how to use the public components of the
Circlefrom outside its module.
Expected Behavior:
- Users of your library should be able to create a
Circleinstance by providing a radius. - Users should be able to access and modify the
radiusof aCircleinstance. - Users should be able to calculate the area of a
Circleinstance using theareamethod. - 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
newconstructor (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
Circlestruct and its associated items must be defined within a module namedshapes. - The
radiusfield must be af64. - The
areamethod should return af64. - The
newconstructor should take af64and return aCircle.
Notes
- Remember that by default, all items in Rust are private to their module. You'll need to explicitly use the
pubkeyword to make them accessible from outside. - Consider how you would make the
Circlestruct itself public, while potentially keeping some of its internal implementation details private if the problem were more complex. - The
std::f64::consts::PIconstant will be useful for calculating the area.