canonical introduction to rust programming
None
2022-11-03
<div>
 
 
 
 
 
 
 
 
 <nav>
 <div>
 <ol><li><a href="title-page.html">The Rust Programming Language</a></li><li><a href="foreword.html">Foreword</a></li><li><a href="ch00-00-introduction.html">Introduction</a></li><li><a href="ch01-00-getting-started.html"><strong>1.</strong> Getting Started</a></li><li><ol><li><a href="ch01-01-installation.html"><strong>1.1.</strong> Installation</a></li><li><a href="ch01-02-hello-world.html"><strong>1.2.</strong> Hello, World!</a></li><li><a href="ch01-03-hello-cargo.html"><strong>1.3.</strong> Hello, Cargo!</a></li></ol></li><li><a href="ch02-00-guessing-game-tutorial.html"><strong>2.</strong> Programming a Guessing Game</a></li><li><a href="ch03-00-common-programming-concepts.html"><strong>3.</strong> Common Programming Concepts</a></li><li><ol><li><a href="ch03-01-variables-and-mutability.html"><strong>3.1.</strong> Variables and Mutability</a></li><li><a href="ch03-02-data-types.html"><strong>3.2.</strong> Data Types</a></li><li><a href="ch03-03-how-functions-work.html"><strong>3.3.</strong> Functions</a></li><li><a href="ch03-04-comments.html"><strong>3.4.</strong> Comments</a></li><li><a href="ch03-05-control-flow.html"><strong>3.5.</strong> Control Flow</a></li></ol></li><li><a href="ch04-00-understanding-ownership.html"><strong>4.</strong> Understanding Ownership</a></li><li><ol><li><a href="ch04-01-what-is-ownership.html"><strong>4.1.</strong> What is Ownership?</a></li><li><a href="ch04-02-references-and-borrowing.html"><strong>4.2.</strong> References and Borrowing</a></li><li><a href="ch04-03-slices.html"><strong>4.3.</strong> The Slice Type</a></li></ol></li><li><a href="ch05-00-structs.html"><strong>5.</strong> Using Structs to Structure Related Data</a></li><li><ol><li><a href="ch05-01-defining-structs.html"><strong>5.1.</strong> Defining and Instantiating Structs</a></li><li><a href="ch05-02-example-structs.html"><strong>5.2.</strong> An Example Program Using Structs</a></li><li><a href="ch05-03-method-syntax.html"><strong>5.3.</strong> Method Syntax</a></li></ol></li><li><a href="ch06-00-enums.html"><strong>6.</strong> Enums and Pattern Matching</a></li><li><ol><li><a href="ch06-01-defining-an-enum.html"><strong>6.1.</strong> Defining an Enum</a></li><li><a href="ch06-02-match.html"><strong>6.2.</strong> The match Control Flow Construct</a></li><li><a href="ch06-03-if-let.html"><strong>6.3.</strong> Concise Control Flow with if let</a></li></ol></li><li><a href="ch07-00-managing-growing-projects-with-packages-crates-and-modules.html"><strong>7.</strong> Managing Growing Projects with Packages, Crates, and Modules</a></li><li><ol><li><a href="ch07-01-packages-and-crates.html"><strong>7.1.</strong> Packages and Crates</a></li><li><a href="ch07-02-defining-modules-to-control-scope-and-privacy.html"><strong>7.2.</strong> Defining Modules to Control Scope and Privacy</a></li><li><a href="ch07-03-paths-for-referring-to-an-item-in-the-module-tree.html"><strong>7.3.</strong> Paths for Referring to an Item in the Module Tree</a></li><li><a href="ch07-04-bringing-paths-into-scope-with-the-use-keyword.html"><strong>7.4.</strong> Bringing Paths Into Scope with the use Keyword</a></li><li><a href="ch07-05-separating-modules-into-different-files.html"><strong>7.5.</strong> Separating Modules into Different Files</a></li></ol></li><li><a href="ch08-00-common-collections.html"><strong>8.</strong> Common Collections</a></li><li><ol><li><a href="ch08-01-vectors.html"><strong>8.1.</strong> Storing Lists of Values with Vectors</a></li><li><a href="ch08-02-strings.html"><strong>8.2.</strong> Storing UTF-8 Encoded Text with Strings</a></li><li><a href="ch08-03-hash-maps.html"><strong>8.3.</strong> Storing Keys with Associated Values in Hash Maps</a></li></ol></li><li><a href="ch09-00-error-handling.html"><strong>9.</strong> Error Handling</a></li><li><ol><li><a href="ch09-01-unrecoverable-errors-with-panic.html"><strong>9.1.</strong> Unrecoverable Errors with panic!</a></li><li><a href="ch09-02-recoverable-errors-with-result.html"><strong>9.2.</strong> Recoverable Errors with Result</a></li><li><a href="ch09-03-to-panic-or-not-to-panic.html"><strong>9.3.</strong> To panic! or Not to panic!</a></li></ol></li><li><a href="ch10-00-generics.html"><strong>10.</strong> Generic Types, Traits, and Lifetimes</a></li><li><ol><li><a href="ch10-01-syntax.html"><strong>10.1.</strong> Generic Data Types</a></li><li><a href="ch10-02-traits.html"><strong>10.2.</strong> Traits: Defining Shared Behavior</a></li><li><a href="ch10-03-lifetime-syntax.html"><strong>10.3.</strong> Validating References with Lifetimes</a></li></ol></li><li><a href="ch11-00-testing.html"><strong>11.</strong> Writing Automated Tests</a></li><li><ol><li><a href="ch11-01-writing-tests.html"><strong>11.1.</strong> How to Write Tests</a></li><li><a href="ch11-02-running-tests.html"><strong>11.2.</strong> Controlling How Tests Are Run</a></li><li><a href="ch11-03-test-organization.html"><strong>11.3.</strong> Test Organization</a></li></ol></li><li><a href="ch12-00-an-io-project.html"><strong>12.</strong> An I/O Project: Building a Command Line Program</a></li><li><ol><li><a href="ch12-01-accepting-command-line-arguments.html"><strong>12.1.</strong> Accepting Command Line Arguments</a></li><li><a href="ch12-02-reading-a-file.html"><strong>12.2.</strong> Reading a File</a></li><li><a href="ch12-03-improving-error-handling-and-modularity.html"><strong>12.3.</strong> Refactoring to Improve Modularity and Error Handling</a></li><li><a href="ch12-04-testing-the-librarys-functionality.html"><strong>12.4.</strong> Developing the Libraryâs Functionality with Test Driven Development</a></li><li><a href="ch12-05-working-with-environment-variables.html"><strong>12.5.</strong> Working with Environment Variables</a></li><li><a href="ch12-06-writing-to-stderr-instead-of-stdout.html"><strong>12.6.</strong> Writing Error Messages to Standard Error Instead of Standard Output</a></li></ol></li><li><a href="ch13-00-functional-features.html"><strong>13.</strong> Functional Language Features: Iterators and Closures</a></li><li><ol><li><a href="ch13-01-closures.html"><strong>13.1.</strong> Closures: Anonymous Functions that Capture Their Environment</a></li><li><a href="ch13-02-iterators.html"><strong>13.2.</strong> Processing a Series of Items with Iterators</a></li><li><a href="ch13-03-improving-our-io-project.html"><strong>13.3.</strong> Improving Our I/O Project</a></li><li><a href="ch13-04-performance.html"><strong>13.4.</strong> Comparing Performance: Loops vs. Iterators</a></li></ol></li><li><a href="ch14-00-more-about-cargo.html"><strong>14.</strong> More about Cargo and Crates.io</a></li><li><ol><li><a href="ch14-01-release-profiles.html"><strong>14.1.</strong> Customizing Builds with Release Profiles</a></li><li><a href="ch14-02-publishing-to-crates-io.html"><strong>14.2.</strong> Publishing a Crate to Crates.io</a></li><li><a href="ch14-03-cargo-workspaces.html"><strong>14.3.</strong> Cargo Workspaces</a></li><li><a href="ch14-04-installing-binaries.html"><strong>14.4.</strong> Installing Binaries from Crates.io with cargo install</a></li><li><a href="ch14-05-extending-cargo.html"><strong>14.5.</strong> Extending Cargo with Custom Commands</a></li></ol></li><li><a href="ch15-00-smart-pointers.html"><strong>15.</strong> Smart Pointers</a></li><li><ol><li><a href="ch15-01-box.html"><strong>15.1.</strong> Using Box&lt;T&gt; to Point to Data on the Heap</a></li><li><a href="ch15-02-deref.html"><strong>15.2.</strong> Treating Smart Pointers Like Regular References with the Deref Trait</a></li><li><a href="ch15-03-drop.html"><strong>15.3.</strong> Running Code on Cleanup with the Drop Trait</a></li><li><a href="ch15-04-rc.html"><strong>15.4.</strong> Rc&lt;T&gt;, the Reference Counted Smart Pointer</a></li><li><a href="ch15-05-interior-mutability.html"><strong>15.5.</strong> RefCell&lt;T&gt; and the Interior Mutability Pattern</a></li><li><a href="ch15-06-reference-cycles.html"><strong>15.6.</strong> Reference Cycles Can Leak Memory</a></li></ol></li><li><a href="ch16-00-concurrency.html"><strong>16.</strong> Fearless Concurrency</a></li><li><ol><li><a href="ch16-01-threads.html"><strong>16.1.</strong> Using Threads to Run Code Simultaneously</a></li><li><a href="ch16-02-message-passing.html"><strong>16.2.</strong> Using Message Passing to Transfer Data Between Threads</a></li><li><a href="ch16-03-shared-state.html"><strong>16.3.</strong> Shared-State Concurrency</a></li><li><a href="ch16-04-extensible-concurrency-sync-and-send.html"><strong>16.4.</strong> Extensible Concurrency with the Sync and Send Traits</a></li></ol></li><li><a href="ch17-00-oop.html"><strong>17.</strong> Object Oriented Programming Features of Rust</a></li><li><ol><li><a href="ch17-01-what-is-oo.html"><strong>17.1.</strong> Characteristics of Object-Oriented Languages</a></li><li><a href="ch17-02-trait-objects.html"><strong>17.2.</strong> Using Trait Objects That Allow for Values of Different Types</a></li><li><a href="ch17-03-oo-design-patterns.html"><strong>17.3.</strong> Implementing an Object-Oriented Design Pattern</a></li></ol></li><li><a href="ch18-00-patterns.html"><strong>18.</strong> Patterns and Matching</a></li><li><ol><li><a href="ch18-01-all-the-places-for-patterns.html"><strong>18.1.</strong> All the Places Patterns Can Be Used</a></li><li><a href="ch18-02-refutability.html"><strong>18.2.</strong> Refutability: Whether a Pattern Might Fail to Match</a></li><li><a href="ch18-03-pattern-syntax.html"><strong>18.3.</strong> Pattern Syntax</a></li></ol></li><li><a href="ch19-00-advanced-features.html"><strong>19.</strong> Advanced Features</a></li><li><ol><li><a href="ch19-01-unsafe-rust.html"><strong>19.1.</strong> Unsafe Rust</a></li><li><a href="ch19-03-advanced-traits.html"><strong>19.2.</strong> Advanced Traits</a></li><li><a href="ch19-04-advanced-types.html"><strong>19.3.</strong> Advanced Types</a></li><li><a href="ch19-05-advanced-functions-and-closures.html"><strong>19.4.</strong> Advanced Functions and Closures</a></li><li><a href="ch19-06-macros.html"><strong>19.5.</strong> Macros</a></li></ol></li><li><a href="ch20-00-final-project-a-web-server.html"><strong>20.</strong> Final Project: Building a Multithreaded Web Server</a></li><li><ol><li><a href="ch20-01-single-threaded.html"><strong>20.1.</strong> Building a Single-Threaded Web Server</a></li><li><a href="ch20-02-multithreaded.html"><strong>20.2.</strong> Turning Our Single-Threaded Server into a Multithreaded Server</a></li><li><a href="ch20-03-graceful-shutdown-and-cleanup.html"><strong>20.3.</strong> Graceful Shutdown and Cleanup</a></li></ol></li><li><a href="appendix-00.html"><strong>21.</strong> Appendix</a></li><li><ol><li><a href="appendix-01-keywords.html"><strong>21.1.</strong> A - Keywords</a></li><li><a href="appendix-02-operators.html"><strong>21.2.</strong> B - Operators and Symbols</a></li><li><a href="appendix-03-derivable-traits.html"><strong>21.3.</strong> C - Derivable Traits</a></li><li><a href="appendix-04-useful-development-tools.html"><strong>21.4.</strong> D - Useful Development Tools</a></li><li><a href="appendix-05-editions.html"><strong>21.5.</strong> E - Editions</a></li><li><a href="appendix-06-translation.html"><strong>21.6.</strong> F - Translations of the Book</a></li><li><a href="appendix-07-nightly-rust.html"><strong>21.7.</strong> G - How Rust is Made and âNightly Rustâ</a></li></ol></li></ol>
 </div>
 
 </nav>
 <div>
 <div>
 
 <h2>The Rust Programming Language</h2>
 
 </div>
 <nav>
 <a href="foreword.html">
 <i></i>
 </a>
 </nav>
 </div>
 
 
 
 
 
 
 
 
 
 
