编写Rust程序

1
2
3
4
# 创建Cargo项目
cargo new hello
# 编辑main.rs文件
vi /hello/src/main.rs

拉取所需Rust版本的镜像

DockerHub项目地址:https://hub.docker.com/_/rust/tags

1
2
# 拉取所需版本的Rust镜像
podman pull rust:1.74.1

使用容器构建Rust程序

1
2
3
4
5
6
7
8
9
10
11
# 进入Rust项目目录
cd hello/
# 进入Podman容器
podman run -it --privileged=true --rm -v .:/app rust:1.74.1 /bin/bash
# 构建项目
cd app/
cargo build
# 退出容器
exit
# 在宿主机运行二进制程序
./target/debug/hello # 此处仅为示例,具体文件名应根据项目进行填写

在使用容器构建Rust项目时应注意容器中的libc和其他运行库的版本和宿主机环境是否兼容,否则编译出的二进制文件可能无法正常运行