add solutions to exercise primitives_types

This commit is contained in:
Eri Mendz 2021-05-25 16:52:12 +03:00
parent 1d186e5644
commit 871de4d720
6 changed files with 28 additions and 12 deletions

View file

@ -2,7 +2,7 @@
// 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
// may 25/21 DONE
fn main() {
// Booleans (`bool`)
@ -12,7 +12,7 @@ fn main() {
println!("Good morning!");
}
let // Finish the rest of this line like the example! Or make it be false!
let is_evening = false;
if is_evening {
println!("Good evening!");
}

View file

@ -2,7 +2,7 @@
// 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
// done may 25/21
fn main() {
// Characters (`char`)
@ -16,7 +16,8 @@ fn main() {
println!("Neither alphabetic nor numeric!");
}
let // Finish this line like the example! What's your favorite character?
//let // Finish this line like the example! What's your favorite character?
let your_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() {

View file

@ -2,14 +2,27 @@
// Create an array with at least 100 elements in it where the ??? is.
// Execute `rustlings hint primitive_types3` for hints!
// I AM NOT DONE
// may 25/21 DONE
fn main() {
let a = ???
// create array of type i32 having 120 elements
let a: [i32; 120] = [0; 120];
if a.len() >= 100 {
println!("Wow, that's a big array!");
//println!("the number of elements in array: {}", a.len());
//make function to borrow whole array as slice
analyze_slice(&a);
//slice can point to a section of array
println!("borrow a section of the array as a slice");
analyze_slice(&a[0 .. 10]);
} else {
println!("Meh, I eat arrays like that for breakfast.");
}
}
// this function borrows a slice
fn analyze_slice(slice: &[i32]) {
println!("slice first element: {}", slice[0]);
println!("borrowed slice has {} elements", slice.len());
}

View file

@ -2,13 +2,14 @@
// Get a slice out of Array a where the ??? is so that the test passes.
// Execute `rustlings hint primitive_types4` for hints!!
// I AM NOT DONE
// May 25/21 DONE
#[test]
fn slice_out_of_array() {
let a = [1, 2, 3, 4, 5];
let nice_slice = ???
let nice_slice = &a[1 .. 4];
//println!("value is {:#?}", nice_slice);
assert_eq!([2, 3, 4], nice_slice)
}

View file

@ -2,11 +2,12 @@
// Destructure the `cat` tuple so that the println will work.
// Execute `rustlings hint primitive_types5` for hints!
// I AM NOT DONE
// may 25/21 DONE
fn main() {
let cat = ("Furry McFurson", 3.5);
let /* your pattern here */ = cat;
//let /* your pattern here */ = cat;
let (name, age) = cat;
println!("{} is {} years old.", name, age);
}

View file

@ -3,13 +3,13 @@
// You can put the expression for the second element where ??? is so that the test passes.
// Execute `rustlings hint primitive_types6` for hints!
// I AM NOT DONE
// may 25/21 DONE
#[test]
fn indexing_tuple() {
let numbers = (1, 2, 3);
// Replace below ??? with the tuple indexing syntax.
let second = ???;
let second = numbers.1;
assert_eq!(2, second,
"This is not the 2nd number in the tuple!")