Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document how to use external modules? #61

Open
rauschma opened this issue Feb 12, 2023 · 3 comments
Open

Document how to use external modules? #61

rauschma opened this issue Feb 12, 2023 · 3 comments
Labels
enhancement New feature or request

Comments

@rauschma
Copy link

Is it possible to use external modules? If yes, then it may make sense to document how.

Example:

% cat ./main.rs
mod other;

fn main() {
    other::hello();
}

% cat ./other.rs 
pub fn hello() {
    println!("How are you?");
}

For comparison, cargo-play handles it like this. I can run the aforementioned two modules via cargo play *.rs

@fornwall
Copy link
Owner

Is it possible to use external modules?

It's not currently possible, but looks interesting. Experimenting with it.

@fornwall
Copy link
Owner

Related, it's possible to "include" another rust file, with something like:

include!(concat!(env!("RUST_SCRIPT_BASE_PATH"), "/utils.rs"));

call_function_defined_in_utils_rs();

It's not the same thing as modules and a bit verbose, but it can sometimes be useful.

@rfdonnelly
Copy link

It is also possible to use local crates. Something like:

my_crate/src/lib.rs

pub fn hello() {
    println!("How are you?");
}

my_rust_script

#!/usr/bin/env rust-script

//! ```cargo
//! [dependencies]
//! my_crate = { path = "my_crate" }
//! ```

fn main() {
    my_crate::hello()
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants