rust学习1:hello world、用cargo build project

前言

教程地址

问题1 high-level ergonomics是什么意思

High-level ergonomics and low-level control are often at odds in programming language design; Rust challenges that conflict. Through balancing powerful technical capacity and a great developer experience, Rust gives you the option to control low-level details (such as memory usage) without all the hassle traditionally associated with such control.

Cargo、Rustfmt、Rust语言服务器是干什么的

Cargo,包含的依赖管理器和构建工具,使得添加、编译和管理依赖变得轻松,并且在Rust生态系统中保持一致。
Rustfmt确保了开发人员之间的一致编码风格。
Rust语言服务器支持集成开发环境(IDE)的代码完成和内联错误消息集成。

Installing Rust on Linux, macOS, and Windows

The installation of Rust also includes a copy of the documentation locally, so you can read it offline. Run rustup doc to open the local documentation in your browser.
Any time a type or function is provided by the standard library and you’re not sure what it does or how to use it, use the application programming interface (API) documentation to find out!

Writing a program that prints Hello, world!

Creating a Project Directory

You’ll start by making a directory to store your Rust code. It doesn’t matter to Rust where your code lives, but for the exercises and projects in this book, we suggest making a projects directory in your home directory and keeping all your projects there.

C:\Users\PC>cd C:\Users\PC\Documents\RustHelloWorld

C:\Users\PC\Documents\RustHelloWorld>mkdir hello_world

C:\Users\PC\Documents\RustHelloWorld>cd hello_world

C:\Users\PC\Documents\RustHelloWorld\hello_world>cd.>main.rs

在文件中

fn main() {
    
    
    println!("Hello, world!");
}
C:\Users\PC\Documents\RustHelloWorld\hello_world>rustc main.rs

C:\Users\PC\Documents\RustHelloWorld\hello_world>.\main
Hello, world!

对代码的解释

println! calls a Rust macro. If it called a function instead, it would be entered as println (without the !). We’ll discuss Rust macros in more detail in Chapter 19. For now, you just need to know that using a ! means that you’re calling a macro instead of a normal function, and that macros don’t always follow the same rules as functions.

Compiling and Running Are Separate Steps

Before running a Rust program, you must compile it using the Rust compiler by entering the rustc command and passing it the name of your source file, like this:

$ rustc main.rs
> dir /B %= the /B option says to only show the file names =%
main.exe
main.pdb
main.rs

the executable file (main.exe on Windows, but main on all other platforms)

If you’re more familiar with a dynamic language, such as Ruby, Python, or JavaScript, you might not be used to compiling and running a program as separate steps. Rust is an ahead-of-time compiled language, meaning you can compile a program and give the executable to someone else, and they can run it even without having Rust installed. If you give someone a .rb, .py, or .js file, they need to have a Ruby, Python, or JavaScript implementation installed (respectively). But in those languages, you only need one command to compile and run your program. Everything is a trade-off in language design.

Just compiling with rustc is fine for simple programs, but as your project grows, you’ll want to manage all the options and make it easy to share your code. Next, we’ll introduce you to the Cargo tool, which will help you write real-world Rust programs.

Using cargo, Rust’s package manager and build system

Cargo是Rust的构建系统和包管理器。大多数Rustaceans使用这个工具来管理他们的Rust项目,因为Cargo为你处理了很多任务,比如构建你的代码,下载你的代码所依赖的库,以及构建那些库。(我们将代码所需的库称为依赖项。)最简单的Rust程序,就像我们目前编写的程序一样,没有任何依赖关系。因此,如果我们使用Cargo构建“Hello, world!”项目,它将只使用Cargo中处理构建代码的部分。当您编写更复杂的Rust程序时,您将添加依赖项,如果您使用Cargo启动一个项目,那么添加依赖项将会容易得多。

总结:

We can create a project using cargo new.
We can build a project using cargo build.
We can build and run a project in one step using cargo run.
We can build a project without producing a binary to check for errors using cargo check.

Instead of saving the result of the build in the same directory as our code, Cargo stores it in the target/debug directory.

Creating a Project with Cargo

C:\Users\PC\Documents\RustHelloWorld>cargo new hello_cargo
     Created binary (application) `hello_cargo` package

C:\Users\PC\Documents\RustHelloWorld>cd hello_cargo

Go into the hello_cargo directory and list the files. You’ll see that Cargo has generated two files and one directory for us: a Cargo.toml file and a src directory with a main.rs file inside.

他会自动初始化版本管理仓库
It has also initialized a new Git repository along with a .gitignore file. Git files won’t be generated if you run cargo new within an existing Git repository; you can override this behavior by using cargo new --vcs=git.

查看cargo.toml
This file is in the TOML (Tom’s Obvious, Minimal Language) format, which is Cargo’s configuration format.

[package]
name = "hello_cargo"
version = "0.1.0"
edition = "2021"

[dependencies]

The first line, [package], is a section heading that indicates that the following statements are configuring a package.

The last line, [dependencies], is the start of a section for you to list any of your project’s dependencies. In Rust, packages of code are referred to as crates. We won’t need any other crates for this project, but we will in the first project in Chapter 2, so we’ll use this dependencies section then.

cargo管理project和之前的不同
toml file、gitignore file和src文件夹

Building and Running a Cargo Project

From your hello_cargo directory, build your project by entering the following command:

C:\Users\PC\Documents\RustHelloWorld\hello_cargo>cargo build
   Compiling hello_cargo v0.1.0 (C:\Users\PC\Documents\RustHelloWorld\hello_cargo)
    Finished dev [unoptimized + debuginfo] target(s) in 0.52s

This command creates an executable file in target/debug/hello_cargo (or target\debug\hello_cargo.exe on Windows) rather than in your current directory. You can run the executable with this command:

$ ./target/debug/hello_cargo # or .\target\debug\hello_cargo.exe on Windows
Hello, world!

Running cargo build for the first time also causes Cargo to create a new file at the top level: Cargo.lock.
This file keeps track of the exact versions of dependencies in your project. This project doesn’t have dependencies, so the file is a bit sparse. You won’t ever need to change this file manually; Cargo manages its contents for you.

We just built a project with cargo build and ran it with ./target/debug/hello_cargo, but we can also use cargo run to compile the code and then run the resulting executable all in one command:

cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.0 secs
     Running `target/debug/hello_cargo`
Hello, world!

Cargo also provides a command called cargo check. This command quickly checks your code to make sure it compiles but doesn’t produce an executable:

$ cargo check
   Checking hello_cargo v0.1.0 (file:///projects/hello_cargo)
    Finished dev [unoptimized + debuginfo] target(s) in 0.32 secs

主要用来保证能编译。

Building for Release

When your project is finally ready for release, you can use cargo build --release to compile it with optimizations. This command will create an executable in target/release instead of target/debug.
If you’re benchmarking your code’s running time, be sure to run cargo build --release and benchmark with the executable in target/release.

In fact, to work on any existing projects, you can use the following commands to check out the code using Git, change to that project’s directory, and build:

$ git clone example.org/someproject
$ cd someproject
$ cargo build

猜你喜欢

转载自blog.csdn.net/weixin_42914662/article/details/125735996