Skip to content

Commit 4cf0288

Browse files
add functions, prim, unit and tuple challenges solutions
1 parent bb4e324 commit 4cf0288

File tree

6 files changed

+46
-0
lines changed

6 files changed

+46
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub fn numerical_type_conversion(n: i32) -> u32 {
2+
n as u32
3+
}
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
pub fn add(a: i32, b: i32) -> i32 {
2+
// Step 1: implement addition
3+
a + b
4+
}
5+
6+
// Step 2:
7+
// Define a public function named `subtract`
8+
// that accepts two arguments of type `i32`
9+
// and returns an `i32`.
10+
// make sure you make the function public by using the `pub` keyword.
11+
pub fn subtract(a: i32, b: i32) -> i32 {
12+
a - b
13+
}
14+
15+
// Step 3:
16+
// Define a public function named `multiply`
17+
// that accepts two arguments of type `i32`
18+
// and returns an `i32`.
19+
// make sure you make the function public by using the `pub` keyword.
20+
pub fn multiply(a: i32, b: i32) -> i32 {
21+
a * b
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
pub fn math_operations(a: i32, b: i32) -> (i32, i32, i32, i32) {
2+
// TODO: Return a tuple of 4 values: (sum, difference, multiply, divide)
3+
(a + b, a - b, a * b, a / b)
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
pub fn sum_array(arr: &[i32]) -> i32 {
2+
// TODO: Implement the function here
3+
let mut sum: i32 = 0;
4+
for el in arr {
5+
sum += el;
6+
}
7+
sum
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pub fn print_message() -> () {
2+
// TODO: Implement the function here
3+
println!("Hello, Rust!");
4+
()
5+
}

Rust/rustifinity/challenges/tuples.rs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
pub fn create_tuple(a: i32, b: f64, c: &str) -> (i32, f64, String) {
2+
// TODO: Implement the function here
3+
(a, b , String::from(c))
4+
}

0 commit comments

Comments
 (0)