This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let s = String::from("Hello world"); | |
| let hello = &s[0..5]; | |
| let world = &s[6..11]; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // We first pass a string by refernce | |
| // indicating we will be returning a value of standard size | |
| fn first_word(s: &String) -> usize { | |
| // convert our string to bytes | |
| let bytes = s.as_bytes(); | |
| // when we enumerate, we get both our item, and an index | |
| // iterate over the bytes, and enumerate |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fn main() { | |
| let mut s = String::from("hello"); | |
| change(&mut s); | |
| } | |
| fn change(mystring: &mut String) { | |
| mystring.push_str(", world"); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fn main () { | |
| let mut s = String::from("hello"); | |
| s.push_str(", world"); // appeand a literal to the existing string s | |
| println!("{}", s); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| let s = "string"; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fn main() { | |
| // working through a predefined array | |
| let a = [10, 20, 30, 40, 50]; | |
| for element in a { | |
| println!("The value is {element}"); | |
| } | |
| // generating our own data starting from 1 and ending in 4 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fn main() { | |
| let mut number = 3; | |
| while number != 0 { | |
| println!("{number}!"); | |
| number -= 1; | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fn main() { | |
| // We declare the variable as mutable to be able to change it | |
| let mut counter = 0; | |
| // We can declare a loop within a variable | |
| let result = loop { | |
| // increment our mutable value | |
| counter += 1; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fn main() { | |
| let number = 3; | |
| if number < 5 { | |
| println!("Smaller than 5"); | |
| } else if number > 8 { | |
| println!("Greater than 8"); | |
| } else { | |
| println!("Between 5 and 8"); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fn main() { | |
| let x: i32 = 5; | |
| let y = add_five(x); | |
| println!("The value of y is: {y}"); | |
| } | |
| fn add_five(a: i32) -> i32 { |