Adding firsts notebooks
This commit is contained in:
parent
27c7f3d3cc
commit
07f0d5ca61
|
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"cells": [],
|
||||
"metadata": {},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
|
|
@ -0,0 +1,157 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "7cee85dc-0711-48b9-9dd1-8d5a0efa2a68",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Hello, World!\n",
|
||||
"Now that you’ve installed Rust, it’s time to write your first Rust program. It’s traditional when learning a new language to write a little program that prints the text Hello, world! to the screen, so we’ll do the same here!\n",
|
||||
"\n",
|
||||
"### Creating a Project Directory\n",
|
||||
"\n",
|
||||
"Open a terminal and enter the following commands to make a *projects* directory and a directory for the “Hello, world!” project within the *projects* directory.\n",
|
||||
"\n",
|
||||
"```sh\n",
|
||||
"$ mkdir ~/projects\n",
|
||||
"$ cd ~/projects\n",
|
||||
"$ mkdir hello_world\n",
|
||||
"$ cd hello_world\n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"### Writing and Running a Rust Program\n",
|
||||
"\n",
|
||||
"Next, make a new source file and call it *main.rs*. Rust files always end with the *.rs* extension. If you’re using more than one word in your filename, the convention is to use an underscore to separate them. For example, use *hello_world.rs* rather than *helloworld.rs*.\n",
|
||||
"\n",
|
||||
"Now open the main.rs file you just created and enter the code."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"id": "23512a76-d996-4702-9a5d-51a85e478057",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"fn main() {\n",
|
||||
" println!(\"Hello, world!\");\n",
|
||||
"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "e9934548-d9f5-4c25-b3eb-2d77d476ea78",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"To see the output in this notebook run the code below."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"id": "8f6c527a-0df4-40fc-afbf-f69ec93c5f69",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Hello, world\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"println!(\"Hello, world\");"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "3635726e-d07d-4013-88c0-9eec55a4b5dd",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"To run in your terminal, just follow the steps below.\n",
|
||||
"\n",
|
||||
"```rust\n",
|
||||
"$ rustc main.rs\n",
|
||||
"$ ./main\n",
|
||||
"Hello, world!\n",
|
||||
"```"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "0a99d564-5228-4814-aa7b-f5b112f5f67c",
|
||||
"metadata": {
|
||||
"jp-MarkdownHeadingCollapsed": true
|
||||
},
|
||||
"source": [
|
||||
"```rust\n",
|
||||
"$ rustc main.rs\n",
|
||||
"$ ./main\n",
|
||||
"Hello, world!\n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"Regardless of your operating system, the string `Hello, world!` should print to the terminal. If `Hello, world!` did print, congratulations! You’ve officially written a Rust program. That makes you a Rust programmer—welcome!\n",
|
||||
"\n",
|
||||
"<details>\n",
|
||||
" <summary>more details</summary>\n",
|
||||
" \n",
|
||||
" ### Anatomy of a Rust Program\n",
|
||||
" Let’s review this “Hello, world!” program in detail. Here’s the first piece of the puzzle:\n",
|
||||
" \n",
|
||||
" ```rust\n",
|
||||
" fn main() {\n",
|
||||
"\n",
|
||||
" }\n",
|
||||
" ```\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"These lines define a function named main. The `main` function is special: it is always the first code that runs in every executable Rust program. Here, the first line declares a function named `main` that has no parameters and returns nothing. If there were parameters, they would go inside the parentheses `()`.\n",
|
||||
"\n",
|
||||
"The body of the main function holds the following code:\n",
|
||||
"\n",
|
||||
"```rust\n",
|
||||
" println!(\"Hello, world!\");\n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"This line does all the work in this little program: it prints text to the screen. There are four important details to notice here.\n",
|
||||
"\n",
|
||||
"First, Rust style is to indent with four spaces, not a tab.\n",
|
||||
"\n",
|
||||
"Second, `println!` calls a Rust macro. If it had 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.\n",
|
||||
"\n",
|
||||
"Third, you see the `\"Hello, world!\"` string. We pass this string as an argument to `println!`, and the string is printed to the screen.\n",
|
||||
"\n",
|
||||
"Fourth, we end the line with a semicolon (`;`), which indicates that this expression is over and the next one is ready to begin. Most lines of Rust code end with a semicolon.\n",
|
||||
"\n",
|
||||
"### Compiling and Running Are Separate Steps\n",
|
||||
"\n",
|
||||
"You’ve just run a newly created program, so let’s examine each step in the process.\n",
|
||||
"\n",
|
||||
"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:\n",
|
||||
"\n",
|
||||
"```sh\n",
|
||||
"$ rustc main.rs\n",
|
||||
"```\n",
|
||||
"</details>"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Rust",
|
||||
"language": "rust",
|
||||
"name": "rust"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": "rust",
|
||||
"file_extension": ".rs",
|
||||
"mimetype": "text/rust",
|
||||
"name": "Rust",
|
||||
"pygment_lexer": "rust",
|
||||
"version": ""
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
|
|
@ -0,0 +1,65 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "670184c1-8f72-475b-94a3-0d9f49eff4d2",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Hello, Cargo!\n",
|
||||
"\n",
|
||||
"Cargo is Rust’s build system and package manager. Most Rustaceans use this tool to manage their Rust projects because Cargo handles a lot of tasks for you, such as building your code, downloading the libraries your code depends on, and building those libraries. (We call the libraries that your code needs *dependencies*.)\n",
|
||||
"\n",
|
||||
"The simplest Rust programs, like the one we’ve written so far, don’t have any dependencies. If we had built the “Hello, world!” project with Cargo, it would only use the part of Cargo that handles building your code. As you write more complex Rust programs, you’ll add dependencies, and if you start a project using Cargo, adding dependencies will be much easier to do.\n",
|
||||
"\n",
|
||||
"Because the vast majority of Rust projects use Cargo, the rest of this book assumes that you’re using Cargo too. Cargo comes installed with Rust if you used the official installers discussed in the `“Installation”` section. If you installed Rust through some other means, check whether Cargo is installed by entering the following in your terminal:\n",
|
||||
"\n",
|
||||
"```sh\n",
|
||||
"$ cargo --version\n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"If you see a version number, you have it! If you see an error, such as `command not found`, look at the documentation for your method of installation to determine how to install Cargo separately.\n",
|
||||
"\n",
|
||||
"### Creating a Project with Cargo\n",
|
||||
"\n",
|
||||
"Let’s create a new project using Cargo and look at how it differs from our original “Hello, world!” project. Navigate back to your *projects* directory (or wherever you decided to store your code). Then, on any operating system, run the following:\n",
|
||||
"\n",
|
||||
"```sh\n",
|
||||
"$ cargo new hello_cargo\n",
|
||||
"$ cd hello_cargo\n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"The first command creates a new directory and project called *hello_cargo*. We’ve named our project *hello_cargo*, and Cargo creates its files in a directory of the same name.\n",
|
||||
"\n",
|
||||
"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.\n",
|
||||
"\n",
|
||||
"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`\n",
|
||||
"\n",
|
||||
"<details>\n",
|
||||
" <sumary>more details</sumary>\n",
|
||||
"\n",
|
||||
" ### sdaf\n",
|
||||
" Open Cargo.toml in your text editor of choice. It should look similar to the code in Listing 1-2.\n",
|
||||
"\n",
|
||||
" Filename: Cargo.toml\n",
|
||||
"</details>"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Rust",
|
||||
"language": "rust",
|
||||
"name": "rust"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": "rust",
|
||||
"file_extension": ".rs",
|
||||
"mimetype": "text/rust",
|
||||
"name": "Rust",
|
||||
"pygment_lexer": "rust",
|
||||
"version": ""
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"cells": [],
|
||||
"metadata": {},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
|
|
@ -0,0 +1,53 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "14550fee-0ae7-4870-b779-3d03b9d79b2b",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## 😊 Installing rustup on Linux or macOS\n",
|
||||
"\n",
|
||||
"If you’re using Linux or macOS, open a terminal and enter the following command:\n",
|
||||
"\n",
|
||||
"```sh\n",
|
||||
"$ curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh\n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"The command downloads a script and starts the installation of the `rustup` tool, which installs the latest stable version of Rust. You might be prompted for your password. If the install is successful, the following line will appear:\n",
|
||||
"\n",
|
||||
"```sh\n",
|
||||
"Rust is installed now. Great!\n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"## 😊 Updating and 😭 Uninstalling\n",
|
||||
"\n",
|
||||
"Once Rust is installed via `rustup`, updating to a newly released version is easy. From your shell, run the following update script:\n",
|
||||
"\n",
|
||||
"```sh\n",
|
||||
"$ rustup update\n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"```sh\n",
|
||||
"$ rustup self uninstall\n",
|
||||
"```"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Rust",
|
||||
"language": "rust",
|
||||
"name": "rust"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": "rust",
|
||||
"file_extension": ".rs",
|
||||
"mimetype": "text/rust",
|
||||
"name": "Rust",
|
||||
"pygment_lexer": "rust",
|
||||
"version": ""
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
|
|
@ -0,0 +1,157 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "7cee85dc-0711-48b9-9dd1-8d5a0efa2a68",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Hello, World!\n",
|
||||
"Now that you’ve installed Rust, it’s time to write your first Rust program. It’s traditional when learning a new language to write a little program that prints the text Hello, world! to the screen, so we’ll do the same here!\n",
|
||||
"\n",
|
||||
"### Creating a Project Directory\n",
|
||||
"\n",
|
||||
"Open a terminal and enter the following commands to make a *projects* directory and a directory for the “Hello, world!” project within the *projects* directory.\n",
|
||||
"\n",
|
||||
"```sh\n",
|
||||
"$ mkdir ~/projects\n",
|
||||
"$ cd ~/projects\n",
|
||||
"$ mkdir hello_world\n",
|
||||
"$ cd hello_world\n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"### Writing and Running a Rust Program\n",
|
||||
"\n",
|
||||
"Next, make a new source file and call it *main.rs*. Rust files always end with the *.rs* extension. If you’re using more than one word in your filename, the convention is to use an underscore to separate them. For example, use *hello_world.rs* rather than *helloworld.rs*.\n",
|
||||
"\n",
|
||||
"Now open the *main.rs* file you just created and enter the code."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"id": "23512a76-d996-4702-9a5d-51a85e478057",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"fn main() {\n",
|
||||
" println!(\"Hello, world!\");\n",
|
||||
"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "e9934548-d9f5-4c25-b3eb-2d77d476ea78",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"To see the output in this notebook run the code below."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"id": "8f6c527a-0df4-40fc-afbf-f69ec93c5f69",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Hello, world\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"println!(\"Hello, world\");"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "3635726e-d07d-4013-88c0-9eec55a4b5dd",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"To run in your terminal, just follow the steps below.\n",
|
||||
"\n",
|
||||
"```rust\n",
|
||||
"$ rustc main.rs\n",
|
||||
"$ ./main\n",
|
||||
"Hello, world!\n",
|
||||
"```"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "0a99d564-5228-4814-aa7b-f5b112f5f67c",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"```rust\n",
|
||||
"$ rustc main.rs\n",
|
||||
"$ ./main\n",
|
||||
"Hello, world!\n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"Regardless of your operating system, the string `Hello, world!` should print to the terminal. If `Hello, world!` did print, congratulations! You’ve officially written a Rust program. That makes you a Rust programmer—welcome!\n",
|
||||
"\n",
|
||||
"<details>\n",
|
||||
" <summary>more details</summary>\n",
|
||||
" \n",
|
||||
" ### Anatomy of a Rust Program\n",
|
||||
" Let’s review this “Hello, world!” program in detail. Here’s the first piece of the puzzle:\n",
|
||||
" \n",
|
||||
" ```rust\n",
|
||||
" fn main() {\n",
|
||||
"\n",
|
||||
" }\n",
|
||||
" ```\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"These lines define a function named main. The `main` function is special: it is always the first code that runs in every executable Rust program. Here, the first line declares a function named `main` that has no parameters and returns nothing. If there were parameters, they would go inside the parentheses `()`.\n",
|
||||
"\n",
|
||||
"The body of the main function holds the following code:\n",
|
||||
"\n",
|
||||
"```rust\n",
|
||||
" println!(\"Hello, world!\");\n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"This line does all the work in this little program: it prints text to the screen. There are four important details to notice here.\n",
|
||||
"\n",
|
||||
"First, Rust style is to indent with four spaces, not a tab.\n",
|
||||
"\n",
|
||||
"Second, `println!` calls a Rust macro. If it had 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.\n",
|
||||
"\n",
|
||||
"Third, you see the `\"Hello, world!\"` string. We pass this string as an argument to `println!`, and the string is printed to the screen.\n",
|
||||
"\n",
|
||||
"Fourth, we end the line with a semicolon (`;`), which indicates that this expression is over and the next one is ready to begin. Most lines of Rust code end with a semicolon.\n",
|
||||
"\n",
|
||||
"### Compiling and Running Are Separate Steps\n",
|
||||
"\n",
|
||||
"You’ve just run a newly created program, so let’s examine each step in the process.\n",
|
||||
"\n",
|
||||
"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:\n",
|
||||
"\n",
|
||||
"```sh\n",
|
||||
"$ rustc main.rs\n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"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.\n",
|
||||
"</details>"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Rust",
|
||||
"language": "rust",
|
||||
"name": "rust"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": "rust",
|
||||
"file_extension": ".rs",
|
||||
"mimetype": "text/rust",
|
||||
"name": "Rust",
|
||||
"pygment_lexer": "rust",
|
||||
"version": ""
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
|
|
@ -0,0 +1,131 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "670184c1-8f72-475b-94a3-0d9f49eff4d2",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Hello, Cargo!\n",
|
||||
"\n",
|
||||
"Cargo is Rust’s build system and package manager. Most Rustaceans use this tool to manage their Rust projects because Cargo handles a lot of tasks for you, such as building your code, downloading the libraries your code depends on, and building those libraries. (We call the libraries that your code needs *dependencies*.)\n",
|
||||
"\n",
|
||||
"The simplest Rust programs, like the one we’ve written so far, don’t have any dependencies. If we had built the “Hello, world!” project with Cargo, it would only use the part of Cargo that handles building your code. As you write more complex Rust programs, you’ll add dependencies, and if you start a project using Cargo, adding dependencies will be much easier to do.\n",
|
||||
"\n",
|
||||
"Because the vast majority of Rust projects use Cargo, the rest of this book assumes that you’re using Cargo too. Cargo comes installed with Rust if you used the official installers discussed in the `“Installation”` section. If you installed Rust through some other means, check whether Cargo is installed by entering the following in your terminal:\n",
|
||||
"\n",
|
||||
"```sh\n",
|
||||
"$ cargo --version\n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"If you see a version number, you have it! If you see an error, such as `command not found`, look at the documentation for your method of installation to determine how to install Cargo separately.\n",
|
||||
"\n",
|
||||
"### Creating a Project with Cargo\n",
|
||||
"\n",
|
||||
"Let’s create a new project using Cargo and look at how it differs from our original “Hello, world!” project. Navigate back to your *projects* directory (or wherever you decided to store your code). Then, on any operating system, run the following:\n",
|
||||
"\n",
|
||||
"```sh\n",
|
||||
"$ cargo new hello_cargo\n",
|
||||
"$ cd hello_cargo\n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"The first command creates a new directory and project called *hello_cargo*. We’ve named our project *hello_cargo*, and Cargo creates its files in a directory of the same name.\n",
|
||||
"\n",
|
||||
"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.\n",
|
||||
"\n",
|
||||
"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`\n",
|
||||
"\n",
|
||||
"<details>\n",
|
||||
" <summary>more details</summary>\n",
|
||||
" \n",
|
||||
" Open Cargo.toml in your text editor of choice. It should look similar to the code in Listing 1-2.\n",
|
||||
"\n",
|
||||
" Filename: *Cargo.toml*\n",
|
||||
"\n",
|
||||
" ```sh\n",
|
||||
" [package]\n",
|
||||
" name = \"hello_cargo\"\n",
|
||||
" version = \"0.1.0\"\n",
|
||||
" edition = \"2021\"\n",
|
||||
"\n",
|
||||
" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html\n",
|
||||
"\n",
|
||||
" [dependencies]\n",
|
||||
" ```\n",
|
||||
" This file is in the *TOML (Tom’s Obvious, Minimal Language)* format, which is Cargo’s configuration format.\n",
|
||||
"\n",
|
||||
" The first line, `[package]`, is a section heading that indicates that the following statements are configuring a \n",
|
||||
" package. As we add more information to this file, we’ll add other sections.\n",
|
||||
"\n",
|
||||
" The next three lines set the configuration information Cargo needs to compile your program: the name, the version, and the edition of Rust to use. We’ll talk about the `edition` key in `Appendix E`.\n",
|
||||
"\n",
|
||||
" 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.\n",
|
||||
"\n",
|
||||
" Now open *src/main.rs* and take a look:\n",
|
||||
"\n",
|
||||
" Filename: *src/main.rs*\n",
|
||||
"\n",
|
||||
" ```rust\n",
|
||||
" fn main() {\n",
|
||||
" println!(\"Hello, world!\");\n",
|
||||
" }\n",
|
||||
" ```\n",
|
||||
"\n",
|
||||
" ## Building and Running a Cargo Project\n",
|
||||
" Now let’s look at what’s different when we build and run the “Hello, world!” program with Cargo! From your *hello_cargo* directory, build your project by entering the following command:\n",
|
||||
"\n",
|
||||
" ```sh\n",
|
||||
" $ cargo build\n",
|
||||
" Compiling hello_cargo v0.1.0 (file:///projects/hello_cargo)\n",
|
||||
" Finished dev [unoptimized + debuginfo] target(s) in 2.85 secs\n",
|
||||
" ```\n",
|
||||
"\n",
|
||||
"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 resultant executable all in one command:\n",
|
||||
"\n",
|
||||
"\n",
|
||||
" ```rust\n",
|
||||
" $ cargo run\n",
|
||||
" Finished dev [unoptimized + debuginfo] target(s) in 0.0 secs\n",
|
||||
" Running `target/debug/hello_cargo`\n",
|
||||
" Hello, world!\n",
|
||||
" ```\n",
|
||||
"\n",
|
||||
"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:\n",
|
||||
"\n",
|
||||
"```sh\n",
|
||||
" $ cargo check\n",
|
||||
" Checking hello_cargo v0.1.0 (file:///projects/hello_cargo)\n",
|
||||
" Finished dev [unoptimized + debuginfo] target(s) in 0.32 secs\n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"## Building for Release\n",
|
||||
"With simple projects, Cargo doesn’t provide a lot of value over just using `rustc`, but it will prove its worth as your programs become more intricate. Once programs grow to multiple files or need a dependency, it’s much easier to let Cargo coordinate the build.\n",
|
||||
"\n",
|
||||
"Even though the `hello_cargo` project is simple, it now uses much of the real tooling you’ll use in the rest of your Rust career. 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:\n",
|
||||
"\n",
|
||||
"```sh\n",
|
||||
"$ git clone example.org/someproject\n",
|
||||
"$ cd someproject\n",
|
||||
"$ cargo build\n",
|
||||
"```\n",
|
||||
"</details>"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Rust",
|
||||
"language": "rust",
|
||||
"name": "rust"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": "rust",
|
||||
"file_extension": ".rs",
|
||||
"mimetype": "text/rust",
|
||||
"name": "Rust",
|
||||
"pygment_lexer": "rust",
|
||||
"version": ""
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"id": "7b085c4c-3a18-4212-8101-10fe680b17ca",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"oi\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"()"
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"println!(\"oi\")"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Rust",
|
||||
"language": "rust",
|
||||
"name": "rust"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": "rust",
|
||||
"file_extension": ".rs",
|
||||
"mimetype": "text/rust",
|
||||
"name": "Rust",
|
||||
"pygment_lexer": "rust",
|
||||
"version": ""
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"cells": [],
|
||||
"metadata": {},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
|
|
@ -0,0 +1,121 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "742d84cb-79f0-449f-89b7-56d05d074701",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Programming a Guessing Game\n",
|
||||
"\n",
|
||||
"We’ll implement a classic beginner programming problem: a guessing game. Here’s how it works: the program will generate a random integer between 1 and 100. It will then prompt the player to enter a guess. After a guess is entered, the program will indicate whether the guess is too low or too high. If the guess is correct, the game will print a congratulatory message and exit.\n",
|
||||
"\n",
|
||||
"### Setting Up a New Project\n",
|
||||
"\n",
|
||||
"To set up a new *project*, go to the projects directory that you created in Chapter 1 and make a new project using Cargo, like so:\n",
|
||||
"\n",
|
||||
"```sh\n",
|
||||
"$ cargo new guessing_game\n",
|
||||
"$ cd guessing_game\n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"The first command, *cargo new*, takes the name of the project (*guessing_game*) as the first argument. The second command changes to the new project’s directory.\n",
|
||||
"\n",
|
||||
"Look at the generated *Cargo.toml* file:\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"```toml\n",
|
||||
"[package]\n",
|
||||
"name = \"guessing_game\"\n",
|
||||
"version = \"0.1.0\"\n",
|
||||
"edition = \"2021\"\n",
|
||||
"\n",
|
||||
"# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html\n",
|
||||
"\n",
|
||||
"[dependencies]\n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"As you saw in Chapter 1, `cargo new` generates a “Hello, world!” program for you. Check out the *src/main.rs* file:\n",
|
||||
"\n",
|
||||
"### Processing a Guess\n",
|
||||
"\n",
|
||||
"The first part of the guessing game program will ask for user input, process that input, and check that the input is in the expected form. To start, we’ll allow the player to input a guess. Enter the code in Listing 2-1 into *src/main.rs*\n",
|
||||
"\n",
|
||||
"```rust\n",
|
||||
"use std::io;\n",
|
||||
"\n",
|
||||
"fn main() {\n",
|
||||
" println!(\"Guess the number!\");\n",
|
||||
"\n",
|
||||
" println!(\"Please input your guess.\");\n",
|
||||
"\n",
|
||||
" let mut guess = String::new();\n",
|
||||
"\n",
|
||||
" io::stdin()\n",
|
||||
" .read_line(&mut guess)\n",
|
||||
" .expect(\"Failed to read line\");\n",
|
||||
"\n",
|
||||
" println!(\"You guessed: {guess}\");\n",
|
||||
"}\n",
|
||||
"```"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "5222247e-25ef-4e00-8672-0ba63390f3ad",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"use std::io;\n",
|
||||
"\n",
|
||||
"println!(\"Guess the number!\");\n",
|
||||
"println!(\"Please input your guess.\");\n",
|
||||
"\n",
|
||||
"let mut guess = String::new();\n",
|
||||
"\n",
|
||||
"io::stdin()\n",
|
||||
" .read_line(&mut guess)\n",
|
||||
" .expect(\"Failed to read line\");\n",
|
||||
"\n",
|
||||
"println!(\"You guessed: {guess}\");"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "a7f6d707-2700-4c25-8900-83df60230370",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"This code contains a lot of information, so let’s go over it line by line. To obtain user input and then print the result as output, we need to bring the `io` input/output library into scope. The `io` library comes from the standard library, known as `std`:\n",
|
||||
"\n",
|
||||
"```rust\n",
|
||||
"use std::io;\n",
|
||||
"```"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "c649c49a-2be9-49f4-a30b-cc84f8ba0ebc",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Rust",
|
||||
"language": "rust",
|
||||
"name": "rust"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": "rust",
|
||||
"file_extension": ".rs",
|
||||
"mimetype": "text/rust",
|
||||
"name": "Rust",
|
||||
"pygment_lexer": "rust",
|
||||
"version": ""
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
Loading…
Reference in New Issue