2021-03-25 22:22:29 +00:00
|
|
|
// Unlike C/C++, there's no restriction on the order of function definitions
|
|
|
|
fn main() {
|
|
|
|
// We can use this function here, and define it somewhere later
|
|
|
|
fizzbuzz_to(100);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Function that returns a boolean value
|
|
|
|
fn is_divisible_by(lhs: u32, rhs: u32) -> bool {
|
|
|
|
// Corner case, early return
|
|
|
|
if rhs == 0 {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is an expression, the `return` keyword is not necessary here
|
|
|
|
lhs % rhs == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Functions that "don't" return a value, actually return the unit type `()`
|
|
|
|
fn fizzbuzz(n: u32) -> () {
|
|
|
|
if is_divisible_by(n, 15) {
|
|
|
|
println!("fizzbuzz");
|
|
|
|
} else if is_divisible_by(n, 3) {
|
|
|
|
println!("fizz");
|
|
|
|
} else if is_divisible_by(n, 5) {
|
|
|
|
println!("buzz");
|
|
|
|
} else {
|
|
|
|
println!("{}", n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// When a function returns `()`, the return type can be omitted from the
|
|
|
|
// signature
|
|
|
|
fn fizzbuzz_to(n: u32) {
|
|
|
|
for n in 1..n + 1 {
|
|
|
|
fizzbuzz(n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn async_example(n: u32) {
|
|
|
|
for n in 1..n + 1 {
|
|
|
|
fizzbuzz(n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const fn const_example(n: u32) {
|
|
|
|
for n in 1..n + 1 {
|
|
|
|
fizzbuzz(n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "Rust" fn foo() {}
|
|
|
|
|
|
|
|
unsafe fn unsafe_example(n: u32) {
|
|
|
|
for n in 1..n + 1 {
|
|
|
|
fizzbuzz(n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-17 02:32:46 +00:00
|
|
|
/*
|
|
|
|
unsafe fn commented_func(n: u32) {
|
|
|
|
for n in 1..n + 1 {
|
|
|
|
fizzbuzz(n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2021-03-25 22:22:29 +00:00
|
|
|
// Declares a function with the "C" ABI
|
|
|
|
extern "C" fn new_i32() -> i32 { 0 }
|
|
|
|
|
|
|
|
// Declares a function with the "stdcall" ABI
|
|
|
|
extern "stdcall" fn new_i32_stdcall() -> i32 { 0 }
|
|
|
|
|
|
|
|
async fn regular_example() { }
|
|
|
|
async unsafe fn unsafe_example() { }
|
|
|
|
|
|
|
|
const fn generic_example<'a>(x: &'a str) -> impl Future<Output = usize> + 'a {
|
|
|
|
async move { x.len() }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn generic_example2<const N: usize>(arr: [i32; N]) {
|
|
|
|
// Used as a type within a function body.
|
|
|
|
let x: [i32; N];
|
|
|
|
// Used as an expression.
|
|
|
|
println!("{}", N * 2);
|
|
|
|
}
|