Compile-time calculation: Difference between revisions
Content added Content deleted
(Added Kotlin) |
(Added Rust example) |
||
Line 939: | Line 939: | ||
func factorial nr if nr = 1 return 1 else return nr * factorial(nr-1) ok |
func factorial nr if nr = 1 return 1 else return nr * factorial(nr-1) ok |
||
</lang> |
</lang> |
||
=={{header|Rust}}== |
|||
The Rust compiler can automatically do optimizations in the code to calculate the factorial. |
|||
<lang rust>fn factorial(n: i64) -> i64 { |
|||
let mut total = 1; |
|||
for i in 1..n+1 { |
|||
total *= i; |
|||
} |
|||
return total; |
|||
} |
|||
fn main() { |
|||
println!("Factorial of 10 is {}.", factorial(10)); |
|||
}</lang> |
|||
If we compile this with <code>rustc factorial.rs -O --emit asm</code> and inspect the outputted assembly, we can see <code>movq $3628800, (%rsp)</code>. This means the result of 3628800 was calculated in compile-time rather than run-time. |
|||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||