In Rust, there are 2 ways to sort a vector.
Using sort Method
The sort(&mut self) method sorts the slice. This sort is stable (i.e., does not reorder equal elements) and O(n * log(n)) worst-case. For example,
fn main() {
let mut v = [3, -5, ...
In Rust, using the thread::sleep_ms method is the easiest way to put the current thread to sleep.
Using sleep_ms Method
The thread::sleep_ms() method puts the current thread to sleep for at least the specified amount of time. For example,
use ...
In Rust, using the parse method is the easiest way to convert a string to int.
Using parse Method
The str::parse::<T>() method parses this string slice into another type. For example,
fn main() {
let s = "100".to_string();
println!("{}"...
In Rust, there are 2 ways to check if a string contains whitespace.
Using char::is_whitespace
char::is_whitespace returns true if the character has the Unicode White_Space property.
You can pass char::is_whitespace to .contains():
fn main() {
...
In Rust, there are 2 ways to reverse a string.
Using rev Function
The str::rev() function reverses an iterator’s direction.
See the following example:
fn main() {
let mut s = "Hello";
println!("{}", s.chars().rev().collect::<String>());...
In Rust, there are 2 ways to convert float to integer.
Using as Keyword
Executing an as expression casts the value on the left-hand side to the type on the right-hand side.
An example of an as expression:
fn main() {
let a = 3.6415_f64;
l...
In Rust, there are 2 ways to remove an element from a vector.
Using retain Function
The vec::retain() function retains only the elements specified by the predicate.
For example,
fn main() {
let mut v = vec![1, 3, 2, 3, 4];
v.retain(|&x| x ...
In Rust, there are 2 ways to get the string length in characters.
Using chars Function
The str::chars() function returns an iterator over the chars of a string slice.
See the following example:
fn main() {
let mut s = "Hello";
println!("{}...
In Rust, using the read_dir function is the easiest way to list files of a directory.
Using read_dir Function
The fs::read_dir() function returns an iterator over the entries within a directory.
See the following example:
use std::fs;
fn main...
In Rust, there are 2 ways to raise a number to a power.
Using pow Method
The pow() function raises a value to the power of exp, using exponentiation by squaring. Note that 0⁰ (pow(0, 0)) returns 1. Mathematically this is undefined.
See the followi...
In Rust, using the Path::exists function is the easiest way to check whether a path exists.
Using Path::exists Function
The Path::exists() function returns true if the path points at an existing entity.
See the following example:
// Rust 1.5+
...
In Rust, using the position function is the easiest way to find the index of an element in a vector.
Using position Method
The position() method searches for an element in an iterator, returning its index.
position() is short-circuiting; in other ...
In Rust, there are 2 ways to convert a str to a &[u8].
Using as_bytes Function
The as_bytes() method converts a string slice to a byte slice. To convert the byte slice back into a string slice, use the from_utf8 function.
See the following example...
In Rust, there are 2 ways to index a string.
Using as_bytes Function
If the enum is C-like, then you can create a static array of each of the variants and return an iterator of references to them:
fn main() {
let s = "abc";
let b: u8 = s.as...
In Rust, there are 2 ways to iterate through the values of an enum.
Using a static array
If the enum is C-like, then you can create a static array of each of the variants and return an iterator of references to them:
use self::Direction::*;
use...