PluginProbe
Code Block Pro – Beautiful Syntax Highlighting / 1.27.6
Code Block Pro – Beautiful Syntax Highlighting v1.27.6
1.27.1 1.27.2 1.27.3 1.27.4 1.27.5 1.27.6 1.27.7 1.28.0 1.3.0 1.4.0 1.5.0 1.5.1 1.5.2 1.6.0 1.7.0 1.8.0 1.9.0 1.9.1 1.9.2 1.9.3 trunk 1.1.0 1.10.0 1.11.0 1.11.1 All 63 releases
code-block-pro / build / shiki / samples / rust.sample

rust.sample in Code Block Pro – Beautiful Syntax Highlighting 1.27.6, at build/shiki/samples/rust.sample

40 lines 1011 B
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 // Unlike C/C++, there's no restriction on the order of function definitions
2 fn main() {
3 // We can use this function here, and define it somewhere later
4 fizzbuzz_to(100);
5 }
6
7 // Function that returns a boolean value
8 fn is_divisible_by(lhs: u32, rhs: u32) -> bool {
9 // Corner case, early return
10 if rhs == 0 {
11 return false;
12 }
13
14 // This is an expression, the `return` keyword is not necessary here
15 lhs % rhs == 0
16 }
17
18 // Functions that "don't" return a value, actually return the unit type `()`
19 fn fizzbuzz(n: u32) -> () {
20 if is_divisible_by(n, 15) {
21 println!("fizzbuzz");
22 } else if is_divisible_by(n, 3) {
23 println!("fizz");
24 } else if is_divisible_by(n, 5) {
25 println!("buzz");
26 } else {
27 println!("{}", n);
28 }
29 }
30
31 // When a function returns `()`, the return type can be omitted from the
32 // signature
33 fn fizzbuzz_to(n: u32) {
34 for n in 1..=n {
35 fizzbuzz(n);
36 }
37 }
38
39 // From https://doc.rust-lang.org/rust-by-example/fn.html
40