doc.rust-lang.org
Generics Generics is the topic of generalizing types and functionalities to broader cases. This is extremely useful for reducing code duplication in many ways, but can call for rather involved syntax. Namely, being generic requires taking g...
Rust by Example
Book
2024-01-01
~1 min read
doc.rust-lang.org
Custom Some conditionals like target_os are implicitly provided by rustc, but custom conditionals must be passed to rustc using the --cfg flag. #[cfg(some_condition)] fn conditional_function() { println!("condition met!"); } fn main() { con...
Rust by Example
Book
2024-01-01
~1 min read
doc.rust-lang.org
cfg Configuration conditional checks are possible through two different operators: • the cfg attribute: #[cfg(...)] in attribute position • the cfg! macro: cfg!(...) in boolean expressions While the former enables conditional compilation, t...
Rust by Example
Book
2024-01-01
~1 min read
doc.rust-lang.org
Crates The crate_type attribute can be used to tell the compiler whether a crate is a binary or a library (and even which type of library), and the crate_name attribute can be used to set the name of the crate. However, it is important to n...
Rust by Example
Book
2024-01-01
~1 min read
doc.rust-lang.org
dead_code The compiler provides a dead_code lint that will warn about unused functions. An attribute can be used to disable the lint. fn used_function() {} // `#[allow(dead_code)]` is an attribute that disables the `dead_code` lint #[allow(...
Rust by Example
Book
2024-01-01
~1 min read
doc.rust-lang.org
Attributes An attribute is metadata applied to some module, crate or item. This metadata can be used to/for: • conditional compilation of code • set crate name, version and type (binary or library) • disable lints (warnings) • enable compil...
Rust by Example
Book
2024-01-01
~1 min read
doc.rust-lang.org
Build Scripts Sometimes a normal build from cargo is not enough. Perhaps your crate needs some pre-requisites before cargo will successfully compile, things like code generation, or some native code that needs to be compiled. To solve this...
Rust by Example
Book
2024-01-01
~1 min read
doc.rust-lang.org
Testing As we know testing is integral to any piece of software! Rust has first-class support for unit and integration testing (see this chapter in TRPL). From the testing chapters linked above, we see how to write unit tests and integratio...
Rust by Example
Book
2024-01-01
~2 min read
doc.rust-lang.org
Conventions In the previous chapter, we saw the following directory hierarchy: foo ├── Cargo.toml └── src └── main.rs Suppose that we wanted to have two binaries in the same project, though. What then? It turns out that cargo supports this....
Rust by Example
Book
2024-01-01
~1 min read
doc.rust-lang.org
Dependencies Most programs have dependencies on some libraries. If you have ever managed dependencies by hand, you know how much of a pain this can be. Luckily, the Rust ecosystem comes standard with cargo! cargo can manage dependencies for...
Rust by Example
Book
2024-01-01
~2 min read
doc.rust-lang.org
Cargo cargo is the official Rust package management tool. It has lots of really useful features to improve code quality and developer velocity! These include • Dependency management and integration with crates.io (the official Rust package...
Rust by Example
Book
2024-01-01
~1 min read
doc.rust-lang.org
Using a Library To link a crate to this new library you may use rustc's --extern flag. All of its items will then be imported under a module named the same as the library. This module generally behaves the same way as any other module. // e...
Rust by Example
Book
2024-01-01
~1 min read
doc.rust-lang.org
Creating a Library Let's create a library, and then see how to link it to another crate. In rary.rs: pub fn public_function() { println!("called rary's `public_function()`"); } fn private_function() { println!("called rary's `private_functi...
Rust by Example
Book
2024-01-01
~1 min read
doc.rust-lang.org
Crates A crate is a compilation unit in Rust. Whenever rustc some_file.rs is called, some_file.rs is treated as the crate file. If some_file.rs has mod declarations in it, then the contents of the module files would be inserted in places wh...
Rust by Example
Book
2024-01-01
~1 min read
doc.rust-lang.org
File hierarchy Modules can be mapped to a file/directory hierarchy. Let's break down the visibility example in files: $ tree . . ├── my │ ├── inaccessible.rs │ └── nested.rs ├── my.rs └── split.rs In split.rs: // This declaration will look...
Rust by Example
Book
2024-01-01
~1 min read
doc.rust-lang.org
super and self The super and self keywords can be used in the path to remove ambiguity when accessing items and to prevent unnecessary hardcoding of paths. fn function() { println!("called `function()`"); } mod cool { pub fn function() { pr...
Rust by Example
Book
2024-01-01
~1 min read
doc.rust-lang.org
The use declaration The use declaration can be used to bind a full path to a new name, for easier access. It is often used like this: use crate::deeply::nested::{ my_first_function, my_second_function, AndATraitType }; fn main() { my_first_...
Rust by Example
Book
2024-01-01
~1 min read
doc.rust-lang.org
Struct visibility Structs have an extra level of visibility with their fields. The visibility defaults to private, and can be overridden with the pub modifier. This visibility only matters when a struct is accessed from outside the module w...
Rust by Example
Book
2024-01-01
~1 min read
doc.rust-lang.org
Visibility By default, the items in a module have private visibility, but this can be overridden with the pub modifier. Only the public items of a module can be accessed from outside the module scope. // A module named `my_mod` mod my_mod {...
Rust by Example
Book
2024-01-01
~2 min read
doc.rust-lang.org
Modules Rust provides a powerful module system that can be used to hierarchically split code in logical units (modules), and manage visibility (public/private) between them. A module is a collection of items: functions, structs, traits, imp...
Rust by Example
Book
2024-01-01
~1 min read