Skip to content

Commit 4ad0396

Browse files
committed
New init template
This new template uses newer Cairo features (loops), and additionally is both runnable and testable.
1 parent 1fb33d6 commit 4ad0396

File tree

4 files changed

+57
-10
lines changed

4 files changed

+57
-10
lines changed

examples/hello_world/README.md

+13
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
11
# `hello_world`
22

33
This example is a result of running `scarb new --no-vcs hello_world`.
4+
This template can be both run and tested, try running following commands:
5+
6+
```shell
7+
scarb build
8+
```
9+
10+
```shell
11+
scarb test
12+
```
13+
14+
```shell
15+
scarb cairo-run --available-gas 200000
16+
```

examples/hello_world/Scarb.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ version = "0.1.0"
55
# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest.html
66

77
[dependencies]
8-
alexandria_math = { git = "https://github.com/keep-starknet-strange/alexandria.git" }
8+
# foo = { path = "vendor/foo" }

examples/hello_world/src/lib.cairo

+17-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
1-
use alexandria_math::fibonacci::fib;
1+
fn main() -> felt252 {
2+
fib(16)
3+
}
24

3-
fn double_fib(a: felt252, b: felt252, n: felt252) -> felt252 {
4-
2 * fib(a, b, n)
5+
fn fib(mut n: felt252) -> felt252 {
6+
let mut a: felt252 = 0;
7+
let mut b: felt252 = 1;
8+
loop {
9+
if n == 0 {
10+
break a;
11+
}
12+
n = n - 1;
13+
let temp = b;
14+
b = a + b;
15+
a = temp;
16+
}
517
}
618

719
#[cfg(test)]
820
mod tests {
9-
use super::double_fib;
21+
use super::fib;
1022

1123
#[test]
1224
#[available_gas(100000)]
1325
fn it_works() {
14-
assert(double_fib(0, 1, 16) == 1974, 'it works!');
26+
assert(fib(16) == 987, 'it works!');
1527
}
1628
}

scarb/src/ops/new.rs

+26-4
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,32 @@ fn mk(
139139
fsx::write(
140140
source_path,
141141
indoc! {r#"
142-
fn fib(a: felt252, b: felt252, n: felt252) -> felt252 {
143-
match n {
144-
0 => a,
145-
_ => fib(b, a + b, n - 1),
142+
fn main() -> felt252 {
143+
fib(16)
144+
}
145+
146+
fn fib(mut n: felt252) -> felt252 {
147+
let mut a: felt252 = 0;
148+
let mut b: felt252 = 1;
149+
loop {
150+
if n == 0 {
151+
break a;
152+
}
153+
n = n - 1;
154+
let temp = b;
155+
b = a + b;
156+
a = temp;
157+
}
158+
}
159+
160+
#[cfg(test)]
161+
mod tests {
162+
use super::fib;
163+
164+
#[test]
165+
#[available_gas(100000)]
166+
fn it_works() {
167+
assert(fib(16) == 987, 'it works!');
146168
}
147169
}
148170
"#},

0 commit comments

Comments
 (0)