Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 32e7406

Browse files
authoredMar 12, 2025··
update examples (#208)
1 parent bff2996 commit 32e7406

File tree

3 files changed

+138
-16
lines changed

3 files changed

+138
-16
lines changed
 

‎README.md

+74-16
Original file line numberDiff line numberDiff line change
@@ -149,16 +149,22 @@ mod StructExample {
149149
}
150150

151151
impl Foo {
152+
pub fn new(bar: i32, baz: i64) -> Foo {
153+
let value: Foo = foo {
154+
bar: bar,
155+
baz: baz,
156+
};
157+
158+
return value;
159+
}
160+
152161
pub fn mul_bar(&mut self, value: i32) {
153162
self.bar = self.bar * 2;
154163
}
155164
}
156165

157166
fn main() -> i32 {
158-
let mut foo: Foo = Foo {
159-
bar: 2,
160-
baz: 3,
161-
};
167+
let mut foo: Foo = Foo#new(2, 3);
162168

163169
foo.mul_bar(2);
164170
foo.baz = foo.baz * 5;
@@ -173,33 +179,85 @@ mod StructExample {
173179
```
174180

175181
```rust
176-
mod Option {
177-
pub enum Option<T> {
182+
mod option {
183+
enum Option<T> {
184+
Some {
185+
value: T,
186+
},
178187
None,
179-
Some(T),
180188
}
181189

182-
impl<A> Option<A> {
183-
pub fn map<A, B>(self, f: A -> B) -> Option<B> {
190+
impl<T> Option<T> {
191+
pub fn is_some(&self) -> bool {
184192
match self {
185-
None -> None,
186-
Some(x) -> Some(f(x)),
193+
Option#Some { value } => {
194+
return true;
195+
},
196+
Option#None => {
197+
return false;
198+
}
187199
}
188200
}
201+
202+
pub fn is_none(&self) -> bool {
203+
return !self.is_some();
204+
}
205+
}
206+
}
207+
```
208+
209+
Example pseudo allocator using libc:
210+
211+
```rust
212+
mod alloc {
213+
import std.mem.{sizeof};
214+
215+
pub fn alloc<T>() -> *mut T {
216+
return std::libc::malloc(sizeof::<T>()) as *mut T;
217+
}
218+
219+
pub fn realloc<T>(old_ptr: *mut T, size: u64) -> *mut T {
220+
return std::libc::realloc(old_ptr as *mut u8, sizeof::<T>() * size) as *mut T;
189221
}
222+
223+
pub fn free<T>(ptr: *mut T) {
224+
std::libc::free(ptr as *mut u8);
225+
}
226+
}
227+
```
228+
229+
A basic for loop:
230+
231+
```rust
232+
fn sum_to(limit: i64) -> i64 {
233+
let mut result: i64 = 0;
234+
235+
for (let mut n: i64 = 1; n <= limit; n = n + 1) {
236+
result = result + n;
237+
}
238+
239+
return result;
190240
}
241+
```
191242

192-
mod UsesOption {
193-
import MyOption.{Option, map};
243+
Or using a `while`:
194244

195-
pub fn headOfVectorPlus1(x: [u8]) -> Option<u8> {
196-
// head returns an option
197-
x.head().map((x: u8) -> x + 1)
245+
```rust
246+
fn sum_to(limit: i64) -> i64 {
247+
let mut result: i64 = 0;
248+
249+
let mut n: i64 = 1;
250+
while n <= limit {
251+
result = result + n;
252+
n = n + 1;
198253
}
199254

255+
return result;
200256
}
201257
```
202258

259+
Check out the [book](https://lambdaclass.github.io/concrete/) for more examples.
260+
203261
## Inspiration
204262
The design was very heavily influenced by all these programming languages:
205263
- [Erlang](https://www.erlang.org) (Preemptive scheduler, message passing, runtime)

‎docs/book/src/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- [Variables](./language/variables.md)
1010
- [Functions](./language/functions.md)
1111
- [Structs](./language/structs.md)
12+
- [Enums](./language/enums.md)
1213
- [Control flow](./language/control_flow.md)
1314
- [Internal Details](./internal/index.md)
1415
- [The IR](./internal/ir.md)

‎docs/book/src/language/enums.md

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Enums
2+
3+
With the `enum` keyword you can define a enum:
4+
5+
```rust
6+
mod option {
7+
enum Option<T> {
8+
Some {
9+
value: T,
10+
},
11+
None,
12+
}
13+
14+
impl<T> Option<T> {
15+
pub fn is_some(&self) -> bool {
16+
match self {
17+
Option#Some { value } => {
18+
return true;
19+
},
20+
Option#None => {
21+
return false;
22+
}
23+
}
24+
}
25+
26+
pub fn is_none(&self) -> bool {
27+
return !self.is_some();
28+
}
29+
}
30+
}
31+
```
32+
33+
```rust
34+
mod Enum {
35+
enum A {
36+
X {
37+
a: i32,
38+
},
39+
Y {
40+
b: i32,
41+
}
42+
}
43+
44+
fn main() -> i32 {
45+
let x: A = A#X {
46+
a: 2,
47+
};
48+
49+
let mut result: i32 = 0;
50+
51+
match x {
52+
A#X { a } => {
53+
result = a;
54+
},
55+
A#Y { b } => {
56+
result = b;
57+
}
58+
}
59+
60+
return result;
61+
}
62+
}
63+
```

0 commit comments

Comments
 (0)
Please sign in to comment.