Could not find `anyencode` in the crate root

Hello, i am writing blockchain and i faced an issue of importing functions from module.

unresolved import `super::anyencode`
could not find `anyencode` in the crate root

I tried "mod anyencode" but it didnt help.
I am newbie at rust, so i wanna ask you about help please.
I have such file structure
src/anyencode.rs (module i want to import)
src/element.rs (file where i try to import)
src/main.rs

Here is element.rs

use super::anyencode::{decode_data, encode_data};
use std::collections::HashMap;

#[derive(Debug)]
pub struct Element {
    data: Vec<u8>,
    neighbors: HashMap<String, String>,
    hash: String,
    extra_data: Vec<u8>,
    memo: String,
}

impl Element {
    pub fn new<T: std::fmt::Debug, U: std::fmt::Debug>(
        data: T,
        neighbors: HashMap<String, String>,
        hash: String,
        extra_data: U,
        memo: String,
    ) -> Self {
        let data = encode_data(&data).unwrap();
        let extra_data = encode_data(&extra_data).unwrap();
        Element {
            data,
            neighbors,
            hash,
            extra_data,
            memo,
        }
    }

    pub fn get_data<T: std::default::Default + std::clone::Clone>(&self) -> T {
        decode_data::<T>(&self.data.clone()).unwrap_or_default()
    }

    pub fn get_extra_data<U: std::default::Default + std::clone::Clone>(&self) -> U {
        decode_data::<U>(&self.extra_data.clone()).unwrap_or_default()
    }
}

Thank you in advance.

The best thing to do here is create src/lib.rs and move all of the non-executable stuff to it. A minimal example might be:

pub mod anyencode;
pub mod element;

In your main.rs, these are referenced by crate name, not super or crate aliases. E.g., if your crate name is my_crate, then it's just use my_crate::anyencode;

The ideal main.rs is just a thin wrapper around your library and might include some things like CLI argument parsing and some kind of error handler. See Testing - Command Line Applications in Rust (rust-cli.github.io) for the basics.

4 Likes

thank you

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.