Index
- Guessing Game
- Common Programming Concepts
- Understanding Ownership
- Using Structs
- Enums and Pattern Matching
- Managing Growing Projects with Packages, Crates, and Modules
- Defining Modules to Control Scope and Privacy
- Paths for Referring to an Item in the Module Tree
- Bringing Paths into Scope with the use Keyword
- Separating Modules into Different Files
- Common Collections
- Error Handling
- Generic Types, Traits, and Lifetimes
- Writing Automated Tests
- Object Oriented Programming
- Adding dependancies
- Option Take
- RefCell
- mem
- Data Structure
- Recipe
- Semi colon
- Calling rust from python
- Default
- Crytocurrency With rust
- Function chaining
- Question Mark Operator
- Tests with println
- lib and bin
- Append vector to hash map
- Random Number
- uuid4
- uwrap and option
- Blockchain with Rust
- Near Protocol
- Actix-web
Bringing Paths into Scope with the use Keyword
mod front_of_house {
pub mod hosting {
pub fn add_to_waitlist() {}
}
}
use crate::front_of_house::hosting;
pub fn eat_at_restaurant() {
hosting::add_to_waitlist();
hosting::add_to_waitlist();
hosting::add_to_waitlist();
}
pub mod hosting {
pub fn add_to_waitlist() {}
}
}
use crate::front_of_house::hosting;
pub fn eat_at_restaurant() {
hosting::add_to_waitlist();
hosting::add_to_waitlist();
hosting::add_to_waitlist();
}
You can also bring an item into scope with use and a relative path
mod front_of_house {
pub mod hosting {
pub fn add_to_waitlist() {}
}
}
use self::front_of_house::hosting;
pub fn eat_at_restaurant() {
hosting::add_to_waitlist();
hosting::add_to_waitlist();
hosting::add_to_waitlist();
}
pub mod hosting {
pub fn add_to_waitlist() {}
}
}
use self::front_of_house::hosting;
pub fn eat_at_restaurant() {
hosting::add_to_waitlist();
hosting::add_to_waitlist();
hosting::add_to_waitlist();
}
Creating Idiomatic use Paths
Bringing the
add_to_waitlist
function into scope with use
, which is unidiomaticmod front_of_house {
pub mod hosting {
pub fn add_to_waitlist() {}
}
}
use crate::front_of_house::hosting::add_to_waitlist;
pub fn eat_at_restaurant() {
add_to_waitlist();
add_to_waitlist();
add_to_waitlist();
}
pub mod hosting {
pub fn add_to_waitlist() {}
}
}
use crate::front_of_house::hosting::add_to_waitlist;
pub fn eat_at_restaurant() {
add_to_waitlist();
add_to_waitlist();
add_to_waitlist();
}
Bringing
HashMap
into scope in an idiomatic wayuse std::collections::HashMap;
fn main() {
let mut map = HashMap::new();
map.insert(1, 2);
}
fn main() {
let mut map = HashMap::new();
map.insert(1, 2);
}
Bringing two types with the same name into the same scope requires using their parent modules.
use std::fmt;
use std::io;
fn function1() -> fmt::Result {
// --snip--
}
fn function2() -> io::Result<()> {
// --snip--
}
use std::io;
fn function1() -> fmt::Result {
// --snip--
}
fn function2() -> io::Result<()> {
// --snip--
}
As you can see, using the parent modules distinguishes the two
Result
types. If instead we specified use std::fmt::Result
and use std::io::Result
, we’d have two Result
types in the same scope and Rust wouldn’t know which one we meant when we used Result
.Providing New Names with the as Keyword
use std::fmt::Result;
use std::io::Result as IoResult;
fn function1() -> Result {
// --snip--
}
fn function2() -> IoResult<()> {
// --snip--
}
Re-exporting Names with pub use
When we bring a name into scope with the
use
keyword, the name available in the new scope is private. To enable the code that calls our code to refer to that name as if it had been defined in that code’s scope, we can combine pub
and use
. This technique is called re-exporting because we’re bringing an item into scope but also making that item available for others to bring into their scopemod front_of_house {
pub mod hosting {
pub fn add_to_waitlist() {}
}
}
pub use crate::front_of_house::hosting;
pub fn eat_at_restaurant() {
hosting::add_to_waitlist();
hosting::add_to_waitlist();
hosting::add_to_waitlist();
}
pub mod hosting {
pub fn add_to_waitlist() {}
}
}
pub use crate::front_of_house::hosting;
pub fn eat_at_restaurant() {
hosting::add_to_waitlist();
hosting::add_to_waitlist();
hosting::add_to_waitlist();
}
Using External Packages
[dependencies]
rand = "0.5.5"
rand = "0.5.5"
use rand::Rng;
fn main() {
let secret_number = rand::thread_rng().gen_range(1, 101);
}
fn main() {
let secret_number = rand::thread_rng().gen_range(1, 101);
}
Using Nested Paths to Clean Up Large use Lists
// --snip--
use std::cmp::Ordering;
use std::io;
// --snip--
use std::cmp::Ordering;
use std::io;
// --snip--
Instead we can use nested paths:
// --snip--
use std::{cmp::Ordering, io};
// --snip--
use std::{cmp::Ordering, io};
// --snip--
Similarly for:
use std::io;
use std::io::Write;
use std::io::{self, Write};
The Glob Operator
use std::collections::*;