Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: Add example of cargo add #3331

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "guessing_game"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
$ cargo add [email protected]
Updating crates.io index
Adding rand v0.8.3 to dependencies.
Features:
+ alloc
+ getrandom
+ libc
+ rand_chacha
+ rand_hc
+ std
+ std_rng
- log
- nightly
- packed_simd
- serde
- serde1
- simd_support
- small_rng
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use std::io;

fn main() {
println!("Guess the number!");

println!("Please input your guess.");

let mut guess = String::new();

io::stdin()
.read_line(&mut guess)
.expect("Failed to read line");

println!("You guessed: {guess}");
}
7 changes: 7 additions & 0 deletions src/ch02-00-guessing-game-tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,13 @@ that will still compile with the code in this chapter. Any version `0.9.0` or
greater is not guaranteed to have the same API as what the following examples
use.

Instead of manually adding the dependency by editing the *Cargo.toml* file, we
can ask Cargo to add the dependency for us, using the `cargo add` command:

```console
{{#include ../listings/ch02-guessing-game-tutorial/no-listing-06-cargo-add/output.txt}}
```

Now, without changing any of the code, let’s build the project, as shown in
Listing 2-2.

Expand Down