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 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 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...
In Rust, there are 3 ways to iterate over a string by character.
Using chars Method
The chars() method returns an iterator over the chars of a string slice. See the following example:
fn main() {
let s = "abc";
for c in s.chars() {
pr...
In Rust, there are 2 ways to access command line parameters.
Using std::env::args Function
This function actually returns the arguments that this program was started with. See the following example:
use std::env;
fn main() {
for arg in env::...
In Rust, there are 3 ways to split a string.
Using split Function
An iterator over substrings of this string slice, separated by characters matched by a pattern. See the following example:
fn main() {
let s = "abc1bcd2e";
let _split = s.sp...
In Rust, there are 3 ways to convert string to json.
Using Serde JSON
Serde is a framework for serializing and deserializing Rust data structures efficiently and generically.
Adding dependencies
In Cargo.toml file we’ll add this information.
[de...
In Rust, variables by default are immutable.Variables Immutable
You can make them mutable by adding mut in front of the variable name. For example,
fn main() {
let x = 2;
println!("value x: {}", x);
x = 4;
println!("value x: {}", x);
}
...
In Rust, "Hello World!" is the first basic program.
Build and Run
In our main.rs, add the following code:
// main.rs
fn main() {
println!("Hello,世界,こんにちは,안녕하세요")
}
Hello,世界,こんにちは,안녕하세요
we can run our application by typing:
B...
In Rust, there are two ways to install rust in Linux.Running the following in your terminal
If you’re running macOS, Linux, or another Unix-like OS. To download Rustup and install Rust, run the following in your terminal, then follow the on-screen i...