查看rustup安装包信息(可选)
$ snap info rustup
name: rustup
summary: "EXPERIMENTAL: The Rust Language installer"
publisher: Daniel Silverstone (dsilvers)
store-url: https://snapcraft.io/rustup
contact: [email protected]
license: Apache-2.0 OR MIT
description: |
**NOTA BENE:** _This is an experimental, unofficial, snap and should not be relied on._
Rustup is the Rust Language's installer and primary front-end. You probably
want this if you want to develop anything written in Rust.
Please note, this snap is experimental and may be yanked without notice
if this experiment proves to be a bad idea.
snap-id: tO4cnls38mXDXJiA0U72G7eCyrCAMweL
channels:
latest/stable: 1.24.3 2021-06-22 (1027) 4MB classic
latest/candidate: ↑
latest/beta: 1.25.0 2022-06-13 (1407) 5MB classic
latest/edge: 1.24.3 2022-07-11 (1425) 5MB classic
安装rustup
$ sudo snap install rustup
[sudo] password for user:
error: This revision of snap "rustup" was published using classic confinement and thus may perform
arbitrary system changes outside of the security sandbox that snaps are usually confined to,
which may put your system at risk.
If you understand and want to proceed repeat the command including --classic.
直接安装,有上述风险提示,需要在安装命令中增加–classic参数来安装。
$ sudo snap install rustup --classic
Download snap "core18"(2667) from channel "stable"40% 323kB/s 1m48s
rustup 1.24.3 from Daniel Silverstone (dsilvers) installed
安装rustc
安装前,执行rustc --version命令,显示找不到。
$ rustc --version
error: no override and no default toolchain set
安装stable版本
$ rustup install stable
info: syncing channel updates for'stable-x86_64-unknown-linux-gnu'
info: latest update on 2023-01-10, rust version 1.66.1 (90743e729 2023-01-10)
info: downloading component 'cargo'
info: downloading component 'clippy'
info: downloading component 'rust-docs'19.1 MiB / 19.1 MiB (100 %)8.9 MiB/s in 1s ETA: 0s
info: downloading component 'rust-std'30.0 MiB / 30.0 MiB (100 %)16.1 MiB/s in 1s ETA: 0s
info: downloading component 'rustc'67.4 MiB / 67.4 MiB (100 %)12.1 MiB/s in 5s ETA: 0s
info: downloading component 'rustfmt'
info: installing component 'cargo'
info: installing component 'clippy'
info: installing component 'rust-docs'19.1 MiB / 19.1 MiB (100 %)1.6 MiB/s in 10s ETA: 0s
info: installing component 'rust-std'30.0 MiB / 30.0 MiB (100 %)3.3 MiB/s in 7s ETA: 0s
info: installing component 'rustc'67.4 MiB / 67.4 MiB (100 %)4.4 MiB/s in 16s ETA: 0s
info: installing component 'rustfmt'
stable-x86_64-unknown-linux-gnu installed - rustc 1.66.1 (90743e729 2023-01-10)
info: default toolchain set to 'stable-x86_64-unknown-linux-gnu'
安装完成后进行确认
$ rustc --version
rustc 1.66.1 (90743e729 2023-01-10)
$ rustup default
stable-x86_64-unknown-linux-gnu (default)
cargo初体验
创建hello工程
创建名为“hello”的工程,名称可以任选。
在命令执行的目录下,创建以工程名(hello)命名的目录,其中包含rust项目默认的目录结构。
$ cargo new hello
Created binary (application)`hello` package
创建完成后,查看自动生成文件。
$ find hello/
hello/
hello/Cargo.toml
hello/src
hello/src/main.rs
查看main.rs文件内容,默认的一个hello world。
//main.rs fnmain(){println!("Hello, world!");}
编译、运行
编译+运行
在hello目录下,直接执行cargo run,后面不跟项目名称。
自动编译,并运行,显示执行结果。
$ cargo run
Compiling hello v0.1.0 (/home/user/source/rust/myrust/hello)
Finished dev [unoptimized + debuginfo] target(s)in0.67s
Running `target/debug/hello`
Hello, world!
也可以在cargo run后面跟上项目名称。
注意,再次编译耗时为0,因为检测到已生成的目标文件和可执行文件,没有再次编译。
$ cargo run hello
Finished dev [unoptimized + debuginfo] target(s)in0.00s
Running `target/debug/hello hello`
Hello, world!
cargo编译自动生成的文件
其中可执行文件为./target/debug/hello
$ find.
./target
./target/debug
./target/debug/hello
./target/debug/deps
./target/debug/deps/hello-274bceb238a71d2c
./target/debug/deps/hello-274bceb238a71d2c.d
./target/debug/.cargo-lock
./target/debug/hello.d
./target/debug/examples
./target/debug/incremental
./target/debug/incremental/hello-1mywrsfxa307p
./target/debug/incremental/hello-1mywrsfxa307p/s-ghgnder5d2-atwot6-1rt7qa06ycrsb
./target/debug/incremental/hello-1mywrsfxa307p/s-ghgnder5d2-atwot6-1rt7qa06ycrsb/1k6fmvbj4kgxmvo.o
./target/debug/incremental/hello-1mywrsfxa307p/s-ghgnder5d2-atwot6-1rt7qa06ycrsb/11gozpvmuuvnuax0.o
./target/debug/incremental/hello-1mywrsfxa307p/s-ghgnder5d2-atwot6-1rt7qa06ycrsb/5a4b3go9tpkabteb.o
./target/debug/incremental/hello-1mywrsfxa307p/s-ghgnder5d2-atwot6-1rt7qa06ycrsb/work-products.bin
./target/debug/incremental/hello-1mywrsfxa307p/s-ghgnder5d2-atwot6-1rt7qa06ycrsb/42ng4k24mutpcm07.o
./target/debug/incremental/hello-1mywrsfxa307p/s-ghgnder5d2-atwot6-1rt7qa06ycrsb/f4rryg4sswprfky.o
./target/debug/incremental/hello-1mywrsfxa307p/s-ghgnder5d2-atwot6-1rt7qa06ycrsb/dep-graph.bin
./target/debug/incremental/hello-1mywrsfxa307p/s-ghgnder5d2-atwot6-1rt7qa06ycrsb/35wjyzzyjx18spd.o
./target/debug/incremental/hello-1mywrsfxa307p/s-ghgnder5d2-atwot6-1rt7qa06ycrsb/query-cache.bin
./target/debug/incremental/hello-1mywrsfxa307p/s-ghgnder5d2-atwot6.lock
./target/debug/.fingerprint
./target/debug/.fingerprint/hello-274bceb238a71d2c
./target/debug/.fingerprint/hello-274bceb238a71d2c/invoked.timestamp
./target/debug/.fingerprint/hello-274bceb238a71d2c/bin-hello
./target/debug/.fingerprint/hello-274bceb238a71d2c/dep-bin-hello
./target/debug/.fingerprint/hello-274bceb238a71d2c/bin-hello.json
./target/debug/build
./target/.rustc_info.json
./target/CACHEDIR.TAG
./Cargo.lock
./Cargo.toml
./src
./src/main.rs
单独执行可执行文件
$ cd target/debug/
$ l
build/ deps/ examples/ hello* hello.d incremental/
$ ./hello
Hello, world!
用rustc单独编译
$ cd src/
$ rustc main.rs
$ ./main
Hello, world!
版权归原作者 抓饼先生 所有, 如有侵权,请联系我们删除。