Functions are a fundamental building block in Rust, as in any programming language. They allow you to encapsulate logic and reuse code, making your programs more modular and easier to understand. In this challenge, you will define and implement a series of simple functions that perform basic operations.
Your task is to define three functions:
add(a: i32, b: i32) -> i32
- This function should return the sum of a
and b
.subtract(a: i32, b: i32) -> i32
- This function should return the difference between a
and b
.multiply(a: i32, b: i32) -> i32
- This function should return the product of a
and b
.You need to complete these functions so that they correctly perform the specified operations.
fn
keyword followed by the function name, parameters in parentheses, and the return type.return
keyword or just the last expression in the function body to return a value.pub
keyword to make your functions public so they can be accessed from other modules.CB-42458
pub fn add(a: i32, b: i32) -> i32 { // Step 1: implement addition a+b}// Step 2:// Define a public function named `subtract`// that accepts two arguments of type `i32`// and returns an `i32`.// make sure you make the function public by using the `pub` keyword.pub fn subtract(a: i32, b: i32) -> i32 { a-b}// Step 3:// Define a public function named `multiply`// that accepts two arguments of type `i32`// and returns an `i32`.// make sure you make the function public by using the `pub` keyword.pub fn multiply(a: i32, b: i32) -> i32 { a*b}
quantumxt
pub fn add(a: i32, b: i32) -> i32 { // Step 1: implement addition a + b}// Step 2:// Define a public function named `subtract`// that accepts two arguments of type `i32`// and returns an `i32`.// make sure you make the function public by using the `pub` keyword.pub fn subtract(a: i32, b: i32) -> i32 { a - b}// Step 3:// Define a public function named `multiply`// that accepts two arguments of type `i32`// and returns an `i32`.// make sure you make the function public by using the `pub` keyword.pub fn multiply(a: i32, b: i32) -> i32 { a * b}
D-Rekk
pub fn add(a: i32, b: i32) -> i32 { a + b}// Step 2:// Define a public function named `subtract`// that accepts two arguments of type `i32`// and returns an `i32`.// make sure you make the function public by using the `pub` keyword.pub fn subtract(a: i32, b: i32) -> i32 { a - b}// Step 3:// Define a public function named `multiply`// that accepts two arguments of type `i32`// and returns an `i32`.// make sure you make the function public by using the `pub` keyword.pub fn multiply(a: i32, b: i32) -> i32 { a * b}
Spivur
pub fn add(a: i32, b: i32) -> i32 { return a + b;// Step 1: implement addition}// Step 2:pub fn subtract(a: i32, b: i32) -> i32{ return a - b;}// Define a public function named `subtract`// that accepts two arguments of type `i32`// and returns an `i32`.// make sure you make the function public by using the `pub` keyword.// Step 3:pub fn multiply(a: i32, b: i32) -> i32{ return a * b;}// Define a public function named `multiply`// that accepts two arguments of type `i32`// and returns an `i32`.// make sure you make the function public by using the `pub` keyword.
JuanjoA
pub fn add(a: i32, b: i32) -> i32 { // Step 1: implement addition a + b}pub fn subtract(a: i32, b: i32) -> i32 { a - b}pub fn multiply(a: i32, b: i32) -> i32 { a * b}// Step 2:// Define a public function named `subtract`// that accepts two arguments of type `i32`// and returns an `i32`.// make sure you make the function public by using the `pub` keyword.// Step 3:// Define a public function named `multiply`// that accepts two arguments of type `i32`// and returns an `i32`.// make sure you make the function public by using the `pub` keyword.
cdcb52
pub fn add(a: i32, b: i32) -> i32 { let add = a + b; return add;}// Step 2:// Define a public function named `subtract`// that accepts two arguments of type `i32`// and returns an `i32`.// make sure you make the function public by using the `pub` keyword.pub fn subtract(a: i32, b: i32) -> i32 { let subtract = a - b; return subtract;}// Step 3:// Define a public function named `multiply`// that accepts two arguments of type `i32`// and returns an `i32`.// make sure you make the function public by using the `pub` keyword.pub fn multiply(a: i32, b: i32) -> i32 { let mult = a * b; return mult;}
umairziyan
pub fn add(a: i32, b: i32) -> i32 { a + b}pub fn subtract(a: i32, b: i32) -> i32 { a - b}pub fn multiply(a: i32, b: i32) -> i32 { a * b}// Step 2:// Define a public function named `subtract`// that accepts two arguments of type `i32`// and returns an `i32`.// make sure you make the function public by using the `pub` keyword.// Step 3:// Define a public function named `multiply`// that accepts two arguments of type `i32`// and returns an `i32`.// make sure you make the function public by using the `pub` keyword.
relia1
pub fn add(a: i32, b: i32) -> i32 { // Step 1: implement addition a + b}// Step 2:// Define a public function named `subtract`// that accepts two arguments of type `i32`// and returns an `i32`.// make sure you make the function public by using the `pub` keyword.pub fn subtract(a: i32, b: i32) -> i32 { a - b}// Step 3:// Define a public function named `multiply`// that accepts two arguments of type `i32`// and returns an `i32`.// make sure you make the function public by using the `pub` keyword.pub fn multiply(a: i32, b: i32) -> i32 { a * b}
hooneun
pub fn add(a: i32, b: i32) -> i32 { // Step 1: implement addition a + b}// Step 2:// Define a public function named `subtract`// that accepts two arguments of type `i32`// and returns an `i32`.// make sure you make the function public by using the `pub` keyword.pub fn subtract(a: i32, b: i32) -> i32 { a - b}// Step 3:// Define a public function named `multiply`// that accepts two arguments of type `i32`// and returns an `i32`.// make sure you make the function public by using the `pub` keyword.pub fn multiply(a: i32, b: i32) -> i32 { a * b}
herlock77
pub fn add(a: i32, b: i32) -> i32 { // Step 1: implement addition a + b}// Step 2:// Define a public function named `subtract`// that accepts two arguments of type `i32`// and returns an `i32`.// make sure you make the function public by using the `pub` keyword.pub fn subtract(a: i32, b: i32) -> i32 { a - b}// Step 3:// Define a public function named `multiply`// that accepts two arguments of type `i32`// and returns an `i32`.// make sure you make the function public by using the `pub` keyword.pub fn multiply(a : i32, b : i32 ) -> i32 { a * b}
nodbew
pub fn add(a: i32, b: i32) -> i32 { a + b }pub fn subtract(a: i32, b: i32) -> i32 { a - b }pub fn multiply(a: i32, b: i32) -> i32 { a * b }
Alienora
pub fn add(a: i32, b: i32) -> i32 { a+b}pub fn subtract(a: i32, b: i32) -> i32 { a-b}pub fn multiply(a: i32, b: i32) -> i32 { a*b}
dcodes
pub fn add(a: i32, b: i32) -> i32 { a + b}pub fn subtract(a: i32, b: i32) -> i32 { a - b}pub fn multiply(a: i32, b: i32) -> i32 { a * b}
dorosch
pub fn add(a: i32, b: i32) -> i32 { a + b}pub fn subtract(a: i32, b: i32) -> i32 { a - b}pub fn multiply(a: i32, b: i32) -> i32 { a * b}
tszhong0411
pub fn add(a: i32, b: i32) -> i32 { a + b}pub fn subtract(a: i32, b: i32) -> i32 { a - b}pub fn multiply(a: i32, b: i32) -> i32 { a * b}// Step 2:// Define a public function named `subtract`// that accepts two arguments of type `i32`// and returns an `i32`.// make sure you make the function public by using the `pub` keyword.// Step 3:// Define a public function named `multiply`// that accepts two arguments of type `i32`// and returns an `i32`.// make sure you make the function public by using the `pub` keyword.
JJMinton
pub fn add(a: i32, b: i32) -> i32 { return a + b;}pub fn subtract(a: i32, b: i32) -> i32 { return a - b;}pub fn multiply(a: i32, b: i32) -> i32 { return a * b;}
GideonBature
pub fn add(a: i32, b: i32) -> i32 { // Step 1: implement addition a + b}// Step 2:// Define a public function named `subtract`// that accepts two arguments of type `i32`// and returns an `i32`.// make sure you make the function public by using the `pub` keyword.pub fn subtract(a: i32, b: i32) -> i32 { a - b}// Step 3:// Define a public function named `multiply`// that accepts two arguments of type `i32`// and returns an `i32`.// make sure you make the function public by using the `pub` keyword.pub fn multiply(a: i32, b: i32) -> i32 { a * b}
JaiSuryaPrabu
pub fn add(a: i32, b: i32) -> i32 { // Step 1: implement addition return a+b;}// Step 2:// Define a public function named `subtract`// that accepts two arguments of type `i32`// and returns an `i32`.// make sure you make the function public by using the `pub` keyword.pub fn subtract(a : i32, b : i32) -> i32 { return a-b;}// Step 3:// Define a public function named `multiply`// that accepts two arguments of type `i32`// and returns an `i32`.// make sure you make the function public by using the `pub` keyword.pub fn multiply(a:i32,b:i32) -> i32{ return a*b;}
Mrigesh901
pub fn add(a: i32, b: i32) -> i32 { // Step 1: implement addition let sum = a+b; return sum;}pub fn subtract(a: i32, b: i32) -> i32 { let sum = a-b; return sum;}pub fn multiply(a: i32, b: i32) -> i32 { let sum = a*b; return sum;}fn main() { let result = add(2, 3); assert_eq!(result, 5); let result = subtract(5, 3); assert_eq!(result, 2); let result = multiply(2, 3); assert_eq!(result, 6);}// Step 2:// Define a public function named `subtract`// that accepts two arguments of type `i32`// and returns an `i32`.// make sure you make the function public by using the `pub` keyword.// Step 3:// Define a public function named `multiply`// that accepts two arguments of type `i32`// and returns an `i32`.// make sure you make the function public by using the `pub` keyword.
Hritul2
pub fn add(a: i32, b: i32) -> i32 { // Step 1: implement addition a+b}// Step 2:// Define a public function named `subtract`// that accepts two arguments of type `i32`// and returns an `i32`.// make sure you make the function public by using the `pub` keyword.pub fn subtract(a:i32,b:i32)->i32{ a-b}// Step 3:// Define a public function named `multiply`// that accepts two arguments of type `i32`// and returns an `i32`.// make sure you make the function public by using the `pub` keyword.pub fn multiply(a:i32,b:i32)->i32{ a*b}
nerudxlf
pub fn add(a: i32, b: i32) -> i32 { // Step 1: implement addition a + b}// Step 2:// Define a public function named `subtract`// that accepts two arguments of type `i32`// and returns an `i32`.// make sure you make the function public by using the `pub` keyword.pub fn subtract(a: i32, b: i32) -> i32 { a - b}// Step 3:// Define a public function named `multiply`// that accepts two arguments of type `i32`// and returns an `i32`.// make sure you make the function public by using the `pub` keyword.pub fn multiply(a: i32, b: i32) -> i32 { a * b}
istominrs
pub fn add(a: i32, b: i32) -> i32 { a + b}// Step 2:// Define a public function named `subtract`// that accepts two arguments of type `i32`// and returns an `i32`.// make sure you make the function public by using the `pub` keyword.pub fn subtract(a: i32, b: i32) -> i32 { a - b}// Step 3:// Define a public function named `multiply`// that accepts two arguments of type `i32`// and returns an `i32`.// make sure you make the function public by using the `pub` keyword.pub fn multiply(a: i32, b: i32) -> i32 { a * b}
enzo-rma
pub fn add(a: i32, b: i32) -> i32 { // Step 1: implement addition a + b}// Step 2:// Define a public function named `subtract`// that accepts two arguments of type `i32`// and returns an `i32`.// make sure you make the function public by using the `pub` keyword.pub fn subtract(a: i32, b: i32) -> i32 { a - b}// Step 3:// Define a public function named `multiply`// that accepts two arguments of type `i32`// and returns an `i32`.// make sure you make the function public by using the `pub` keyword.pub fn multiply(a: i32, b: i32) -> i32 { a * b}