Declaring and using a module without a struct

I have the following module without a struct:

pub mod img_util {
    use image::{ImageBuffer, Pixel};
    use std::{fs::File, io::BufWriter};

    fn get_color_type<P>() -> Result<png::ColorType, std::io::Error>
        P: Pixel<Subpixel = u8>,
        match <P as Pixel>::CHANNEL_COUNT {
            4 => Ok(png::ColorType::Rgba),
            3 => Ok(png::ColorType::Rgb),
            _ => Err(std::io::Error::new(
                "Incorrect color channel count / format.",

    fn get_dpi_header_data(dpi: u32) -> Vec<u8> {
        let dpm = 39.370079 * dpi as f32;
        let rounded_dpm = dpm.round() as u32;

        let mut data: Vec<u8> = Vec::new();
        data.extend_from_slice(&rounded_dpm.to_be_bytes()); // Pixels per unit in X-direction.
        data.extend_from_slice(&rounded_dpm.to_be_bytes()); // Pixels per unit in Y-direction.

        data.push(1); // Indicate that meters are used as unit.

    pub fn save_png_with_dpi<P>(
        input_img: &ImageBuffer<P, Vec<u8>>,
        output_img: File,
        dpi: u32,
    ) -> Result<(), std::io::Error>
        P: Pixel<Subpixel = u8>,
        let w = &mut BufWriter::new(output_img);
        let (width, height) = input_img.dimensions();
        let mut encoder = png::Encoder::new(w, width, height);


        let data = get_dpi_header_data(dpi);
        let mut writer = encoder.write_header()?;

        writer.write_chunk(png::chunk::pHYs, data.as_slice())?;

pub mod img_util;

use visual_center::img_util::img_util;

    // arguments

I can use the module, but I feel strange about having to declare pub mod img_util twice (once in and another in Or maybe this is the only way to declare and use a module without a struct?

>Solution :

Since you have pub mod img_util; in, the compiler will consider as a module of your crate.
Everything in is already in the img_util module.
If you state pub mod img_util { ... } in this module, this means that you provide (inline, not in another file) a submodule with the exact same name as its parent (this is a coincidence here, you could have named it differently).

You just have to remove this pub mod img_util { ... }.
Then you will use it in as

use visual_center::img_util; // one less

    // arguments

Leave a Reply