You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<li>Generate the project documentation via <ahref="https://doc.rust-lang.org/stable/rustdoc/">rustdoc</a>: <code>cargo doc</code></li>
403
-
<li>Analyze the project to see it has any errors, without building it: <code>cargo check</code></li>
404
-
</ul>
405
-
<p>In addition, there are <code>cargo</code> commands to publish the project as a crate/ package to <strong>Rust’s official crate registry, <ahref="https://crates.io/">crates.io</a></strong>.</p>
393
+
<p>Cargo is Rust’s built-in package manager and build system. It also supports the following actions,</p>
394
+
<table>
395
+
<thead>
396
+
<tr>
397
+
<th>Command</th>
398
+
<th>Action</th>
399
+
</tr>
400
+
</thead>
401
+
<tbody>
402
+
<tr>
403
+
<td><code>cargo new</code></td>
404
+
<td>Create a new project</td>
405
+
</tr>
406
+
<tr>
407
+
<td><code>cargo init</code></td>
408
+
<td>Create a new project in an existing directory</td>
409
+
</tr>
410
+
<tr>
411
+
<td><code>cargo check</code></td>
412
+
<td>Verify the project compiles without errors</td>
413
+
</tr>
414
+
<tr>
415
+
<td><code>cargo build</code></td>
416
+
<td>Build the executable</td>
417
+
</tr>
418
+
<tr>
419
+
<td><code>cargo run</code></td>
420
+
<td>Build the executable and run</td>
421
+
</tr>
422
+
</tbody>
423
+
</table>
406
424
<blockquote>
407
-
<p>💡 We need to get an API token from <ahref="https://crates.io/">crates.io</a> to publish a crate to it. The API token can be found in the <ahref="https://crates.io/me">Account Settings page</a>, after login to that site. We will discuss more about this under <ahref="/docs/crates#c-using-cratesio">code organization with crates</a>.</p>
425
+
<p>💡 The <code>cargo check</code> command verifies that the project compiles without errors, without producing an executable.
426
+
Thus, it is often faster than <code>cargo build</code>.</p>
427
+
</blockquote>
428
+
<blockquote>
429
+
<p>💡 Cargo places executables compiled with <code>cargo build</code> or <code>cargo run</code> in the <code>target/debug/</code> directory.
430
+
But, while those built with <strong><code>cargo build --release</code></strong> for release purposes are stored in <code>target/release/</code> directory.
431
+
Release builds use more optimizations and remove some runtime safety checks to increase performance, although this comes at the cost of longer compile time.</p>
432
+
</blockquote>
433
+
<table>
434
+
<thead>
435
+
<tr>
436
+
<th>Command</th>
437
+
<th>Action</th>
438
+
</tr>
439
+
</thead>
440
+
<tbody>
441
+
<tr>
442
+
<td><code>cargo add</code></td>
443
+
<td>Add a dependency crate to the project</td>
444
+
</tr>
445
+
<tr>
446
+
<td><code>cargo remove</code></td>
447
+
<td>Remove a dependency crate from the project</td>
448
+
</tr>
449
+
<tr>
450
+
<td><code>cargo fetch</code></td>
451
+
<td>Download the dependencies specified in Cargo.lock</td>
452
+
</tr>
453
+
<tr>
454
+
<td><code>cargo update</code></td>
455
+
<td>Update project dependencies</td>
456
+
</tr>
457
+
</tbody>
458
+
</table>
459
+
<blockquote>
460
+
<p>💡 A crate is a package that can be shared via <ahref="https://crates.io">crates.io</a>, Rust community’s crate registry.
461
+
<code>cargo add</code>, <code>cargo remove</code>, <code>cargo fetch</code>, and <code>cargo update</code> commands manage project dependencies through the crate hosted on crates.io.</p>
462
+
</blockquote>
463
+
<blockquote>
464
+
<p>💡 The <code>cargo add</code> command includes a specified crate in the <code>[dependencies]</code> section of <code>Cargo.toml</code>, while <code>cargo add --dev</code> adds a crate to the <code>[dev-dependencies]</code> section. This indicates that the crate is only used for development purposes like testing and will not be included in the final compiled code.</p>
465
+
</blockquote>
466
+
<table>
467
+
<thead>
468
+
<tr>
469
+
<th>Command</th>
470
+
<th>Action</th>
471
+
</tr>
472
+
</thead>
473
+
<tbody>
474
+
<tr>
475
+
<td><code>cargo test</code></td>
476
+
<td>Run tests</td>
477
+
</tr>
478
+
<tr>
479
+
<td><code>cargo bench</code></td>
480
+
<td>Run benchmarks</td>
481
+
</tr>
482
+
<tr>
483
+
<td><code>cargo doc</code></td>
484
+
<td>Generate the project documentation via <ahref="https://doc.rust-lang.org/stable/rustdoc/">rustdoc</a></td>
485
+
</tr>
486
+
</tbody>
487
+
</table>
488
+
<p>In addition, there are <code>cargo</code> commands to publish the project as a crate to <ahref="https://crates.io/">crates.io</a>.</p>
489
+
<table>
490
+
<thead>
491
+
<tr>
492
+
<th>Command</th>
493
+
<th>Action</th>
494
+
</tr>
495
+
</thead>
496
+
<tbody>
497
+
<tr>
498
+
<td><code>cargo login</code></td>
499
+
<td>Login to <ahref="https://crates.io/">crates.io</a> with the API token</td>
500
+
</tr>
501
+
<tr>
502
+
<td><code>cargo package</code></td>
503
+
<td>Make the local crate uploadable to <ahref="https://crates.io/">crates.io</a></td>
504
+
</tr>
505
+
<tr>
506
+
<td><code>cargo publish</code></td>
507
+
<td>Upload the crate to <ahref="https://crates.io/">crates.io</a></td>
508
+
</tr>
509
+
<tr>
510
+
<td><code>cargo install</code></td>
511
+
<td>Install a Rust binary</td>
512
+
</tr>
513
+
<tr>
514
+
<td><code>cargo uninstall</code></td>
515
+
<td>Uninstall a Rust binary</td>
516
+
</tr>
517
+
</tbody>
518
+
</table>
519
+
<blockquote>
520
+
<p>💡 You need to get an API token from <ahref="https://crates.io/">crates.io</a> to publish a crate to it. The API token can be found in the <ahref="https://crates.io/me">Account Settings page</a>, after login to that site. We will discuss more about this under <ahref="/docs/crates#c-using-cratesio">code organization with crates</a>.</p>
408
521
</blockquote>
409
-
<ul>
410
-
<li>Login to <ahref="https://crates.io/">crates.io</a> with the API token: <code>cargo login</code></li>
411
-
<li>Make the local crate uploadable to <ahref="https://crates.io/">crates.io</a>: <code>cargo package</code></li>
412
-
<li>Upload the crate to <ahref="https://crates.io/">crates.io</a>: <code>cargo publish</code></li>
413
-
<li>Install a Rust binary: <code>cargo install</code></li>
414
-
<li>Uninstall a Rust binary: <code>cargo uninstall</code></li>
415
-
</ul>
416
522
<h2id="crate">Crate</h2>
417
-
<p>A crate is a package, which can be shared via <ahref="https://crates.io/">crates.io</a>. A crate can produce an executable or a library. In other words, it can be a <strong>binary</strong> crate or a <strong>library</strong> crate.</p>
523
+
<ul>
524
+
<li>
525
+
<p>A crate is a package, which can be shared via Rust community’s crate registry, <ahref="https://crates.io/">crates.io</a>.</p>
526
+
</li>
527
+
<li>
528
+
<p>A crate can produce an executable or a library. In other words, it can be a <strong>binary</strong> crate or a <strong>library</strong> crate.</p>
418
529
<ol>
419
530
<li><code>cargo new crate_name --bin</code> or <code>cargo new crate_name</code>: Produces an executable</li>
420
531
<li><code>cargo new crate_name --lib</code>: Produces a library</li>
421
532
</ol>
533
+
</li>
534
+
</ul>
422
535
<p>The first one generates,</p>
423
536
<pretabindex="0"><code>├── Cargo.toml
424
537
└── src
@@ -432,9 +545,6 @@ <h2 id="crate">Crate</h2>
432
545
<li><strong>src</strong> folder is the place to store the source code.</li>
433
546
<li>Each crate has an implicit crate root/ entry point. <strong>main.rs</strong> is the crate root for a binary crate and <strong>lib.rs</strong> is the crate root for a library crate.</li>
434
547
</ul>
435
-
<blockquote>
436
-
<p>💡 When we build a binary crate via <code>cargo build</code> or <code>cargo run</code>, the executable file will be stored in the <strong>target/debug/</strong> folder. But when building it via <strong><code>cargo build --release</code></strong> for a release it will be stored in the <strong>target/release/</strong> folder. The release builds are applying more optimizations while compiling the code, to make the code run faster. But it takes more compile time.</p>
437
-
</blockquote>
438
548
<h2id="project-structure">Project Structure</h2>
439
549
<p>This is how <ahref="https://doc.rust-lang.org/cargo/guide/project-layout.html">Cargo documentation describes</a> about the recommended project layout,</p>
<li>The default executable file is <code>src/main.rs</code>.</li>
458
571
<li>The default library file is <code>src/lib.rs</code>.</li>
459
-
<li>Other executables can be placed in <code>src/bin/*.rs</code>.</li>
572
+
<li>Other executables can be placed in,
573
+
<ul>
574
+
<li><code>src/bin/*.rs</code></li>
575
+
<li><code>src/bin/*/main.rs</code></li>
576
+
</ul>
577
+
</li>
460
578
</ul>
461
579
</li>
462
580
<li>Integration tests go in the <code>tests</code> directory (unit tests go in each file they’re testing).</li>
463
581
<li>Benchmarks go in the <code>benches</code> directory.</li>
464
582
<li>Examples go in the <code>examples</code> directory.</li>
465
583
</ul>
466
584
<h2id="rust-editions">Rust Editions</h2>
467
-
<p>After the initial release in 2015, according to the feedback got from user communities, the Rust team was focusing to increase the <strong>productivity</strong> of the language and the ecosystem. After 3 years of hard work in 2018, a new Rust edition was released with new features, simplified syntax and better tooling. We call it <strong>Rust 2018</strong> edition.</p>
468
-
<p>To keep the promise of supporting backward compatibility, the new <code>edition = "2018"</code> configuration was added to the <code>Cargo.toml</code> file. For new projects, the <code>cargo new</code> command adds this configuration by default. So, you don’t need to care. But on legacy crates, if you can not see any <code>edition</code> configuration, Cargo will consider it as a Rust 2015 edition crate.</p>
469
-
<h2id="-before-going-to-the-next">👨🏫 Before going to the next…</h2>
585
+
<p>The language has seen a series of improvements every three years through new editions since its initial stable release in 2015, including the initial version, <strong>Rust 2015</strong>, followed by <strong>Rust 2018</strong>, and the latest, <strong>Rust 2021</strong>.</p>
586
+
<p>The <code>edition</code> key in the <code>Cargo.toml</code> file denotes the edition of the Rust compiler to be used for compiling the crate. Editions are opt-in, meaning existing crates will not see these changes until they explicitly migrate to the new edition. Rust guarantees backward compatibility between editions, allowing crates using older editions of Rust to interoperate seamlessly with those using newer versions.</p>
587
+
<p>For new projects created by <code>cargo new</code>, it will set <code>edition = "2021"</code> by default in the <code>Cargo.toml</code> file. For example,</p>
</span></span></code></pre></div><h2id="-before-going-to-the-next">👨🏫 Before going to the next…</h2>
470
593
<ul>
471
594
<li>
472
-
<p>The <strong><code>.cargo/bin</code> directory of your home directory</strong> is the default location of Rust binaries. Not only the official binaries like <code>rustup</code>, <code>rustc</code>, <code>cargo</code>, <code>rustfmt</code>, <code>rustdoc</code>, <code>rls</code> and also the binaries you can install via <code>cargo install</code> command, will be stored in this directory.</p>
595
+
<p>The <code>.cargo/bin</code> directory of your home directory is the default location of Rust binaries. Not only the official binaries like <code>rustc</code>, <code>cargo</code>, <code>rustup</code>, <code>rustfmt</code>, <code>rustdoc</code>, <code>rust-analyzer</code> and also the binaries you can install via <code>cargo install</code> command, will be stored in this directory.</p>
473
596
</li>
474
597
<li>
475
598
<p>Even though the initial convention for naming crates and file names is using the <ahref="https://en.wikipedia.org/wiki/Snake_case"><code>snake_case</code></a>, some crate developers are using <code>kebab-case</code> on both crates and file names. To make your code more consistent, use the initial convention <code>snake_case</code>; especially on file names.</p>
@@ -544,7 +667,7 @@ <h2 id="-before-going-to-the-next">👨🏫 Before going to the next…<
0 commit comments