mirror of
https://github.com/Fechin/reference.git
synced 2026-11-20 21:23:11 +03:00
Reformat rust cheatsheet
This commit is contained in:
1 parent
94dcb12e37
commit
fed01d13d9
1 file changed
+390
-427
@@ -117,13 +117,14 @@ See: [Comment](https://doc.rust-lang.org/reference/comments.html)
|
||||
|
||||
```rust
|
||||
fn test(){
|
||||
println!("This is a function!");
|
||||
println!("This is a function!");
|
||||
}
|
||||
|
||||
fn main(){
|
||||
test();
|
||||
}
|
||||
```
|
||||
See: [Functions](#rust-functions)
|
||||
|
||||
|
||||
|
||||
@@ -178,8 +179,8 @@ let community_name = "AXIAL";
|
||||
let no_of_members: &str = "ten";
|
||||
|
||||
println!("The name of the community is {} and it has {} members", community_name, no_of_members);
|
||||
```
|
||||
|
||||
```
|
||||
See: [Strings](#rust-strings)
|
||||
|
||||
|
||||
### Arrays
|
||||
@@ -198,7 +199,7 @@ let array: [i64; 6] = [92,97,98,99,98,94];
|
||||
|
||||
|
||||
|
||||
### Multi-Dimensional Array
|
||||
### Multi-Dimensional Array {.row-span-2}
|
||||
|
||||
```rust
|
||||
j0 j1 j2 j3 j4 j5
|
||||
@@ -253,6 +254,72 @@ let tuple = (1, 'A' , "Cool", 78, true);
|
||||
```
|
||||
|
||||
|
||||
|
||||
Rust Strings {.cols-3}
|
||||
--------------
|
||||
|
||||
### String Literal
|
||||
|
||||
```rust
|
||||
let cs:&str = "cheat sheet";
|
||||
|
||||
// => Share cheat sheet for developers
|
||||
println!("Share {} for developers", cs);
|
||||
```
|
||||
|
||||
|
||||
### String Object
|
||||
|
||||
```rust
|
||||
// Creating an empty string object
|
||||
let my_string = String::new;
|
||||
|
||||
// Converting to a string object
|
||||
let S_string = a_string.to_string()
|
||||
|
||||
// Creating an initialized string object
|
||||
let lang = String::from("Rust");
|
||||
println!("First language is {}", lang);
|
||||
```
|
||||
|
||||
### .capacity()
|
||||
|
||||
```rust
|
||||
let rand = String::from("Random String");
|
||||
rand.capacity() // => 13
|
||||
```
|
||||
Calculates the capacity of the string in bytes.
|
||||
|
||||
|
||||
### .contains()
|
||||
|
||||
```rust
|
||||
let name = String::from("ElementalX");
|
||||
name.contains("Element") // => true
|
||||
```
|
||||
Checks if the substring is contained inside the original string or not.
|
||||
|
||||
|
||||
### Pushing a single character
|
||||
|
||||
```rust
|
||||
let mut half_text = String::from("Hal");
|
||||
half_text.push('f'); // => Half
|
||||
```
|
||||
|
||||
|
||||
### Pushing an entire String
|
||||
|
||||
```rust
|
||||
let mut hi = String::from("Hey there...");
|
||||
hi.push_str("How are you doing??");
|
||||
|
||||
// => Hey there...How are you doing??
|
||||
println!("{}", hi);
|
||||
```
|
||||
|
||||
|
||||
|
||||
Rust Operators {.cols-3}
|
||||
-----------
|
||||
|
||||
@@ -354,29 +421,328 @@ let not = !c; // => false
|
||||
### Compound Assignment Operator
|
||||
|
||||
```rust
|
||||
| Operator | Description |
|
||||
|-----------|-------------------------------------------|
|
||||
| `k += l` | Add a value and assign |
|
||||
| `k -= l` | Substract a value and assign |
|
||||
| `k /= l` | Divide a value and assign |
|
||||
| `k *= l` | Multiply a value and assign |
|
||||
| `k |= l ` | Bitwise OR and assign |
|
||||
let mut k = 9;
|
||||
let mut l = k;
|
||||
```
|
||||
|
||||
----
|
||||
|
||||
| Operator | Description |
|
||||
|-----------|-----------------------------------------|
|
||||
| `k += l` | Add a value and assign, then k=9 |
|
||||
| `k -= l` | Substrate a value and assign, then k=18 |
|
||||
| `k /= l` | Divide a value and assign, then k=9 |
|
||||
| `k *= l` | Multiply a value and assign, then k=81 |
|
||||
| `k |= l` | Bitwise OR and assign, then k=89 |
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Rust Flow Control {.cols-3}
|
||||
--------------
|
||||
|
||||
### If Expression
|
||||
|
||||
```rust
|
||||
let mut k = 9;
|
||||
let mut l = k;
|
||||
println!("{}", l); // => 9
|
||||
k += l;
|
||||
println!("{}", k); // => 18
|
||||
k -= l;
|
||||
println!("{}", k); // => 9
|
||||
k *= l;
|
||||
println!("{}", k); // => 81
|
||||
k |= l;
|
||||
println!("{}", k); // => 89
|
||||
let case1: i32 = 81;
|
||||
let case2: i32 = 82;
|
||||
|
||||
if case1 < case2 {
|
||||
println!("case1 is greater than case2");
|
||||
}
|
||||
```
|
||||
|
||||
### If...Else Expression
|
||||
|
||||
```rust
|
||||
let case3 = 8;
|
||||
let case4 = 9;
|
||||
|
||||
if case3 >= case4 {
|
||||
println!("case3 is better than case4");
|
||||
} else {
|
||||
println!("case4 is greater than case3");
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### If...Else...if...Else Expression
|
||||
|
||||
```rust
|
||||
let foo = 12;
|
||||
let bar = 13;
|
||||
|
||||
if foo == bar {
|
||||
println!("foo is equal to bar");
|
||||
} else if foo < bar {
|
||||
println!("foo less than bar");
|
||||
} else if foo != bar {
|
||||
println!("foo is not equal to bar");
|
||||
} else {
|
||||
println!("Nothing");
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
### If...Let Expression {.row-span-3}
|
||||
|
||||
```rust
|
||||
let mut arr1:[i64 ; 3] = [1,2,3];
|
||||
if let[1,2,_] = arr1{
|
||||
println!("Works with array");
|
||||
}
|
||||
|
||||
let mut arr2:[&str; 2] = ["one", "two"];
|
||||
if let["Apple", _] = arr2{
|
||||
println!("Works with str array too");
|
||||
}
|
||||
```
|
||||
----
|
||||
```rust
|
||||
let tuple_1 = ("India", 7, 90, 90.432);
|
||||
if let(_, 7, 9, 78.99) = tuple_1{
|
||||
println!("Works woth tuples too");
|
||||
}
|
||||
|
||||
let tuple_2 = ( 9, 7, 89, 12, "Okay");
|
||||
if let(9, 7,89, 12, blank) = tuple_2 {
|
||||
println!("Everything {} mate?", blank);
|
||||
}
|
||||
|
||||
let tuple_3 = (89, 90, "Yes");
|
||||
if let(9, 89, "Yes") = tuple_3{
|
||||
println!("Pattern did match");
|
||||
}
|
||||
else {
|
||||
println!("Pattern did not match");
|
||||
}
|
||||
```
|
||||
|
||||
### Match Expression {.row-span-3}
|
||||
|
||||
```rust
|
||||
let day_of_week = 2;
|
||||
match day_of_week {
|
||||
1 => {
|
||||
println!("Its Monday my dudes");
|
||||
},
|
||||
2 => {
|
||||
println!("It's Tuesday my dudes");
|
||||
},
|
||||
3 => {
|
||||
println!("It's Wednesday my dudes");
|
||||
},
|
||||
4 => {
|
||||
println!("It's Thursday my dudes");
|
||||
},
|
||||
5 => {
|
||||
println!("It's Friday my dudes");
|
||||
},
|
||||
6 => {
|
||||
println!("It's Saturday my dudes");
|
||||
},
|
||||
7 => {
|
||||
println!("It's Sunday my dudes");
|
||||
},
|
||||
_ => {
|
||||
println!("Default!")
|
||||
}
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
### Nested...If Expression
|
||||
|
||||
```rust
|
||||
let nested_conditions = 89;
|
||||
if nested_conditions == 89 {
|
||||
let just_a_value = 98;
|
||||
if just_a_value >= 97 {
|
||||
println!("Greater than 97");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
### For Loop
|
||||
|
||||
```rust
|
||||
for mut i in 0..15 {
|
||||
i-=1;
|
||||
println!("The value of i is : {}", i);
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### While Loop
|
||||
|
||||
```rust
|
||||
let mut check = 0;
|
||||
while check < 11{
|
||||
println!("Check is : {}", check);
|
||||
check+=1;
|
||||
println!("After incrementing: {}", check);
|
||||
|
||||
if check == 10{
|
||||
break; // stop while
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
### Loop keyword
|
||||
|
||||
```rust
|
||||
loop {
|
||||
println!("hello world forever!");
|
||||
}
|
||||
```
|
||||
The infinite loop indicated.
|
||||
|
||||
### Break Statement
|
||||
|
||||
```rust
|
||||
let mut i = 1;
|
||||
loop {
|
||||
println!("i is {}", i);
|
||||
if i > 100 {
|
||||
break;
|
||||
}
|
||||
i *= 2;
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
### Continue Statement
|
||||
|
||||
```rust
|
||||
for (v, c) in (0..10+1).enumerate(){
|
||||
println!("The {} number loop", c);
|
||||
if v == 9{
|
||||
println!("Here we go continue?");
|
||||
continue;
|
||||
}
|
||||
println!{"The value of v is : {}", v};
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
Rust Functions {.cols-3}
|
||||
--------------
|
||||
|
||||
|
||||
### Basic function
|
||||
|
||||
```rust
|
||||
|
||||
fn print_message(){
|
||||
println!("Hello, QuickRef.ME!");
|
||||
}
|
||||
|
||||
fn main(){
|
||||
//Invoking a function in Rust.
|
||||
print_message();
|
||||
}
|
||||
```
|
||||
|
||||
### Pass by Value
|
||||
|
||||
```rust
|
||||
fn main()
|
||||
{
|
||||
let x:u32 = 10;
|
||||
let y:u32 = 20;
|
||||
|
||||
// => 200
|
||||
println!("Calc: {}", cal_rect(x, y));
|
||||
}
|
||||
fn cal_rect(x:u32, y:u32) -> u32
|
||||
{
|
||||
x * y
|
||||
}
|
||||
```
|
||||
|
||||
### Pass by Reference
|
||||
|
||||
```rust
|
||||
fn main(){
|
||||
let mut by_ref = 3; // => 3
|
||||
power_of_three(&mut by_ref);
|
||||
println!("{}", by_ref); // => 9
|
||||
}
|
||||
|
||||
fn power_of_three(by_ref: &mut i32){
|
||||
// de-referencing is important
|
||||
*by_ref = *by_ref * *by_ref;
|
||||
println!("{}", by_ref); // => 9
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### Returns
|
||||
|
||||
```rust {.wrap}
|
||||
fn main(){
|
||||
let (mut radius, mut pi) = (3.0, 3.14);
|
||||
let(area, _perimeter) = calculate (
|
||||
&mut radius,
|
||||
&mut pi
|
||||
);
|
||||
println!("The area and the perimeter of the circle are: {} & {}", area, _perimeter);
|
||||
}
|
||||
|
||||
fn calculate(radius : &mut f64, pi: &mut f64) -> (f64, f64){
|
||||
let perimeter = 2.0 * *pi * *radius;
|
||||
let area = *pi * *radius * *radius;
|
||||
return (area, perimeter);
|
||||
}
|
||||
```
|
||||
|
||||
### Arrays as Arguments
|
||||
|
||||
```rust
|
||||
fn main(){
|
||||
let mut array: [i32 ; 5] = [1,2,3,4,6];
|
||||
print_arrays(array);
|
||||
println!("The elements: {:?}", array);
|
||||
}
|
||||
|
||||
fn print_arrays(mut array:[i32; 5]) {
|
||||
array[0] = 89;
|
||||
array[1] = 90;
|
||||
array[2] = 91;
|
||||
array[3] = 92;
|
||||
array[4] = 93;
|
||||
println!("The elements: {:?}", array);
|
||||
}
|
||||
```
|
||||
|
||||
### Returning Arrays
|
||||
|
||||
```rust
|
||||
|
||||
fn main(){
|
||||
let mut arr:[i32; 5] = [2,4,6,8,10];
|
||||
multiply(arr);
|
||||
println!("The array is : {:?}", multiply(arr));
|
||||
}
|
||||
|
||||
fn multiply (mut arr: [i32 ; 5]) -> [i32 ; 5]{
|
||||
arr[2] = 90;
|
||||
for mut i in 0..5 {
|
||||
arr[i] = arr[i] * arr[2];
|
||||
}
|
||||
return multiply;
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
Misc {.cols-3}
|
||||
@@ -402,7 +768,7 @@ To perform type-casting in Rust one must use the `as` keyword.
|
||||
|
||||
|
||||
|
||||
### Borrowing
|
||||
### Borrowing
|
||||
|
||||
```rust
|
||||
let mut foo = 4;
|
||||
@@ -420,7 +786,7 @@ Here borrowed value borrows the value from value one using `&` operator.
|
||||
|
||||
|
||||
|
||||
### De-referencing
|
||||
### De-referencing
|
||||
|
||||
```rust
|
||||
let mut borrow = 10;
|
||||
@@ -443,411 +809,8 @@ println!("{}", a_number);
|
||||
This will produce error as the scope of the variable `a_number` ends at the braces
|
||||
|
||||
|
||||
|
||||
Flow Control {.cols-3}
|
||||
--------------
|
||||
|
||||
### If Expression
|
||||
|
||||
```rust
|
||||
let case1: i32 = 81;
|
||||
let case2: i32 = 82;
|
||||
|
||||
if case1 < case2 {
|
||||
println!("Case 1 is greater than Case 2 "); // This statement will be printed as the comparision evaluates to true.
|
||||
}
|
||||
```
|
||||
|
||||
### If..Else Expression
|
||||
|
||||
```rust
|
||||
let case3 = 8;
|
||||
let case4 = 9;
|
||||
|
||||
if case3 >= case4 {
|
||||
println!("Case 3 is better than Case 4 ");
|
||||
} else {
|
||||
println!("Case 4 is greater than Case 3"); // This statement will be executed because the statement inside the 'if' block is not true.
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### If..Else..if..Else Expression
|
||||
|
||||
```rust
|
||||
let variable_one = 12;
|
||||
let variable_two = 13;
|
||||
|
||||
if variable_one == variable_two {
|
||||
println!("Variable one is equal to Variable 2");
|
||||
} else if variable_one < variable_two {
|
||||
println!("Variable one less than Variable two "); // This statement will be printed.
|
||||
} else if variable_one != variable_two {
|
||||
println!("Variable one is not equal to variable two");
|
||||
} else {
|
||||
println!("Nothing");
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### Nested..If Expression
|
||||
|
||||
```rust
|
||||
let nested_conditions = 89;
|
||||
if nested_conditions == 89{ // This condition evaluates to true, control of the program is passed inside the if block.
|
||||
|
||||
let just_a_value = 98;
|
||||
if just_a_value >= 97{
|
||||
println!("The value is greater than 97"); // This statement will be printed, as the condition beside nested if statement evaluates to true.
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### If..Let Expression
|
||||
|
||||
```rust
|
||||
let mut new_array:[i64 ; 3] = [1,2,3];
|
||||
if let[1,2,_] = new_array{
|
||||
println!(" Works with array.");
|
||||
}
|
||||
|
||||
let mut string_array:[&str; 2] = ["Apple", "Guava"];
|
||||
if let["Apple", _] = string_array{
|
||||
println!("Works with string array too!");
|
||||
}
|
||||
|
||||
let new_tuple = ("India", 7, 90, 90.432);
|
||||
if let(_, 7, 9, 78.99) = new_tuple{
|
||||
println!("Works woth tuples too");
|
||||
}
|
||||
|
||||
let second_new_tuple = ( 9, 7, 89, 12, "Okay");
|
||||
if let(9, 7,89, 12, blank) = second_new_tuple{
|
||||
println!("Is everything {} mate?", blank);
|
||||
}
|
||||
|
||||
let third_new_tuple = (89, 90, "Yes");
|
||||
if let(9, 89, "Yes") = third_new_tuple{
|
||||
|
||||
println!("Pattern did match");
|
||||
}
|
||||
else {
|
||||
|
||||
println!("Pattern did not match");
|
||||
}
|
||||
```
|
||||
|
||||
### Match Expression
|
||||
|
||||
```rust
|
||||
let day_of_week = 2;
|
||||
|
||||
match day_of_week {
|
||||
1 => {
|
||||
println!("Its Monday my dudes");
|
||||
},
|
||||
2 => {
|
||||
println!("It's Tuesday my dudes");
|
||||
},
|
||||
3 => {
|
||||
println!("It's Wednesday my dudes");
|
||||
},
|
||||
4 => {
|
||||
println!("It's Thursday my dudes");
|
||||
},
|
||||
5 => {
|
||||
println!("It's Friday my dudes");
|
||||
},
|
||||
6 => {
|
||||
println!("It's Saturday my dudes");
|
||||
},
|
||||
7 => {
|
||||
println!("It's Sunday my dudes");
|
||||
},
|
||||
_ => {
|
||||
println!("Default!")
|
||||
}
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
### For Loop
|
||||
|
||||
```rust
|
||||
for mut i in 0..15{
|
||||
|
||||
i-=1;
|
||||
println!("The value of i is : {}", i);
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### While Loop
|
||||
|
||||
```rust
|
||||
let mut check = 0;
|
||||
while check < 11{
|
||||
|
||||
println!("The value of check is : {}", check);
|
||||
check+=1;
|
||||
println!("The value of check after incrementing is : {}", check);
|
||||
|
||||
if check == 10{
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
### Loop
|
||||
|
||||
```rust
|
||||
let mut random_value = 5;
|
||||
|
||||
loop {
|
||||
|
||||
random_value++;
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
### Break Statement
|
||||
|
||||
```rust
|
||||
|
||||
let mut some_random_val = 90;
|
||||
loop {
|
||||
|
||||
some_random_val+=1;
|
||||
println!("The value of some random value is {}", some_random_val);
|
||||
if some_random_val == 100{
|
||||
break;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
### Continue Statement
|
||||
|
||||
|
||||
```rust
|
||||
for (some_var, count) in (0..10+1).enumerate(){
|
||||
println!("This is the {} number loop", count);
|
||||
if some_var == 9{
|
||||
println!("Here we go continue statement?");
|
||||
continue;
|
||||
}
|
||||
println!{"The value of some_var is : {}", some_var};
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Functions {.cols-3}
|
||||
--------------
|
||||
|
||||
|
||||
### Functions - Basic Syntax
|
||||
|
||||
```rust
|
||||
|
||||
fn print_message(){
|
||||
|
||||
println!("Hello, World!");
|
||||
|
||||
}
|
||||
|
||||
fn main(){
|
||||
|
||||
print_message(); //Invoking a function in Rust.
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
### Functions - Pass by Value
|
||||
|
||||
```rust
|
||||
|
||||
fn main(){
|
||||
|
||||
let pass_by_value = 1;
|
||||
println!("The value of pass by value variable is : {}", pass_by_value) ;
|
||||
multiplywith2(pass_by_value);
|
||||
println!("The value of pass by value is still : {}", pass_by_value);
|
||||
|
||||
}
|
||||
|
||||
fn multiplywith2(pass_by_value:i32) {
|
||||
|
||||
let value = println!("The value of pass by value after getting a new function invoked is : {}", 2 * pass_by_value);
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
### Functions - Pass by Reference
|
||||
|
||||
```rust
|
||||
fn main(){
|
||||
|
||||
let mut pass_by_reference = 3;
|
||||
println!("The value of pass by reference variable is : {}", pass_by_reference);
|
||||
power_of_three( &mut pass_by_reference);
|
||||
println!("The modified value is now : {}", pass_by_reference);
|
||||
|
||||
|
||||
}
|
||||
|
||||
fn power_of_three(pass_by_reference: &mut i32){
|
||||
|
||||
*pass_by_reference = *pass_by_reference * *pass_by_reference; //de-referencing is important
|
||||
println!("The value of the variable after invoking pass by reference function is : {}", pass_by_reference);
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### Functions - Returns
|
||||
|
||||
```rust
|
||||
fn main(){
|
||||
|
||||
let (mut radius , mut pi) = (3.0, 3.14);
|
||||
let(area, _perimeter) = calculate_area_perimeter(
|
||||
&mut radius,
|
||||
&mut pi
|
||||
);
|
||||
println!("The area and the perimeter of the circle are : {} & {}",area,_perimeter);
|
||||
|
||||
}
|
||||
|
||||
fn calculate_area_perimeter(radius : &mut f64, pi: &mut f64) -> (f64, f64){
|
||||
|
||||
let perimeter = 2.0 * *pi * *radius;
|
||||
let area = *pi * *radius * *radius;
|
||||
return (area, perimeter);
|
||||
}
|
||||
```
|
||||
|
||||
### Functions - Arrays as Arguments
|
||||
|
||||
```rust
|
||||
fn main(){
|
||||
|
||||
let mut array: [i32 ; 5] = [1,2,3,4,6];
|
||||
print_arrays(array);
|
||||
println!("The elements of the array are : {:?}", array);
|
||||
|
||||
}
|
||||
|
||||
fn print_arrays(mut array:[i32; 5]) {
|
||||
|
||||
array[0] = 89;
|
||||
array[1] = 90;
|
||||
array[2] = 91;
|
||||
array[3] = 92;
|
||||
array[4] = 93;
|
||||
|
||||
println!("The elements of the array are : {:?}", array);
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
### Function - Returning Arrays
|
||||
|
||||
```rust
|
||||
|
||||
fn main(){
|
||||
|
||||
let mut multiply:[i32; 5] = [ 2,4,6,8,10];
|
||||
multiply_all(multiply);
|
||||
println!("The array is : {:?}", multiply_all(multiply));
|
||||
}
|
||||
|
||||
fn multiply_all(mut multiply: [i32 ; 5]) -> [i32 ; 5]{
|
||||
|
||||
multiply[2] = 90;
|
||||
for mut i in 0..5{
|
||||
|
||||
multiply[i] = multiply[i] * multiply[2];
|
||||
}
|
||||
return multiply;
|
||||
|
||||
```
|
||||
|
||||
|
||||
Strings {.cols-3}
|
||||
--------------
|
||||
|
||||
### String Literal
|
||||
|
||||
```rust
|
||||
let safe_programming_language:&str = "Rust";
|
||||
println!("An example of safe programming language is : {}", safe_programming_language);
|
||||
```
|
||||
|
||||
|
||||
### String Object
|
||||
|
||||
```rust
|
||||
|
||||
let other_safe_programminglanguage = String::new; // Creating an empty string object
|
||||
let S_language = other_safe_programminglanguage.to_string() // Converting a string literal to a string object
|
||||
let language_one = String::from("Rust"); // Creating an initialized string object
|
||||
println!("The first language is {}", language_one);
|
||||
```
|
||||
|
||||
### String Functions - .capacity()
|
||||
|
||||
```rust
|
||||
let test_capacity = String::from("Random String");
|
||||
println!("The capacity of the variable test_capacity is : {}", test_capacity.capacity()); // Calculates the capacity of the string in bytes.
|
||||
```
|
||||
|
||||
|
||||
### String Functions - .contains()
|
||||
|
||||
```rust
|
||||
let name = String::from("ElementalX");
|
||||
let check = name.contains("Element");
|
||||
println!("Does the word Element contain in the original string : {}", check); // Checks if the substring is contained inside the original string or not.
|
||||
```
|
||||
|
||||
|
||||
### Updating a String - Pushing a single character
|
||||
|
||||
```rust
|
||||
let mut half_text = String::from("Hal");
|
||||
half_text.push('f');
|
||||
println!("The updated value of the variable is : {}", half_text);
|
||||
```
|
||||
|
||||
|
||||
### Updating a String - Pushing an entire String
|
||||
|
||||
```rust
|
||||
|
||||
let mut half_message = String::from("Hey there...");
|
||||
half_message.push_str("How are you doing??");
|
||||
println!("{}", half_message);
|
||||
```
|
||||
|
||||
### Strings and functions
|
||||
|
||||
```rust
|
||||
let mut param = "Just some text".to_string();
|
||||
test_print(&mut param);
|
||||
println!("The updated value of param is : {}", param);
|
||||
```
|
||||
|
||||
|
||||
Also see
|
||||
--------
|
||||
|
||||
- [The Rust Document](https://doc.rust-lang.org/book/ch00-00-introduction.html) _(doc.rust-lang.org)_
|
||||
- [The Rust Reference](https://doc.rust-lang.org/reference/introduction.html) _(doc.rust-lang.org)_
|
||||
|
||||
|
||||
Reference in new issue
Block a user