||||I|||| 1. The Rust Programming Language
 2. Foreword
 3. Introduction
 4. 1. Getting Started
 5. 
 1. 1.1. Installation
 2. 1.2. Hello, World!
 3. 1.3. Hello, Cargo!
 6. 2. Programming a Guessing Game
 7. 3. Common Programming Concepts
 8. 
 1. 3.1. Variables and Mutability
 2. 3.2. Data Types
 3. 3.3. Functions
 4. 3.4. Comments
 5. 3.5. Control Flow
 9. 4. Understanding Ownership
 10. 
 1. 4.1. What is Ownership?
 2. 4.2. References and Borrowing
 3. 4.3. The Slice Type
 11. 5. Using Structs to Structure Related Data
 12. 
 1. 5.1. Defining and Instantiating Structs
 2. 5.2. An Example Program Using Structs
 3. 5.3. Method Syntax
 13. 6. Enums and Pattern Matching
 14. 
 1. 6.1. Defining an Enum
 2. 6.2. The match Control Flow Construct
 3. 6.3. Concise Control Flow with if let
 15. 7. Managing Growing Projects with Packages, Crates, and Modules
 16. 
 1. 7.1. Packages and Crates
 2. 7.2. Defining Modules to Control Scope and Privacy
 3. 7.3. Paths for Referring to an Item in the Module Tree
 4. 7.4. Bringing Paths Into Scope with the use Keyword
 5. 7.5. Separating Modules into Different Files
 17. 8. Common Collections
 18. 
 1. 8.1. Storing Lists of Values with Vectors
 2. 8.2. Storing UTF-8 Encoded Text with Strings
 3. 8.3. Storing Keys with Associated Values in Hash Maps
 19. 9. Error Handling
 20. 
 1. 9.1. Unrecoverable Errors with panic!
 2. 9.2. Recoverable Errors with Result
 3. 9.3. To panic! or Not to panic!
 21. 10. Generic Types, Traits, and Lifetimes
 22. 
 1. 10.1. Generic Data Types
 2. 10.2. Traits: Defining Shared Behavior
 3. 10.3. Validating References with Lifetimes
 23. 11. Writing Automated Tests
 24. 
 1. 11.1. How to Write Tests
 2. 11.2. Controlling How Tests Are Run
 3. 11.3. Test Organization
 25. 12. An I/O Project: Building a Command Line Program
 26. 
 1. 12.1. Accepting Command Line Arguments
 2. 12.2. Reading a File
 3. 12.3. Refactoring to Improve Modularity and Error Handling
 4. 12.4. Developing the Libraryâs Functionality with Test Driven Development
 5. 12.5. Working with Environment Variables
 6. 12.6. Writing Error Messages to Standard Error Instead of Standard Output
 27. 13. Functional Language Features: Iterators and Closures
 28. 
 1. 13.1. Closures: Anonymous Functions that Capture Their Environment
 2. 13.2. Processing a Series of Items with Iterators
 3. 13.3. Improving Our I/O Project
 4. 13.4. Comparing Performance: Loops vs. Iterators
 29. 14. More about Cargo and Crates.io
 30. 
 1. 14.1. Customizing Builds with Release Profiles
 2. 14.2. Publishing a Crate to Crates.io
 3. 14.3. Cargo Workspaces
 4. 14.4. Installing Binaries from Crates.io with cargo install
 5. 14.5. Extending Cargo with Custom Commands
 31. 15. Smart Pointers
 32. 
 1. 15.1. Using Box<T> to Point to Data on the Heap
 2. 15.2. Treating Smart Pointers Like Regular References with the Deref Trait
 3. 15.3. Running Code on Cleanup with the Drop Trait
 4. 15.4. Rc<T>, the Reference Counted Smart Pointer
 5. 15.5. RefCell<T> and the Interior Mutability Pattern
 6. 15.6. Reference Cycles Can Leak Memory
 33. 16. Fearless Concurrency
 34. 
 1. 16.1. Using Threads to Run Code Simultaneously
 2. 16.2. Using Message Passing to Transfer Data Between Threads
 3. 16.3. Shared-State Concurrency
 4. 16.4. Extensible Concurrency with the Sync and Send Traits
 35. 17. Object Oriented Programming Features of Rust
 36. 
 1. 17.1. Characteristics of Object-Oriented Languages
 2. 17.2. Using Trait Objects That Allow for Values of Different Types
 3. 17.3. Implementing an Object-Oriented Design Pattern
 37. 18. Patterns and Matching
 38. 
 1. 18.1. All the Places Patterns Can Be Used
 2. 18.2. Refutability: Whether a Pattern Might Fail to Match
 3. 18.3. Pattern Syntax
 39. 19. Advanced Features
 40. 
 1. 19.1. Unsafe Rust
 2. 19.2. Advanced Traits
 3. 19.3. Advanced Types
 4. 19.4. Advanced Functions and Closures
 5. 19.5. Macros
 41. 20. Final Project: Building a Multithreaded Web Server
 42. 
 1. 20.1. Building a Single-Threaded Web Server
 2. 20.2. Turning Our Single-Threaded Server into a Multithreaded Server
 3. 20.3. Graceful Shutdown and Cleanup
 43. 21. Appendix
 44. 
 1. 21.1. A - Keywords
 2. 21.2. B - Operators and Symbols
 3. 21.3. C - Derivable Traits
 4. 21.4. D - Useful Development Tools
 5. 21.5. E - Editions
 6. 21.6. F - Translations of the Book
 7. 21.7. G - How Rust is Made and âNightly Rustâ
 * Light (default)
 * Rust
 * Coal
 * Navy
 * Ayu

 The Rust Programming Language

 The Rust Programming Language

 by Steve Klabnik and Carol Nichols, with contributions from the Rust Community

 This version of the text assumes youâre using Rust 1.65 (released 2022-11-03) or later. See the âInstallationâ section of Chapter 1 to install or update Rust.

 The HTML format is available online at https://doc.rust-lang.org/stable/book/ and offline with installations of Rust made with rustup; run rustup docs --book to open.

 Several community translations are also available.

 This text is available in paperback and ebook format from No Starch Press.

 ð¨ Want a more interactive learning experience? Try out a different version of the Rust Book, featuring: quizzes, highlighting, visualizations, and more: https://rust-book.cs.brown.edu

