You've already forked comprehensive-rust
mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-06-29 03:51:35 +02:00
Publish Comprehensive Rust 🦀
This commit is contained in:
20
src/cargo/code-samples.md
Normal file
20
src/cargo/code-samples.md
Normal file
@ -0,0 +1,20 @@
|
||||
# Code Samples in This Training
|
||||
|
||||
For this training, we will mostly explore the Rust language through examples
|
||||
which can be executed through your browser. This makes the setup much easier and
|
||||
ensures a consistent experience for everyone.
|
||||
|
||||
Installing Cargo is still encouraged: it will make it easier for you to do the
|
||||
exercises. On the last day, we will do a larger exercise which shows you how to
|
||||
work with dependencies and for that you need Cargo.
|
||||
|
||||
The code blocks in this course are fully interactive:
|
||||
|
||||
```rust,editable
|
||||
fn main() {
|
||||
println!("Edit me!");
|
||||
}
|
||||
```
|
||||
|
||||
You can use <kbd>Ctrl-Enter</kbd> to execute the code when focus is in the text
|
||||
box.
|
66
src/cargo/running-locally.md
Normal file
66
src/cargo/running-locally.md
Normal file
@ -0,0 +1,66 @@
|
||||
# Running Code Locally with Cargo
|
||||
|
||||
If you want to experiment with the code on your own system, then you will need
|
||||
to first install Rust. Do this by following the [instructions in the Rust
|
||||
Book][1]. This should give you a working `rustc` and `cargo`. At the time of
|
||||
writing, the latest stable Rust release has these version numbers:
|
||||
|
||||
```shell
|
||||
% rustc --version
|
||||
rustc 1.61.0 (fe5b13d68 2022-05-18)
|
||||
% cargo --version
|
||||
cargo 1.61.0 (a028ae4 2022-04-29)
|
||||
```
|
||||
|
||||
With this is in place, then follow these steps to build a Rust binary from one
|
||||
of the examples in this training:
|
||||
|
||||
1. Click the "Copy to clipboard" button on the example you want to copy.
|
||||
|
||||
2. Use `cargo new exercise` to create a new `exercise/` directory for your code:
|
||||
|
||||
```shell
|
||||
$ cargo new exercise
|
||||
Created binary (application) `exercise` package
|
||||
```
|
||||
|
||||
3. Navigate into `exercise/` and use `cargo run` to build and run your binary:
|
||||
|
||||
```shell
|
||||
$ cd exercise
|
||||
$ cargo run
|
||||
Compiling exercise v0.1.0 (/home/mgeisler/tmp/exercise)
|
||||
Finished dev [unoptimized + debuginfo] target(s) in 0.75s
|
||||
Running `target/debug/exercise`
|
||||
Hello, world!
|
||||
```
|
||||
|
||||
4. Replace the boiler-plate code in `src/main.rs` with your own code. For
|
||||
example, using the example on the previous page, make `src/main.rs` look like
|
||||
|
||||
```rust
|
||||
fn main() {
|
||||
println!("Edit me!");
|
||||
}
|
||||
```
|
||||
|
||||
5. Use `cargo run` to build and run your updated binary:
|
||||
|
||||
```shell
|
||||
$ cargo run
|
||||
Compiling exercise v0.1.0 (/home/mgeisler/tmp/exercise)
|
||||
Finished dev [unoptimized + debuginfo] target(s) in 0.24s
|
||||
Running `target/debug/exercise`
|
||||
Edit me!
|
||||
```
|
||||
|
||||
6. Use `cargo check` to quickly check your project for errors, use `cargo build`
|
||||
to compile it without running it. You will find the output in `target/debug/`
|
||||
for a normal debug build. Use `cargo build --release` to produce an optimized
|
||||
release build in `target/release/`.
|
||||
|
||||
7. You can add dependencies for your project by editing `Cargo.toml`. When you
|
||||
run `cargo` commands, it will automatically download and compile missing
|
||||
dependencies for you.
|
||||
|
||||
[1]: https://doc.rust-lang.org/book/ch01-01-installation.html
|
17
src/cargo/rust-ecosystem.md
Normal file
17
src/cargo/rust-ecosystem.md
Normal file
@ -0,0 +1,17 @@
|
||||
# The Rust Ecosystem
|
||||
|
||||
The Rust ecosystem consists of a number of tools, of which the main ones are:
|
||||
|
||||
* `rustc`: the Rust compiler which turn `.rs` files into binaries and other
|
||||
intermediate formats.
|
||||
|
||||
* `cargo`: the Rust dependency manager and build tool. Cargo knows how to
|
||||
download dependencies hosted on <https://crates.io> and it will pass them to
|
||||
`rustc` when building your project. Cargo also comes with a built-in test
|
||||
runner which is used to execute unit tests.
|
||||
|
||||
* `rustup`: the Rust toolchain installer and updater. This tool is used to
|
||||
install and update `rustc` and `cargo` when new versions of Rust is released.
|
||||
In addition, `rustup` can also download documentation for the standard
|
||||
library. You can have multiple versions of Rust installed at once and `rustup`
|
||||
will let you switch between them as needed.
|
Reference in New Issue
Block a user