学习Rust的第二天:Cargo

We dive into Cargo, the powerful and convenient build system and package manager for Rust.
基于Steve Klabnik的《The Rust Programming Language》一书,我们深入了解Cargo,这是Rust强大而方便的构建系统和包管理器。

6e70ec5165d195dde7cb65971a609ea0.jpeg

 

Cargo is a robust and efficient build system and package manager for Rust, designed to help manage project dependencies and ensure consistent builds across various environments.
Cargo是Rust的一个强大而高效的构建系统和包管理器,旨在帮助管理项目依赖关系,并确保在各种环境中构建一致。

To create a new program with cargo :
要使用cargo创建新程序,请执行以下操作:

$ cargo new main$ cd main$ ls

Directory Structure : 目录结构:

 

main
└── target└── debug└── release
└── src└── main.rs
├── Cargo.toml

In the created directory structure:
在创建的目录结构中:

  • main is the root directory of the program.
    main 是程序的根目录。
  • Cargo.toml contains metadata about the project and its dependencies.
    Cargo.toml 包含关于项目及其依赖项的元数据。
  • src is a directory containing the source code of the program.
    src 是包含程序源代码的目录。
  • main.rs is the main entry point to the program. This is where the main function is defined which is the entry point of the program.
    main.rs 是程序的主要入口点。这是定义main函数的地方,它是程序的入口点。
  • target is the directory that stores all the builds or outputs.
    target 是存储所有构建或输出的目录。

The important files : 重要文件:

The Cargo.toml file looks like this:
Cargo.toml 文件看起来像这样:

[package]
name = "main"
version = "0.1.0"
authors = ["Your Name <you@example.com>"]
edition = "2018"
[dependencies]

The [package] section defines the package name, version, authors, and edition. The [dependencies] section is where you list your project dependencies.
[package] 部分定义了软件包名称、版本、作者和版本。 [dependencies] 部分是您列出项目依赖项的地方。

The main.rs file looks like this:
main.rs 文件看起来像这样:

fn main() {println!("Hello, world!");
}

This is a simple Rust program that prints “Hello, world!” to the console.
这是一个简单的Rust程序,打印“Hello,world!”到控制台

To build and run your program, navigate to the project directory (in this case, main) and run cargo run.
要构建并运行程序,请导航到项目目录(在本例中为 main )并运行 cargo run 。

$ cd main
$ cargo run

This command builds your project and runs the resulting binary.
此命令生成项目并运行生成的二进制文件。

And that’s the basic structure and workflow of a Cargo program in Rust!
这就是Rust中Cargo程序的基本结构和工作流!

Some other useful cargo commands :
其他一些有用的cargo命令:

  • cargo build : used to build for testing (Output directory : target/debug)
    cargo build :用于构建测试(输出目录: target/debug )
  • cargo build --release : used to build for release (Output directory : target/debug)
    cargo build --release :用于构建发布(输出目录: target/debug )
  • cargo check : to check if the program would compile without actually compiling it.
    cargo check :检查程序是否可以编译,而无需实际编译。

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/610743.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

DevExpress WinForms中文教程 - 如何通过UI测试自动化增强应用可靠性?(二)

DevExpress WinForm拥有180组件和UI库&#xff0c;能为Windows Forms平台创建具有影响力的业务解决方案。DevExpress WinForm能完美构建流畅、美观且易于使用的应用程序&#xff0c;无论是Office风格的界面&#xff0c;还是分析处理大批量的业务数据&#xff0c;它都能轻松胜任…

选择正确的Go Module Path

最近我在查看项目代码时&#xff0c;注意到有人在go.mod文件中将module path写为com.example.foo了。根据这个写法&#xff0c;相信屏幕前的读者也可以推断出这位开发人员可能是从Java阵营转到Go的。实际开发中可能有很多开发者会使用类似的内容作为module path&#xff0c;但这…

STM32 SPI CRC polynominal = 7 含义

