For a list of documents, see: Rust Mobile Complex Graphics Rendering Project Development Series summary (table of Contents)

In general, C/C++ imports external symbols through #include (never mind extern and predeclarations). Rust has no header file and uses use to import external symbols, similar to Java import, but rather than reverse urls like Java package names, Rust uses a C++ namespace-like scheme that uses mods to declare modules to distinguish symbols from different modules or files. The following is an example:

#include "OpenGLES/ES2/gl.h"
#include "Framebuffer.h"

use namespace my_package_1::ClassHello;
use namespace my_package_2::ClassWorld;
Copy the code

Rust uses the AS keyword to solve the problem of symbol duplication in multiple modules.

use OpenGLES::ES2::gl::*;
use Framebuffer::*;

use mod_1::MyStruct as Mod1MyStruct;
use mod_2::MyStruct as Mod2MyStruct;
Copy the code

When C++ contains two files with the same name, a path is required to distinguish them. Multiple namespaces in the same class, all have to complete each use symbolic name to distinguish, namely Namespace1: : Namespace2: : NamespaceN: : MyClass, code more frequently. Rust has a better solution to this problem: AS renaming to avoid collisions. However, through my practice, I found it more intuitive to use the full module path and type name, similar to the C++ approach.

Importing all symbols with * is convenient but takes more compile time, so enumerating the symbols used in the current file is a better practice.

Advanced: the self keyword can import mod.rs files or rs files with the same name as the upper directory. The following is an example:

// self stands for rc.rs, because the rc module is not divided into multiple directories to organize the source code
use std::rc::{self, Rc, Weak}; 
// where self stands for mod. Rs file in rc directory
use std::io::{self, Read, Write};
Copy the code

Example of importing multiple secondary modules and a module data structure:

use hal::{
    buffer, format as f, window::Extent2D,
};
Copy the code

Example of importing multilevel modules and data structures within modules:

/ / https://github.com/amethyst/amethyst/blob/v0.9.0/examples/fly_camera/main.rs
use amethyst::{
    assets::{PrefabLoader, PrefabLoaderSystem, RonFormat},
    controls::FlyControlBundle,
    core::transform::TransformBundle,
    input::InputBundle,
    prelude::*,
    renderer::{DrawShaded, PosNormTex},
    utils::{application_root_dir, scene::BasicScenePrefab},
    Error,
};
Copy the code