{ "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", "
\n", " more details\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", "
" ] } ], "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 }