Primitive Types
Rust has a couple of basic types that are directly implemented into the compiler. In this section, we’ll go through the most important ones.
Further information
Rustlings
primitive_types1: Fill in the rest of the line that has code missing!
// primitive_types1.rs // Fill in the rest of the line that has code missing! // No hints, there's no tricks, just get used to typing these :) // I AM NOT DONE fn main() { // Booleans (`bool`) let is_morning = true; if is_morning { println!("Good morning!"); } let // Finish the rest of this line like the example! Or make it be false! if is_evening { println!("Good evening!"); } }
primitive_types2: Fill in the rest of the line that has code missing!
// primitive_types2.rs // Fill in the rest of the line that has code missing! // No hints, there's no tricks, just get used to typing these :) // I AM NOT DONE fn main() { // Characters (`char`) // Note the _single_ quotes, these are different from the double quotes // you've been seeing around. let my_first_initial = 'C'; if my_first_initial.is_alphabetic() { println!("Alphabetical!"); } else if my_first_initial.is_numeric() { println!("Numerical!"); } else { println!("Neither alphabetic nor numeric!"); } let // Finish this line like the example! What's your favorite character? // Try a letter, try a number, try a special character, try a character // from a different language than your own, try an emoji! if your_character.is_alphabetic() { println!("Alphabetical!"); } else if your_character.is_numeric() { println!("Numerical!"); } else { println!("Neither alphabetic nor numeric!"); } }
primitive_types3: Create an array with at least 100 elements in it where the ??? is.
// primitive_types3.rs // Create an array with at least 100 elements in it where the ??? is. // Execute `rustlings hint primitive_types3` or use the `hint` watch subcommand for a hint. // I AM NOT DONE fn main() { let a = ??? if a.len() >= 100 { println!("Wow, that's a big array!"); } else { println!("Meh, I eat arrays like that for breakfast."); } }
Hint
There’s a shorthand to initialize Arrays with a certain size that does not require you to type in 100 items (but you certainly can if you want!). For example, you can do: let array = [“Are we there yet?”; 10];
Bonus: what are some other things you could have that would return true
for a.len() >= 100?
solution
// primitive_types3.rs // Create an array with at least 100 elements in it where the ??? is. // Execute `rustlings hint primitive_types3` or use the `hint` watch subcommand for a hint. fn main() { let a = ["a"; 110]; if a.len() >= 100 { println!("Wow, that's a big array!"); } else { println!("Meh, I eat arrays like that for breakfast."); } }
primitive_types4: Get a slice out of Array a
// primitive_types4.rs // Get a slice out of Array a where the ??? is so that the test passes. // Execute `rustlings hint primitive_types4` or use the `hint` watch subcommand for a hint. // I AM NOT DONE fn main() { let a = [1, 2, 3, 4, 5]; let nice_slice = ??? assert_eq!([2, 3, 4], nice_slice) }
primitive_types5: Destructure the cat tuple
// primitive_types5.rs // Destructure the `cat` tuple so that the println will work. // Execute `rustlings hint primitive_types5` or use the `hint` watch subcommand for a hint. // I AM NOT DONE fn main() { let cat = ("Furry McFurson", 3.5); let /* your pattern here */ = cat; println!("{} is {} years old.", name, age); }
primitive_types6: Use a tuple index to access the second element of numbers.
// primitive_types6.rs // Use a tuple index to access the second element of `numbers`. // You can put the expression for the second element where ??? is so that the test passes. // Execute `rustlings hint primitive_types6` or use the `hint` watch subcommand for a hint. // I AM NOT DONE #[test] fn indexing_tuple() { let numbers = (1, 2, 3); // Replace below ??? with the tuple indexing syntax. let second = ???; assert_eq!(2, second, "This is not the 2nd number in the tuple!") }