All Go Rust Python PHP JavaScript
Chrome Dev Summit to secure your spot in workshops, office hours and learning lounges!

How to sort a vector in Rust

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, ...
Tomoki

How to Put the Current Thread to Sleep in Rust

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 ...
Sambhav Khandelwal

How to Convert a String to int in Rust

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!("{}"...
Tomoki

How to Check if a String Contains Whitespace in Rust

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() { ...
Unused

How to Reverse a String in Rust

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>());...
pooriabt

How to Convert Float to Integer in Rust

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...
pooriabt

How to Remove an Element from a Vector in Rust

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 ...
aweis

How to Get the String Length in Characters in Rust

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!("{}...
Tomoki

How to List Files of a Directory in Rust

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...
ada

How to Raise a Number to a Power in Rust

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...
ada

How to Check Whether a Path Exists in Rust

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+ ...
aweis

How to Find the Index of an Element in a Vector in Rust

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 ...
pooriabt

How to Convert a Str to a &[u8] in Rust

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...
aweis

How to Index a String in Rust

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...
pooriabt

How to Iterate through the Values of an Enum in Rust

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...
pooriabt