Rust is a cutting-edge systems programming language that has become quite popular in recent years because of its emphasis on concurrency, efficiency, and safety. It is renowned for its capacity to manage tasks that other high-level programming languages can't as well as its low-level control over system resources. Rust offers the ideal compromise between security and speed because of its robust type system and zero-cost abstractions.
We'll look at the basics of the Rust programming language `methods` in this post, regardless of the experience you'll be using all the time.
Before we start
If you're new to new Rust, I recommend this
You use the Rust Docs or use this Ebook
So these are all the lists we'll discuss here.
- println!()
- format!()
- unwrap()
- expect()
- ok_or()
- Ok()
- iter()
- map()
- map_or()
- unwrap_or()
- loop{}
- match {}
- for {}
- into_iter()
- clone()
- to_owned()
- to_string()
- len()
- pop()
- remove()
- is_empty()
- as_ref()
- as_mut()
println!()
In my previous post, I talked about the print macro in Rust and the other types of print macros available in Rust. Here is a link to it if you want to check it out.
The common knowledge you need to understand and know is that the `print!` is used to output a text to the standard output.
Standard output `stdout` is used by an operating system to output text data from a program.
The `!` after the `print` tells us that it is a macro, not a function. Now what `print!` does is, it prints an output without inserting a new line. To print the output on a new line, we include `ln` between the t and!
The different ways to output text data:
1. `print!()` 2. `println!()` 3. `eprint!()` 4. `eprintln!()`
fn main() {
print!("{}", "Hello World") // adds a new line then output
println!("{}", "Hello World") // doesn't add a new line then output
eprint!("{}", "Hello World") // doesn't add a new line then output
eprintln!("{}", "Hello World") // add a new line then output
}
format!()
The format! macro in Rust, returns a formatted string as a value, instead of printing the result to the Standard output like the print! macro does.
So what does it mean?
So let's take the `println!` I talked about earlier, what the `println!` does is print to the standard output, but the `format!` macro returns the string that you pass to it, which makes it possible for you to pass in dynamic data.
For this example:
#![allow(unused)]
fn main() {
// Type is String
// Therefore it can be borrowed.
let is_male: String = format!("This is the boy"); // return a string or value pass through it
let string_: String = format!("Hello World");
println!("{}", string_)
// output is: "Hello World"
}
So what you're seeing is that `string_` stores a value which returns by the `format!` macro, and then we are printing it out using the `println!` macro, note that, the `format!` is a type of `String.` You can try it here
So why will we use the `format!`? it is useful for cases where you need to construct a string with dynamic content, such as when you want to return a string from a function or create a string that you will use later in your program.
Use case:
fn main(){
let user_name = "Rocky Essel";
let user_height = "170cm";
let user_age = 23;
let user_info = format!("The name of the user is {}, and he has a height of {}, and finally he is {} years old. So keep him!", user_name, user_height, user_age);
println!("{}",user_info)
// Output is: The name of the user is Rocky Essel, and he has a height of 170cm, and finally he is 23 years old. So keep him!
}
So I hope you're clear with this example. You can try it here