The Rust Programming Language
https://doc.rust-lang.org/book/
None
2018-01-01T00:00:00-09:30
<div><div>
                    <a href="https://stevedonovan.github.io/rust-gentle-intro/readme.html#a-gentle-introduction-to-rust"></a>
<p></p>
<p><a href="http://leftoversalad.com/c/015_programmingpeople/">thanks to David Marino</a></p>
<a href="https://stevedonovan.github.io/rust-gentle-intro/readme.html#why-learn-a-new-programming-language"><h2>Why learn a new Programming Language?</h2></a>
<p>The aim of this tutorial is to take you to a place where you can read and write
enough Rust to fully appreciate the excellent learning resources available
online, in particular <a href="https://doc.rust-lang.org/stable/book/">The Book</a>.
It's an opportunity to <em>try before you buy</em>, and get enough feeling for the
power of the language to want to go deeper.</p>
<p>As Einstein might have said, "As gentle as possible, but no gentler.". There is a
lot of new stuff to learn here, and it's different enough to require some
rearrangement of your mental furniture. By 'gentle' I mean that the features are
presented practically with examples; as we encounter difficulties, I hope to
show how Rust solves these problems. It is important to understand the problems before
the solutions make sense. To put it in flowery language, we are going for a hike
in hilly country and I will point out some interesting rock formations on the way,
with only a few geology lectures. There will be some uphill but the view will be
inspiring; the community is unusually pleasant and happy to help.
There is the <a href="https://users.rust-lang.org/">Rust Users Forum</a> and an active
<a href="https://www.reddit.com/r/rust/">subreddit</a> which is unusually well-moderated.
The <a href="https://www.rust-lang.org/en-US/faq.html">FAQ</a> is a good resource if
you have specific questions.</p>
<p>First, why learn a new programming language? It is an investment of time and energy
and that needs some justification. Even if you do not immediately land
a cool job using that language, it stretches the mental muscles and makes you a
better programmer. That seems a poor kind of return-on-investment but if you're
not learning something <em>genuinely</em> new all the time then you will stagnate and be
like the person who has ten years of experience in doing the same thing over and over.</p>
<a href="https://stevedonovan.github.io/rust-gentle-intro/readme.html#where-rust-shines"><h2>Where Rust Shines</h2></a>
<p>Rust is a statically and strongly typed systems programming language. <em>statically</em>
means that all types are known at compile-time, <em>strongly</em> means that these types
are designed to make it harder to write incorrect programs. A successful compilation
means you have a much better guarantee of correctness than with a cowboy language
like C. <em>systems</em> means generating the best possible machine code with full control
of memory use.  So the uses are pretty hardcore: operating systems, device drivers
and embedded systems that might not even have an operating system.  However, it's
actually a very pleasant language to write normal application code in as well.</p>
<p>The big difference from C and C++ is that Rust is <em>safe by default</em>; all memory accesses
are checked. It is not possible to corrupt memory by accident.</p>
<p>The unifying principles behind Rust are:</p>
<ul>
<li>strictly enforcing <em>safe borrowing</em> of data</li>
<li>functions, methods and closures to operate on data</li>
<li>tuples, structs and enums to aggregate data</li>
<li>pattern matching to select and destructure data</li>
<li>traits to define <em>behaviour</em> on data</li>
</ul>
<p>There is a fast-growing ecosystem of available libraries through Cargo
but here we will concentrate on the core principles of the language
by learning to use the standard library. My advice is to write <em>lots of small programs</em>,
so learning to use <code>rustc</code> directly is a core skill. When doing the examples in this
tutorial I defined a little script called <code>rrun</code> which does a compilation and runs
the result:</p>
<pre><code>rustc $1.rs &amp;&amp; ./$1
</code></pre>
<a href="https://stevedonovan.github.io/rust-gentle-intro/readme.html#setting-up"><h2>Setting Up</h2></a>
<p>This tutorial assumes that you have Rust installed locally. Fortunately this is
<a href="https://www.rust-lang.org/en-US/downloads.html">very straightforward</a>.</p>
<pre><code>$ curl https://sh.rustup.rs -sSf | sh
$ rustup component add rust-docs
</code></pre>
<p>I would recommend getting the default stable version; it's easy to download
unstable versions later and to switch between.</p>
<p>This gets the compiler, the Cargo package manager, the API documentation, and the Rust Book.
The journey of a thousand miles starts with one step, and this first step is painless.</p>
<p><code>rustup</code> is the command you use to manage your Rust installation. When a new stable release
appears, you just have to say <code>rustup update</code> to upgrade. <code>rustup doc</code> will open
the offline documentation in your browser.</p>
<p>You will probably already have an editor you like, and <a href="https://areweideyet.com/">basic Rust support</a>
is good. I'd suggest you start out with basic syntax highlighting at first, and
work up as your programs get larger.</p>
<p>Personally I'm a fan of <a href="https://www.geany.org/Download/Releases">Geany</a> which is
one of the few editors with Rust support out-of-the-box; it's particularly easy
on Linux since it's available through the package manager, but it works fine on
other platforms.</p>
<p>The main thing is knowing how to edit, compile and run Rust programs.
You learn to program with your <em>fingers</em>; type in
the code yourself, and learn to rearrange things efficiently with your editor.</p>
<p>Zed Shaw's <a href="https://learnpythonthehardway.org/book/intro.html">advice</a> about learning
to program in Python remains good, whatever the language. He says learning to program
is like learning a musical instrument - the secret is practice and persistence.
There's also good advice from Yoga and the soft martial arts like Tai Chi;
feel the strain, but don't over-strain. You are not building dumb muscle here.</p>
<p>I'd like to thank the many contributors who caught bad English or bad Rust for me,
and thanks to David Marino for his cool characterization
of Rust as a friendly-but-hardcore no-nonsense knight in shining armour.</p>
<p>Steve Donovan © 2017-2018 MIT license version 0.4.0</p>

                </div></div>||||I||||    * Introduction
    * 1. Basics
    * 2. Structs, Enums and Matching
    * 3. Filesystem and Processes
    * 4. Modules and Cargo
    * 5. Standard Library Containers
    * 6. Error Handling
    * 7. Threads, Networking and Sharing
    * 8. Object-Oriented Programming
    * 9. Parsing with Nom
    * 10. Pain Points

      A Gentle Introduction to Rust

      A Gentle Introduction To Rust

      thanks to David Marino

      Why learn a new Programming Language?

      The aim of this tutorial is to take you to a place where you can read and write enough Rust to fully appreciate the excellent learning resources available online, in particular The Book. It's an opportunity to try before you buy, and get enough feeling for the power of the language to want to go deeper.

      As Einstein might have said, "As gentle as possible, but no gentler.". There is a lot of new stuff to learn here, and it's different enough to require some rearrangement of your mental furniture. By 'gentle' I mean that the features are presented practically with examples; as we encounter difficulties, I hope to show how Rust solves these problems. It is important to understand the problems before the solutions make sense. To put it in flowery language, we are going for a hike in hilly country and I will point out some interesting rock formations on the way, with only a few geology lectures. There will be some uphill but the view will be inspiring; the community is unusually pleasant and happy to help. There is the Rust Users Forum and an active subreddit which is unusually well-moderated. The FAQ is a good resource if you have specific questions.

      First, why learn a new programming language? It is an investment of time and energy and that needs some justification. Even if you do not immediately land a cool job using that language, it stretches the mental muscles and makes you a better programmer. That seems a poor kind of return-on-investment but if you're not learning something genuinely new all the time then you will stagnate and be like the person who has ten years of experience in doing the same thing over and over.

      Where Rust Shines

      Rust is a statically and strongly typed systems programming language. statically means that all types are known at compile-time, strongly means that these types are designed to make it harder to write incorrect programs. A successful compilation means you have a much better guarantee of correctness than with a cowboy language like C. systems means generating the best possible machine code with full control of memory use. So the uses are pretty hardcore: operating systems, device drivers and embedded systems that might not even have an operating system. However, it's actually a very pleasant language to write normal application code in as well.

      The big difference from C and C++ is that Rust is safe by default; all memory accesses are checked. It is not possible to corrupt memory by accident.

      The unifying principles behind Rust are:

        * strictly enforcing safe borrowing of data
        * functions, methods and closures to operate on data
        * tuples, structs and enums to aggregate data
        * pattern matching to select and destructure data
        * traits to define behaviour on data

      There is a fast-growing ecosystem of available libraries through Cargo but here we will concentrate on the core principles of the language by learning to use the standard library. My advice is to write lots of small programs, so learning to use rustc directly is a core skill. When doing the examples in this tutorial I defined a little script called rrun which does a compilation and runs the result:

      rustc $1.rs && ./$1
      

      Setting Up

      This tutorial assumes that you have Rust installed locally. Fortunately this is very straightforward.

      $ curl https://sh.rustup.rs -sSf | sh
      $ rustup component add rust-docs
      

      I would recommend getting the default stable version; it's easy to download unstable versions later and to switch between.

      This gets the compiler, the Cargo package manager, the API documentation, and the Rust Book. The journey of a thousand miles starts with one step, and this first step is painless.

      rustup is the command you use to manage your Rust installation. When a new stable release appears, you just have to say rustup update to upgrade. rustup doc will open the offline documentation in your browser.

      You will probably already have an editor you like, and basic Rust support is good. I'd suggest you start out with basic syntax highlighting at first, and work up as your programs get larger.

      Personally I'm a fan of Geany which is one of the few editors with Rust support out-of-the-box; it's particularly easy on Linux since it's available through the package manager, but it works fine on other platforms.

      The main thing is knowing how to edit, compile and run Rust programs. You learn to program with your fingers; type in the code yourself, and learn to rearrange things efficiently with your editor.

      Zed Shaw's advice about learning to program in Python remains good, whatever the language. He says learning to program is like learning a musical instrument - the secret is practice and persistence. There's also good advice from Yoga and the soft martial arts like Tai Chi; feel the strain, but don't over-strain. You are not building dumb muscle here.

      I'd like to thank the many contributors who caught bad English or bad Rust for me, and thanks to David Marino for his cool characterization of Rust as a friendly-but-hardcore no-nonsense knight in shining armour.

      Steve Donovan © 2017-2018 MIT license version 0.4.0

