Last active
February 3, 2026 20:20
-
-
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/c3ad3660cf76c53ecd61e191c920ac7833d6c6fb
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 License Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt) | |
| // 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