Last active
October 16, 2024 19:54
-
-
Save dacr/f9c3f0adfc5d297a916aea0e27c72df7 to your computer and use it in GitHub Desktop.
hello rust functions / published by https://github.com/dacr/code-examples-manager #aeec4fd7-defa-45d9-911f-8cbcf68a9727/a81d64efb9217ee5f21b07a3ea2688b364af49fd
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env rust-script | |
// summary : hello rust functions | |
// keywords : rust, functions, @testable | |
// publish : gist | |
// authors : David Crosson | |
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2) | |
// id : aeec4fd7-defa-45d9-911f-8cbcf68a9727 | |
// created-on : 2024-10-12T23:11:09+02:00 | |
// managed-by : https://github.com/dacr/code-examples-manager | |
// run-with : ./$file | |
// ------------------------------------------------------------------------------------------------- | |
fn ident(n: i32) -> i32 { | |
n | |
} | |
fn sum(x: i32, y: i32) -> i32 { | |
x + y | |
} | |
fn show(x: i32, y: i32) -> () { // -> () is optional | |
println!("x: {}, y: {}", x, y); | |
} | |
// ------------------------------------------------------------------------------------------------- | |
fn fact_recurs(x: i32, accu: i128) -> i128 { | |
if x <= 1 { | |
accu | |
} else { | |
fact_recurs(x - 1, accu * i128::from(x)) | |
} | |
} | |
fn fact(x: i32) -> i128 { | |
fact_recurs(x, 1) | |
} | |
// ------------------------------------------------------------------------------------------------- | |
fn main() { | |
show(ident(42), sum(40, 2)); | |
let x = 23; | |
println!("{}! = {}", x, fact(x)) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment