Skip to content

Commit 777ebb5

Browse files
committed
Merge remote-tracking branch 'upstream/main'
2 parents d1169eb + 09af271 commit 777ebb5

File tree

58 files changed

+1897
-1465
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+1897
-1465
lines changed

listings/ch04-using-structs-to-structure-related-data/listing_04_03_mut_struct.cairo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ fn build_user(email: felt252, username: felt252) -> User {
2323

2424
// ANCHOR: build_user2
2525
fn build_user_short(email: felt252, username: felt252) -> User {
26-
User { active: true, username: username, email: email, sign_in_count: 1, }
26+
User { active: true, username, email, sign_in_count: 1, }
2727
}
2828
// ANCHOR_END: build_user2
2929
// ANCHOR_END: all
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// does_not_compile
2+
3+
use array::ArrayTrait;
4+
5+
// Specify generic type T between the angulars
6+
fn largest_list<T>(l1: Array<T>, l2: Array<T>) -> Array<T> {
7+
if l1.len() > l2.len() {
8+
l1
9+
} else {
10+
l2
11+
}
12+
}
13+
14+
fn main() {
15+
let mut l1 = ArrayTrait::new();
16+
let mut l2 = ArrayTrait::new();
17+
18+
l1.append(1);
19+
l1.append(2);
20+
21+
l2.append(3);
22+
l2.append(4);
23+
l2.append(5);
24+
25+
// There is no need to specify the concrete type of T because
26+
// it is inferred by the compiler
27+
let l3 = largest_list(l1, l2);
28+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
use array::ArrayTrait;
2+
fn largest_list<T, impl TDrop: Drop<T>>(l1: Array<T>, l2: Array<T>) -> Array<T> {
3+
if l1.len() > l2.len() {
4+
l1
5+
} else {
6+
l2
7+
}
8+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// does_not_compile
2+
use array::ArrayTrait;
3+
4+
// Given a list of T get the smallest one.
5+
// The PartialOrd trait implements comparison operations for T
6+
fn smallest_element<T, impl TPartialOrd: PartialOrd<T>>(list: @Array<T>) -> T {
7+
// This represents the smallest element through the iteration
8+
// Notice that we use the desnap (*) operator
9+
let mut smallest = *list[0];
10+
11+
// The index we will use to move through the list
12+
let mut index = 1;
13+
14+
// Iterate through the whole list storing the smallest
15+
loop {
16+
if index >= list.len() {
17+
break smallest;
18+
}
19+
if *list[index] < smallest {
20+
smallest = *list[index];
21+
}
22+
index = index + 1;
23+
}
24+
}
25+
26+
fn main() {
27+
let mut list: Array<u8> = ArrayTrait::new();
28+
list.append(5);
29+
list.append(3);
30+
list.append(10);
31+
32+
// We need to specify that we are passing a snapshot of `list` as an argument
33+
let s = smallest_element(@list);
34+
assert(s == 3, 0);
35+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use array::ArrayTrait;
2+
fn smallest_element<T, impl TPartialOrd: PartialOrd<T>, impl TCopy: Copy<T>, impl TDrop: Drop<T>>(
3+
list: @Array<T>
4+
) -> T {
5+
let mut smallest = *list[0];
6+
let mut index = 1;
7+
loop {
8+
if index >= list.len() {
9+
break smallest;
10+
}
11+
if *list[index] < smallest {
12+
smallest = *list[index];
13+
}
14+
index = index + 1;
15+
}
16+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#[derive(Drop)]
2+
struct Wallet<T> {
3+
balance: T
4+
}
5+
6+
7+
fn main() {
8+
let w = Wallet { balance: 3 };
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
struct Wallet<T> {
2+
balance: T
3+
}
4+
5+
impl WalletDrop<T, impl TDrop: Drop<T>> of Drop<Wallet<T>>;
6+
7+
fn main() {
8+
let w = Wallet { balance: 3 };
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#[derive(Drop)]
2+
struct Wallet<T, U> {
3+
balance: T,
4+
address: U,
5+
}
6+
7+
fn main() {
8+
let w = Wallet { balance: 3, address: 14 };
9+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
enum Option<T> {
2+
Some: T,
3+
None: (),
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
enum Result<T, E> {
2+
Ok: T,
3+
Err: E,
4+
}

0 commit comments

Comments
 (0)