A Gentle Introduction to Rust
https://stevedonovan.github.io/rust-gentle-intro/
None
2024-01-01T00:00:00Z
<div><div>
<p>
</p>



<p>Rust is a modern systems programming language developed by the Mozilla Corporation. It is intended to be a language for highly concurrent and highly secure systems. It compiles to native code; hence, it is blazingly fast like C and C++. This tutorial adopts a simple and practical approach to describe the concepts of Rust programming.</p>
<h2>Audience</h2>
<p>This tutorial has been prepared for beginners to help them understand the basic and advanced concepts of Rust.</p>
<h2>Prerequisites</h2>
<p>We assume that the reader has an understanding of basic programming concepts is necessary for this course.</p>

		
</div></div>||||I||||      * Home
      * Coding Ground
      * Jobs
      * Whiteboard
      * Tools
      * Articles
      * Write and Earn
    Menu Categories
  Login
    * Switch theme
    * 
    * 
    * 
    * 
    * 
    * Category
              + AI, ML, and Data Science
              + Programming Languages
              + Web Development Languages
              + DevOps
              + Databases
              + Computer Science Subjects
              + Python Technologies
              + Software Testing
              + Cyber Security
              + All Categories
                  Back
                    + Artificial Intelligence
                    + Machine Learning
                    + ML With Python
                    + Data Science
                    + Statistics
                    + NLP
                    + Neural Networks
                    + TensorFlow
                    + PyTorch
                    + Matplotlib
                    + NumPy
                    + Pandas
                    + SciPy
                    + Big Data Analytics
                    + See all
                  Back
                    + Python
                    + Java
                    + C++
                    + C
                    + PHP
                    + Go
                    + Kotlin
                    + R
                    + ASP.Net
                    + C#.Net
                    + VB.Net
                    + Scala
                    + Swift
                    + Perl
                    + Ruby
                    + Rust
                    + Lua
                    + See all
                  Back
                    + HTML
                    + CSS
                    + JavaScript
                    + jQuery
                    + ReactJs
                    + NodeJs
                    + Wordpress
                    + AngularJs
                    + PHP
                    + Django
                    + JSON
                    + Codeigniter
                    + TypeScript
                    + Ajax
                    + Bootstrap
                    + Sass
                    + AppML
                    + See all
                  Back
                    + GIT
                    + AWS
                    + Docker
                    + Kubernetes
                    + Azure
                    + OpenShift
                    + Jira
                    + Gerrit
                    + Ansible
                    + Bugzilla
                    + Chef
                    + Gitlab
                    + OpenShift
                    + Puppet
                    + UNIX
                    + Linux Admin
                    + Ubuntu
                    + See all
                  Back
                    + DBMS
                    + SQL
                    + PL/SQL
                    + MySQL
                    + TinyDB
                    + SQL Server
                    + MongoDB
                    + Postgrey SQL
                    + SQLite
                    + Redis
                    + PHP MyAdmin
                    + MariaDB
                    + CouchDB
                    + DB2
                    + See all
                  Back
                    + Computer Fundamentals
                    + Operating System
                    + DBMS
                    + DSA
                    + Computer Networks
                    + Software Engineering
                    + Computer Graphics
                    + Data Mining
                    + Digital Marketing
                    + SEO
                    + Digital Circuits
                    + Desecrate Mathematics
                    + Cryptography
                    + Cloud Computing
                    + Compiler Design
                    + Embedded Systems
                    + Microprocessors
                    + See all
                  Back
                    + Python
                    + NumPy
                    + Pandas
                    + Matplotlib
                    + Django
                    + PyQt
                    + PyCharm
                    + Pillow
                    + OpenCV
                    + Seaborn
                    + ML with Python
                    + SciPy
                    + See all
                  Back
                    + Software Testing
                    + Jira
                    + Selenium
                    + TestRail
                    + Postman
                    + Cucumber
                    + Cypress
                    + Watir
                    + Agile
                    + jMeter
                  Back
                    + Blockchain
                    + Information Security
                    + Computer Security
                    + Internet Security
                    + Network Security
                    + Wireless Security
    * 
    * Library
    * Courses
    * Certifications
    * Login
  Menu
    Show search
      * HTML
      * CSS
      * Javascript
      * SQL
      * Python
      * Java
      * C
      * C++
      * PHP
      * Scala
      * C#
      * Node.js
      * MySQL
      * MongoDB
      * PL/SQL
      * Swift
      * Bootstrap
      * R
      * Machine Learning
      * Blockchain
      * Angular
      * React Native
      * Computer Fundamentals
      * Compiler Design
      * Operating System
      * Data Structure and Algorithms
      * Computer Network
      * DBMS
      * Excel
          * Rust Tutorial
          * Rust - Home
          * Rust - Introduction
          * Rust - Environment Setup
          * Rust - HelloWorld Example
          * Rust - Data Types
          * Rust - Variables
          * Rust - Constant
          * Rust - String
          * Rust - Operators
          * Rust - Decision Making
          * Rust - Loop
          * Rust - Functions
          * Rust - Tuple
          * Rust - Array
          * Rust - Ownership
          * Rust - Borrowing
          * Rust - Slices
          * Rust - Structure
          * Rust - Enums
          * Rust - Modules
          * Rust - Collections
          * Rust - Error Handling
          * Rust - Generic Types
          * Rust - Input Output
          * Rust - File Input/ Output
          * Rust - Package Manager
          * Rust - Iterator and Closure
          * Rust - Smart Pointers
          * Rust - Concurrency
          * Rust Useful Resources
          * Rust - Quick Guide
          * Rust - Useful Resources
          * Rust - Discussion
          * Selected Reading
          * UPSC IAS Exams Notes
          * Developer's Best Practices
          * Questions and Answers
          * Effective Resume Writing
          * HR Interview Questions
          * Computer Glossary
          * Who is Who

      Rust Tutorial

        PDF Version Quick Guide Resources Job Search Discussion

      Rust is a modern systems programming language developed by the Mozilla Corporation. It is intended to be a language for highly concurrent and highly secure systems. It compiles to native code; hence, it is blazingly fast like C and C++. This tutorial adopts a simple and practical approach to describe the concepts of Rust programming.

      Audience

      This tutorial has been prepared for beginners to help them understand the basic and advanced concepts of Rust.

      Prerequisites

      We assume that the reader has an understanding of basic programming concepts is necessary for this course.

          Print Page
            Previous
            Next
            Advertisements


    Tutorials Point is a leading Ed Tech company striving to provide the best learning material on technical and non-technical subjects.

    About us

        * Company
        * Our Team
        * Careers
        * Jobs
        * Contact Us
        * Terms of use
        * Privacy Policy
        * Refund Policy
        * Cookies Policy
        * FAQ's
      * Tutorials Point India Private Limited, Incor9 Building, Kavuri Hills, Madhapur, Hyderabad, Telangana - 500081, INDIA
      * 
      * 
      * 
      * 
      * 
      Tutorials Articles Jobs Courses Certifications Annual Membership

      Languages

        * Python Tutorial
        * Java Tutorial
        * C++ Tutorial
        * C Programming Tutorial
        * C# Tutorial
        * PHP Tutorial
        * R Tutorial
        * Go Tutorial

      Web Technologies

        * HTML Tutorial
        * CSS Tutorial
        * JavaScript Tutorial
        * ReactJS Tutorial
        * Bootstrap Tutorial
        * AngularJS Tutorial
        * Node.js Tutorial
        * TypeScript Tutorial

      Database

        * SQL Tutorial
        * MySQL Tutorial
        * DBMS Tutorial
        * MongoDB Tutorial
        * SQLite Tutorial
        * PL/SQL Tutorial
        * PostgreSQL Tutorial
        * Excel Tutorial

      Editors

        * Online SQL Editor
        * Online Html Editor
        * Online Css Editor
        * Online Javascript Editor
        * Online Latext Editor
        * Online TEX Editor
        * Online Mathml Compiler
        * Online Markdown Editor

      Trending Technologies

        * Cloud Computing Tutorial
        * Amazon Web Services Tutorial
        * Microsoft Azure Tutorial
        * Git Tutorial
        * Ethical Hacking Tutorial
        * Docker Tutorial
        * Kubernetes Tutorial

      Compilers

        * Online Java Compiler
        * Online C Compiler
        * Online C++ Compiler
        * Online C# Compiler
        * Online Php Compiler
        * Online Matlab Compiler
        * Online Bash Compiler

      Terminals

        * Online Unix Terminal
        * Online Python3 Terminal
        * Online Php Terminal
        * Online Nodejs Terminal
        * Online R Terminal
        * Online Numpy Terminal
        * Online Octave Terminal

      Data Science & ML

        * NLP Tutorial
        * NumPy Tutorial
        * Python Pandas Tutorial
        * Machine Learning Tutorial
        * Big Data Analytics Tutorial
        * Cryptography Tutorial
        * Power BI Tutorial

      Computer Science

        * DSA Tutorial
        * Spring Boot Tutorial
        * SDLC Tutorial
        * Unix Tutorial
        * Operating System Tutorial
        * Assembly Programming Tutorial
        * Digital Circuits Tutorial
        * Microprocessor Tutorial
        * System Analysis and Design Tutorial
        * Flutter Tutorial

      Top Certifications

        * Business Analytics Certification
        * Java & Spring Boot Advanced Certification
        * Data Science Advanced Certification
        * Advanced Certification In Cloud Computing And DevOps
        * Advanced Certification In Business Analytics
        * Artificial Intelligence And Machine Learning Certification
        * DevOps Certification

        * Game Development Certification
        * Front-End Developer Certification
        * AWS Certification Training
        * Python Programming Certification
        * Generative AI Certification
        * Microsoft Excel Certification Training
        * Java Certification
        * Cyber Security Certification
        * Coding For Beginners Certification

        * JavaScript Certification
        * Apache Spark Certification
        * Advanced Python Certification
        * Back-End Developer Certification
        * Front-End Developer Certification
        * Web Developer Advanced Certification
        * Linux System Administrator Certification
        * Automation Testing Certification Training

    © Copyright 2024. All Rights Reserved.

Rust Tutorial
https://www.tutorialspoint.com/rust/index.htm