STM32 SPI CRC polynominal 7 含义 1. CRC校验1.1 多项式公式1.2 多项式简记式1.3 数据宽度1.4 初始值与结果异或值1.5 输入值反转与输出值反转 2. 常见的CRC算法3. CRC校验算法前置知识3.1 异或3.2 模2加法3.3 模2减法3.4 模2除法 4. CRC原理5. 计算示例6. STM32 SPI 中的CRC…

C++常用技巧与算法总结(简洁)

前言警告&#xff1a; 1、注意数据的边界&#xff0c;数组不能越界&#xff01; 2、 时间复杂度 3、开long long 4、注意输出的四舍五入 printf("%d %.0f\n",k,sum*1.0/k);//double类型输出会自动四舍五入&#xff1b; 5、让Dev C支持C11 先在dev的【工具】里找…

SpringMVC中拦截所有请求后,<mvc:resources/>不起作用

报错显示 出现这种问题的前提是&#xff0c;用DispatcherServlet将所有的请求拦截&#xff0c;然后导致静态资源无法访问。 拦截代码如下&#xff0c;配置卸载web.xml文件中。 <servlet><servlet-name>dispatcherServlet</servlet-name><servlet-class&…

Apache SeaTunnel 社区 3 月月报

各位热爱 SeaTunnel 的小伙伴们&#xff0c;SeaTunnel 社区 3 月月报来啦&#xff01;这里将记录 SeaTunnel 社区每个月的重要更新&#xff0c;并评选出月度之星&#xff0c;欢迎关注。 SeaTunnel 月度 Merge Stars 感谢以下小伙伴 3 月为 Apache SeaTunnel 做的精彩贡献&…

libVLC 提取视频帧使用QWidget渲染

在前面的文章中&#xff0c;我们使用libvlc_media_player_set_hwnd设置了视频的显示的窗口。 libvlc_media_player_set_hwnd(vlc_mediaPlayer, (void *)ui.widgetShow->winId()); 如果我们想要提取每一帧数据&#xff0c;将数据渲染到QWidget上&#xff0c;该如何操作呢&a…

使用Android Studio制作一个蓝牙软件 ---(一)

一、创建项目&#xff08;项目名称---BluetoothActivity&#xff09; 二、创建HomeActivity页面 1.点击一个文件夹 --- 鼠标右击 --- 新建 --- Acyivity --- Empty Views Activity 三、创建styles.xml 点击 values文件夹 --- 鼠标右击 --- 新建 --- Values Resource File sty…

IJKPLAYER源码分析-iOS端显示

1 简介 1.1 EAGL(Embedded Apple Graphics Library) 与Android系统使用EGL连接OpenGL ES与原生窗口进行surface输出类似&#xff0c;iOS则用EAGL将CAEAGLLayer作为OpenGL ES输出目标。 与 Android EGL 不同的是&#xff0c;iOS EAGL 不会让应用直接向 BackendFrameBuffer 和 F…

AJAX 入门到实战 第1天 2024 笔记

1.1-AJAX入门与axios使用 1.2-认识URL 1.3-查询参数 1.4-案例_地区查询 <script src"https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script><script>/*获取地区列表: http://hmajax.itheima.net/api/area查询参数:pname: 省份或直辖市…

2024年认证杯A题保暖纤维的保暖能力完整思路代码论文讲解与分析

保暖纤维的保暖能力建模 摘要 本文针对保暖纤维的保暖能力建模问题进行了深入研究。首先,文章指出现有的一些保暖性能指标,如热导率、热阻值、CLO值等,存在一些局限性,无法全面反映保暖材料的实际保暖性能。因此,本文提出了建立一个更加完善的保暖能力评价指标体系,包括热传导…

文献学习-33-一个用于生成手术视频摘要的python库

VideoSum: A Python Library for Surgical Video Summarization Authors: Luis C. Garcia-Peraza-Herrera, Sebastien Ourselin, and Tom Vercauteren Source: https://arxiv.org/pdf/2303.10173.pdf 这篇文章主要关注的是如何通过视频摘要来简化和可视化手术视频&#xff0c…