0
0
Rustprogramming~10 mins

Module visibility in Rust - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to make the function accessible outside the module.

Rust
mod greetings {
    pub fn [1]() {
        println!("Hello!");
    }
}

fn main() {
    greetings::hello();
}
Drag options to blanks, or click blank then click option'
Awelcome
Bgreet
Csay_hello
Dhello
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different function name than the one called in main.
Not making the function public with 'pub'.
2fill in blank
medium

Complete the code to make the struct field accessible outside the module.

Rust
mod person {
    pub struct Person {
        pub [1]: String,
        age: u32,
    }
}

fn main() {
    let p = person::Person { name: String::from("Alice"), age: 30 };
    println!("Name: {}", p.name);
}
Drag options to blanks, or click blank then click option'
Aperson
Bname
Cinfo
Dage
Attempts:
3 left
💡 Hint
Common Mistakes
Making the wrong field public.
Trying to access a private field from outside the module.
3fill in blank
hard

Fix the error by completing the code to allow access to the function from outside the module.

Rust
mod utils {
    [1]() {
        println!("Utility function");
    }
}

fn main() {
    utils::helper();
}
Drag options to blanks, or click blank then click option'
Apub fn helper
Bpub helper
Chelper
Dfn helper
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to add pub keyword.
Adding pub but missing fn keyword.
4fill in blank
hard

Fill both blanks to make the module and its function accessible from outside.

Rust
mod [1] {
    pub fn [2]() {
        println!("Inside module");
    }
}

fn main() {
    [1]::[2]();
}
Drag options to blanks, or click blank then click option'
Anetwork
Butils
Cconnect
Drun
Attempts:
3 left
💡 Hint
Common Mistakes
Mismatch between module/function names and their usage in main.
Not making the function public.
5fill in blank
hard

Fill all three blanks to create a public struct with a public method accessible outside the module.

Rust
mod shapes {
    pub struct [1] {
        pub size: u32,
    }

    impl [1] {
        pub fn [2](&self) -> u32 {
            self.size
        }
    }
}

fn main() {
    let s = shapes::[1] { size: 10 };
    println!("Size: {}", s.[2]());
}
Drag options to blanks, or click blank then click option'
ASquare
BCircle
Csize
Dget_size
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for struct or method than in main.
Not making struct or method public.