SoFunction
Updated on 2025-03-10

Detailed explanation of workspace in Rust

When using maven to manage code in Java projects, if you encounter large projects, it will generally be split into different modules. For example, in spring-mvc, 3 modules are usually built according to model, view, and controller, and then referenced according to certain dependencies. This concept is common in Rust, but maven is replaced with cargo, and the module becomes crate, see the example below.

1. Directory structure

.
├──
├── controller
│   ├──
│   └── src
│       └──
├── model
│   ├──
│   └── src
│       └──
└── view
    ├──
    └── src
        └──

In the root directory, similar to the parent in maven, you can declare the child "module" in it: (Note: In order to avoid confusion with the mod "module" in rust, you will still use crate to call "submodule" later)

[workspace]
members=[
    "model",
    "view",
    "controller"
]

Here is a so-called workspace, which has 3 members, that is, the crate corresponding to 3 directories.

2. Statement in sub-crata

Assume that in the above engineering structure:

  • Model does not rely on other crates
  • View depends on model
  • Controller depends on view and model

Then the files in these 3 crates can be written like this:

model/

[package]
name = "model"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at /cargo/reference/
[dependencies]
# Not relying on othercrate,This node is empty

view/

[package]
name = "view"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at /cargo/reference/
[dependencies]
# declare dependency modelmodel = {path = "../model"}

controll/

[package]
name = "controller"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at /cargo/reference/
[dependencies]
model = {path = "../model"}
view = {path = "../view"}

3. Code reference

With the previous crate dependency declarations, you can write the code, see the following example:

3.1 model/src/

#[derive(Debug)]
pub struct User{
   pub username:String,
   pub password:String
}
#[derive(Debug)]
pub struct Order{
    pub orderno:St

Suppose there are 2 structures defined in the model (i.e.: class in OOP)

3.2 view/src/

//Use the User class in the modeluse model::User;
pub fn get_login_info(name:String,pass:String)->User{
    User{
        username:name,
        password:pass
    }
}

3.3 controller/src/

use view::get_login_info;
use model::{User,Order};
fn main() {
    let mut u = get_login_info(String::from("test"), String::from("123456"));
     = String::from("abcde");
    println!("{:?}", u);
 
    let o = Order{
        orderno:String::from("20211244123")
    };
    println!("{:?}",o);
 
    let u1 = User{
        username:String::from("abcd"),
        password:String::from("*123*J")
    };
    println!("{:?}",u1);
}

Running results:

User { username: "test", password: "abcde" }
Order { orderno: "20211244123" }
User { username: "abcd", password: "*123*J" }

This is all about this article about workspace in Rust. For more related Rust workspace content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!