File compilation structure issue

well hi I am new to rust and I am Learning it I request some help here is the context:

in the code bellow I build a simple game its working very fine but...

mod scenario1;
use std::io;

fn main() {
    let mut _character_name = String::from("link");
    const _PLAYER_DAMAGE: i32 = 10;
    let mut _link_has_ms: bool = false;
    loop {
        println!(
            "Oh look! its {_character_name} i wonder what hes doin you head and look his name displaying on the top of his head "
        );
        let mut inputt = String::new();
        println!("do you want to borrow his name or nah");
        match io::stdin().read_line(&mut inputt) {
            Ok(_) => {
                let inputt = inputt.trim();
                if inputt == "borrow" {
                    let an_empty_name = &_character_name;
                    println!(
                        "{} name BEEN borrowed..WAIT Links name?? now its your name!?",
                        an_empty_name
                    );
                    println!("Link took back his name grunting {}", _character_name);
                } else if inputt == "nah" {
                    println!("too bad stay nameless then");
                    break;
                }
                break;
            }
            Err(error) => {
                println!(" {error}");
                break;
            }
        }
    }
}

after success of creating my first rust code I decided to create a new rust file scenario2 but it seems I cant run it and can be only used as a mod I also have no idea about but now assuming it works like the io::std but I cant really use it and worse I cant run it due the cargo project only runs main could someone help me explain how mod keyword works? and why can I only run the main.rs file?

pub fn crss() {
    const _SWORD_DAMAGE: i32 = 40;
    const _DURABILITY_THERESHOLD: i32 = 400;
    const _AIR_SLASH_RANGE: u32 = 200;
    const _AIR_SLASH_DMG: i32 = 15;
}
pub fn link_owns_it() {
    let mut _deku_tree = String::from("master sword");
    const _SWORD_OWNER: &'static str = "link";
    if _SWORD_OWNER == "link" {
        let owns_it = _deku_tree;
        println!("{} link now owns the master sword", owns_it);
    }
}

Here's a good article on Rust's module system that might help you out: Clear explanation of Rust’s module system

2 Likes

the linked article by @firebits.io explains the module system very well, I can't say anything better.

main.rs is the default file name used by cargo's target auto discovery mechanism for bin crates. if you want to have multiple runnable binaries, you can either list them explicitly in the Cargo.toml manifest, or you can put the file under the src/bin directory for auto-discovery.

rust's equivalent to TU (translation unit) in C++ is called crate, which can be different types such as binary or library. when running cargo new command, it creates a package, which contains one default build target in it, each of which is a crate itself. you can have multiple targets/crates in a single package.

the terminology is explained in chapter 7 of the book:

I also explained this topic briefly in this previous answer:

wow thank you for you're answer I genuinely appreciate it I was trying to figure out why but after reading your message late its wise more clear to understand crates working thank you!