The One Question bounty closes Thursday 10 December.

Join in
We Them Media logo We Them Media Support us

Cairo Programming Language I

The picture loads from YouTube. The player loads only when you press Play. Turn on captions in the player if you need them.

Musa Abukarim, called Wise Mr. Musa, teaches the first part of a Cairo masterclass, linking Cairo to StarkNet and the blockchain trilemma. The class covers how Cairo compiles through Sierra, variables, types, functions, control flow, arrays, ownership and structs, with quizzes, and several students say the pace is a lot to take in.

Not by We Them Media Channel: Web3Bridge. Published Thursday 23 May 2024.

Titles are the owner’s. It plays from their channel.

Transcript

This transcript may contain errors. This video belongs to Web3Bridge. Letters mark each change of speaker.

These are the speakers’ own words. We have not checked their claims.

0:04 A: All right, so, um, yeah, my name is Musa Abukarim, popularly known as Wise Mr. Musa, and I'm going to be the instructor for today's class. Yeah. So in today's class, we are going to be talking about, um, we are not talking actually, we are going to be learning about the Cairo programming language. So for today's session, we are going to be going through the the first part of it. So yeah, we, um, in order for us to- so there are some things that we actually need to understand as regards Cairo so that it's going to like be a fundamental for, um, what we are doing. So, you know, this particular class now is called the Cairo Masterclass, um, and before you can have a masterclass in any session, it means that you have to be first good at, you know, the basis. So we all know that the reason we are learning Cairo is because we want to be able to build on StarkNet. And we all know that the reason we have StarkNet is because there is a fundamental problem with distributed systems. So when you have, you blockchain. So we have what we call the blockchain trilemma, which is that a blockchain cannot be decentralized, have security, and also scaling. So which means that any blockchain that you look at, they're going to sacrifice 2 to get the 3rd one. They would have to sacrifice 2 in order for them to get to get the third one. So, uh, for Ethereum as a layer- so that's the reason we have what we call the layer 1 blockchains and then the layer 2 blockchains. So the layer 2 blockchains are basically, um, solving the scalability problem. So I don't want to like dive too much into You know, all of those talks. I believe the previous classes explained it better. So just a little bit of reminder, uh, to serve as an introduction to, uh, what we are going to be doing today. Like, why, why do we need to learn Cairo since we already know Solidity? Why do we, why do we still have to learn, um, Cairo? Sorry, I want to add. So why do we have to learn Cairo? So that's the basic question now. So if you look at Ethereum as a layer 1 blockchain, Ethereum is decentralized, right? Yes or yes? Please, this, this is a chart. Like, you can turn on your mic so that we can be listening to ourselves. Yeah, so it's not something, you know, very, very, very serious like that. So Ethereum is a layer 1 blockchain, so it is decentralized. Yes or yes?

3:51 B: Yes.

3:56 A: So what do you mean by the word decentralization? So decentralization simply means that, you know, it can mean 2 things. Either it is centralized or decentralized. When you say that something is centralized, it is controlled. Please, um, if you know that you're in a noisy environment, you can just be, you know, muting your mic when you want to speak. Right? So, you can- I'm trying to join the-

5:03 B: okay.

5:16 A: All right, so, so we have what we call, you know, you can have something that is central or it is decentralized. So central, it means that it is controlled by one party. So when something is controlled by one party, it means that it is central. So for example, when you hear the word the Central Bank of Nigeria, Right? So Central Bank of Nigeria is a one party that controls all the financial systems that is happening in Nigeria. So all of us, we have no information about it. But when you say that something is decentralized, it means that you have a lot of things, a lot of authority, you know, like all the entities, there are authorities in themselves. So, and when you have a decentralized authority like that, then there has to be a consensus mechanism. There has to be a way for them to agree to a valid state. So, and in order for them to, you know, do all of those things, there are some trade-offs that comes into play. So we all agree that Ethereum is what is decentralized. The second is that- The second is security. So Ethereum is also secured in the sense that, you know, and what Ethereum uses as its security mechanism is basically, you know, we have the crypto economics. So which is that when you want to- when you are- if you want to like hack Ethereum, then you have some- you want to act as a dishonest party. So you have proof of stake, which A part of your- like a large sum of money. So because of that, you don't want to- you don't want to try that. So, but then scalability. So is Ethereum- is it scalable? So the short answer is no, because the, um, for Ethereum, the number of transactions that you can have per second is between 15 to 30 transactions per per second, which is, uh, which is very, very low. So in order for you to solve this problem, and for layer 2 solutions, there are basically 2 types of layer 2 solutions. So we have the optimistic, optimistic rollup and what, what we call the validity roll up. So when you say that something is optimistic, you know, in English optimistic is that you just, uh, trust me, trust that. Let's just be optimistic that these transactions are what are valid. And the way it also works is that if the transactions- there is, there is a context if a particular transaction is what is not valid. So which means that so far as nobody contends that a particular transaction there is invalid, for the waiting room, then the transaction is what? Is valid. So that's the optimistic. You are optimistic that all the transactions there are what? Are valid. So the validity rollup, that one is also called what? The ZK rollup, which is the zero knowledge, which means that when you are bundling a transaction, you are also sending its proof, the proof that this transaction is what? Is valid. You are sending it along So, and you know, there are many ways in which you can have the ZK. So we have the ZK primitives that talks about, you know, all of the different ZK systems that we have. So talking about the, talking about the circuits, the R1CS, the Rank-1, the, you know, we have the Rank-1 constraint system, the quadratic arithmetic program, And then we now have another one that you can now also use to what? To prove that a particular transaction is what? Is valid. So the ZK, we now have what we call the what? The SNARKs and the stack. So when you have stack and SNARK, we know what SNARK means. SNARK means succinct non-interactive argument of knowledge, while STAK means scalable transparent argument of knowledge. So when you say- when you have this now for STAK, you want to prove, you know, something here. So you have to first convert those statements. You have to convert what you want to prove into arithmetics. So you have to convert it into arithmetics that you can easily, you can easily prove. So the process of converting whatever, whatever you want to prove to arithmetic is what we call what? The arithmetization. So we have the arithmetization process. You know, and there are different arithmetization processes. So one of the arithmetization processes is that, um, the, the, uh, the Rank-1 constraint system is an arithmetization process. So you are converting everything into a, um, into, you know, Rank-1 is that you can have x+ y= z or x times y= z. You can prove the 2. I also have the quadratic arithmetic, uh, program. So we also now have the third one, which is what we call the- what? So, uh, in this, for Cairo, uh, for StarkNet, what is now used- we now have something we call what? The, uh- so we have what we call what? The Cairo. So what is the meaning of Cairo? So Cairo simply means- Cairo simply means CPU Uh, algebraic intermediate representation. So CPU algebraic intermediate representation, that's the, uh, simple meaning of Cairo.

11:56 B: Sorry.

11:57 A: So that's the simple meaning of, of Cairo- CPU Algebraic Intermediate Representation. So what does, what does this mean? Now you know that when you are, uh, when you are performing a program, when you have a computer, right? Now a computer is simply- we have a general, uh, a computer is just, you know, something that makes our work easier. It takes in input and gives out- gives us output. Now, the CPU of your computer, you know, normally before you used to have, uh, you used to have CPU or chips, let's call them chips, that are designed for a specific purpose. So if you want, you can design a chip that is going to, you know, just take 2 numbers and produce another one. So, but then we had Alan Turing that, uh, gave a model of a general-purpose computation, uh, which they called what? The Turing completeness. So with that, you can have a single- No, no, no, that's the von Neumann architecture. So the von Neumann architecture is the one that proposed a system whereby can have a general purpose computing. So a general purpose computing means that you can- you'll be able to like compute, uh, you know, program anything on the CPU. So we have CPU model. So this particular one also is going to be one of, uh, one of CPU models that you can make use of. So what does this CPU model do? So it simply a way of converting if you have, uh, you know, uh, if you have- sorry. Yeah, so if you have any computation, so if you have any computation it's going to, uh, you are going to be able to use this particular architecture to be able to process it. You'll be able to use, uh, this particular, uh, what's it called, to be able to process it. So now this Cairo, the Cairo that we are now talking about, you need to understand that Cairo can mean 2 things. Cairo can mean 2 things. So Cairo itself, so there is the Cairo that deals from low level to the, you know, execution of proof systems. So, and that one, it's simply that when you have a- when you write, you know, algebraic representation, you are going to write whatever you want to prove. It allows you to be able to like generate, uh, proof a computation. You are going to generate proof of computation and you are going to verify it in computer that has a smaller, uh, that has a smaller power. So when you have, uh, Cairo in that case, so when you write, let's say you can write, uh, you can write your algebraic representations, you can write your algebraic representations, and that Cairo is going to help you to prove it. So that's like one aspect of Cairo that we need to firstly understand. So that's the reason. So that's like the first part of Cairo. Then we now have another part of Cairo. So the other part of Cairo is the Cairo programming language itself. So the Cairo programming language now allows you to write, uh, you know, it now allows- it compiles a high-level programming language into, uh, the Cairo assembly. So If you look at the way the evolution of StarkNet and Cairo, there was a time that we had the Cairo Zero. So in Cairo Zero, it was a Python that you can simply use to write, to write, to write all those intermediate, your program, and it's going to be converted to what? To KASIM. Then From Cairo 0, it moved towards Cairo 1. So in Cairo 1, we now have this one, it was Python, and this one, it is Rust-like programming language. So which means that there is- this is like a layer of abstraction which gives us- we don't want to think about x+ y= 3x+- we don't want to think about all of those low-level stuff. So we have the Cairo programming language that is going to allow us to write a smart contract in a syntax of a friendly programming language like Rust and is going to compile it down. So when it was in Cairo 0, when it was in Cairo 0, when you write your Cairo 0 program, it's going to convert to what? To Kasm. But for Cairo 1, when you write a Cairo program, When you write a Chiro1 program, it's going to convert it to what we call what?

17:19 B: SIR.

17:20 A: It's going to convert it to SIR. What does SIR mean? SIR means Save Intermediate Representation. Save Intermediate Representation. Now this SIR that you now have will now be converted to what? to Kaze. Now if we are going to ask why do we have Sierra, so the purpose of Sierra is simply that Sierra, it wants to make sure that it's safe. There means that it is not possible for you to have a Cairo program that is going to panic. You know, if you are writing a Rust program or even JavaScript program, there are possibilities of, you know, for you to have different kinds of error that it can result into panic, that your program can just return error. But with this, it means that when you write a Cairo program in a, in a program- in a programming language that is, you know, human-friendly like Cairo, when you write a Cairo program, now it is not- you have to convert it first to Sierra. So when you convert it to Sierra, if you successfully convert it to Sierra, It means that it is not possible for that program to fail. It's not possible for that program to fail. So that's- which means that for any input, there is going to be an output. For any input, there is going to be an output. Then the CRA is now converted to what? To KASM, which is the Cairo Assembly Language. So since the CRA is safe, then it means that the assembly language that is generated from the CRA is also safe. So that is just about what the compilation- so that, that particular part of it is just about the compilation. Now, you know, at the other part of, uh, what's it called, at the other part of the Cairo also, so for that part of it, it is not possible for, uh, for your Sierra code, it cannot panic or anything. So when you now convert from Sierra to QASM, then at the QASM level, QASM is simply an assembly language which then performs whatever it is that you want to do. Then we now have- then we now have the other one. You know, when you've already like compiled it to- you already have the QASM there. So if you are going to be running it, then whatever computation that you are performing on it is going to generate a proof for it. So it is also, it is now possible, you know, if you have deployed the contract, then it is possible for, you know, people to run, they might want to like run a program and it's not going to, it is possible for it to fail them. But when you compile your program from the high-level language to cache, it is not possible for it to fail. So that's the main purpose of, of Cairo. So, you know, it is, uh, for normal CPU architecture, the main, the major problem that people usually look into is the hotting problem. And the hotting problem, you know, that's also one of, one of the things that resulted into what, gas metering of of Solidity and Layer 1. So what for- but for Cairo, it is not only all 3 problems that you are looking at. You are also looking at what's panic. So a Cairo program, it cannot panic when you convert- when you've converted it into what? Into Sierra. So which means that when you are writing your code, if there is any possibility of it to panic, it is not going to compile. So you need to first understand that it is not going to compile into Sierra. And yes, and the programming language also that we are also making use of, it is also optimized in such a way that the proof system, the proofs that are generated from it, it has already been optimized. So using int- so those ones are the low-level aspect of, of the Cairo that we are not so interested about at this point. So what we are going to be doing now is we are going to be looking at, okay, how do we write a Cairo program? How do you write a Cairo program? So the number one thing that we need to understand is that Cairo programs, their syntax is Rust-inspired. I mean, by Cairo 1 programs, their syntax is Rust-inspired. So it doesn't mean that you can do everything that you do in Rust, that you can do it in Cairo. That doesn't- that's not the case, because for every- all the things that you are going to be making use of in Cairo, they are already associated with some ints. They are compiled to you know, Sierra. So you need to understand that, you know, when you write Rust, it is possible for you to convert your Rust, compile your Rust down to maybe bytecode or LLVM or different kinds of, different kinds of targets. So, but for Cairo, your target is what is Sierra, and you can only make use of what has been predefined. So we are going to be going through the Cairo book to, uh, to learn how to, you know, how, uh, how to do- how to write a Cairo program. So of course, if you want to, I believe every one of us here, we've already installed the- we already have Cairo on our system, right? Is there anyone that hasn't installed Uh, Cairo. Okay, so since all of us, we have installed Cairo on our system, and we also have the- you know, it's just like Rust. In Rust, you have Rust as the programming language, then you have the package manager, which is the crates. So in Cairo, you have Cairo as the programming language, and you also have the package manager, which is what? Which is the scab. So we are going to be looking at how to write. Uh, so let's first just check out how do we even, um, how do we write, how does writing, uh, Cairo programming language, how does it, how does it look like, what does, how does Cairo look like, how does Kasm look like, and all of those, all of those stuff. Okay. Okay, so I actually need to set the- what's it called? All right, so before we go, like, while I'm trying to set, set it up, does anyone have any question? Maybe what I'm doing, it's not clear yet. Does anyone have any question as regards Cairo? We all understand Cairo now, why we are writing Cairo, right? That's great.

25:54 B: I have a question. Can you hear me?

26:01 A: Yes, I can hear you.

26:02 B: Okay, so you were talking about, um, how it's like when we're writing, uh, Cairo code, it comes from the Cairo, um, high-level language, then down to Sierra, then to Kasm. So like from that Sierra to Kasm, so you explained something that I didn't actually catch. So if you can just, you know, rephrase, like, why do we need to go from this area? Why not just go down directly to CASL?

26:35 A: All right, so in, in the Cairo 0, in the Cairo 0 version, you know, when you are writing in Python, you can write your Cairo program and it goes directly into CASL, right? But for Cairo 1, so I said that the meaning of CIR is Safe Intermediate Representation, right? So the word safe there, it means that it's, uh, it makes sure that whatever program, what you have written in that, in that high-level Cairo programming language, it cannot panic. Do you understand the meaning of panic? So panic is anything that can make your program to stop at the middle, in the middle. Do you get? Anything that can make your program to stop in the middle. So if you write systems programming language, there are some times that you just be running your program and it's going to reach a particular whatever kind of error. It will reach some kind of error that you just panic and it will stop. It's just like Maybe you are writing JavaScript and there is a runtime error, there's a runtime error, and let me say compile time error, and it stops. Uh, so something like that. So converting to Sierra, it means that it is safe. So from Sierra to Chasm now means that whatever, um, that particular Cairo code It is- it simply means that that's, that's just, that's just, um, the simple meaning. It cannot panic.

28:23 B: Okay, so I'm going to, uh, refer to Solidity because that's what I know, right? So why didn't we need, um, Sierra for Solidity?

28:33 A: Okay, for Solidity, you, uh, what you are- so there is difference between So there's a difference between the way Solidity and Cairo works, right? In Solidity, you are running Solidity on EVM, which is Ethereum Virtual Machine. And EVM, it's just, it's, um, it's just the same normal CPU model. So the normal CPU model is you know, you read, write, and all of those stuff. Like, it's read from memory, write to memory, and all of those stuff. But for Cairo, it is different. So for Cairo, Cairo is proof of computation. Cairo is proof of computation in the sense that what you are Hello? Yeah, okay, hang on.

30:34 B: He's having tech issues.

32:36 A: I hope everyone can hear me.

32:38 B: Hello, we're not hearing you.

32:45 A: Okay, so actually I'll just start now. Okay, how about now?

33:01 B: Yes, sir, we can hear you.

33:05 A: All right, thank you. So I'm saying that there's difference between Solidity and Cairo. The difference is that we use Solidity- Solidity is used on, um, on EVM, right? So which means that Solidity is just like the normal programming languages is similar to the normal programming languages and it runs on the normal CPU. So a normal CPU, it has- it can read from memory, it can write to memory, right? So that's how the normal CPU works. So which means that Solidity, you just convert it into what? Into bytecode. When you write your program in Solidity, it is compiled into bytecode. and that bytecode is run on the EVM, right? Now there is now, uh, Cairo. Cairo is for StarkNet, or Cairo is used for, um, zkProof. Now when you are making use of zkProof, so you make use of zkProof or zero-knowledge proof for either to achieve 2 things, either to achieve privacy or to achieve what? Scalability. So when you make use of- for scalability, it just allows you to perform computation on a big system. Let's say that you have 2 systems. You have a computer and you have a phone. Now you want to perform a computation on your system. You cannot perform that computation on your phone because you know, maybe your phone has 2 MB RAM, but your system has 1 TB RAM. So which means that you can perform that computation on your system. However, you cannot perform it on your phone. So you want to be able to perform this computation on your system, then prove that you correctly performed it on your system on your phone. So proving is different from what- So for you to be able to generate that proof you will need, you know, all those ZK protocols. That's where that Cairo comes in. And the way Cairo CPU model works, it is different from the way the normal CPU model works. So for the normal CPU model, it is read-write. You can read in the memory and you can also write into memory. However, for Cairo, it is a read-only model, which means that you don't write. It is a read-only model, which means that you don't write. And also it is proof of computation, so which means that, you know, when you run a, uh, when you run a Cairo program, the way it processes it is different from the way normal CPU processes it. So that's the reason, uh, for Cairo you have to convert to Sierra first, then convert it to what? Into Kasm. But for Solidity, from- you just convert it to bytecode and it's going to run normally on the- you know, because whatever it is that you are doing on with the Cairo, the Cairo, after it has run the program, is going to generate a proof that the program run correctly and the outputs. So that output and the proof is now going to be sent into Ethereum as the settlement layer. So on Ethereum, there is a contract, there is a smart contract there that is going to take in the output and the proof, and it's going to verify the proof of computation, and it's going to register the validity of that proof. So there is a there is fundamental difference in the architecture of the way the two of them, the way they work. So for Cairo, you have to convert to Sierra first, then to, then to Kasm. But for Solidity, you go straight from the programming language to, from the programming language straight to Straight to bytecode. Yeah.

37:27 B: That makes sense. That makes a whole lot of sense. Thank you.

37:31 A: You are welcome. Any other question? Yes, I have a question.

37:48 B: Can you hear me? Yes.

37:50 A: You, you mentioned that for Solidity, the, the EVM uses read-write, but for Karo, it uses read-only. So how are, um, let's say you send a transaction now, or you are performing a computational task, how do you, how are the information saved? Are they not saved, or they, after processing, they are discarded? All right, thank you very much. So the way it works in Cairo is that there are some- there is something we call ints. There's something we call what?

38:31 B: Ints.

38:32 A: So the way Cairo works is that because it uses read-only, it doesn't write, so it's going to Um, int is that in some memory it's going to assign before the Cairo program, before it starts running, it's going to write some values into those, into those memory. So what we mean by read-only, it means that when the program starts running, it doesn't change, it doesn't mutate a memory address. So it's going to provide ints in in different places. So it will just, uh, read and assert that, okay, uh, what is in this memory, uh, is what is- is what it's, uh- maybe what is in this memory, if you add it, what is in this memory, you are going to get what is in this memory. So there are like 2 things that actually occurs in a, in a KASIM. So it's either you are asserting. So that asserting is like the write. So instead of writing into a particular memory, it's going to use int to have written into the memory already before the program starts. Already it should have- and the int would have, you know, put the, uh, it should have put some values in some of, in some of those memory. So it will just assert that, okay, what is in this memory is this. So that one is now equivalent to be write. Now, and you know, the next one is going to be it can transition from one memory address to another memory address. So I hope that answers the question. Yeah, it does, but, but it's still a little bit confusing. Maybe you need to do more research on Okay, of course, uh, you might need to do more research, uh, for that. So if you want to- So to learn the Cairo programming language, of course, the best place is to use the Cairo book. So the best place is to use the CairoBook, and I hope everyone can see my screen.

41:07 B: I have a question.

41:11 A: Yes, yes, what's the question?

41:15 B: So if my program needs to be compiled and then comes back to Pyro2Heat. So does it mean the bigger the project, the more time it can take to compute, or that is one of the issues?

41:33 A: Can you be more audible? Or like, I cannot access it very well.

41:39 B: Can you hear me now?

41:40 A: Yes, I can hear you now.

41:41 B: Okay, so I'm saying if the program needs to be compiled down Because I'm like, the more-

41:56 A: I can hear you now. Continue.

42:00 B: Oh, okay. So I'm like, the more- the bigger the program, the more time it will take to be competent, or that is not an issue? Doing the reading, the reading test here.

42:16 A: So this is it, you know, when we talked about the, uh, the zero knowledge and how it solves the scalability issue. So it's just like when you have your computer, you have your computer that is very, very big, you have a computer that is very, very big and you have a phone that is very small. Now you can perform some computation on, on your computer because it has very big memory and very big processing power. So you don't really care about, you know, about, uh, the size of the computation or anything. So for the- but it's not going to generate a proof. Now that proof is going to be succinct, which means it should be small, and you can easily verify it on your phone even though your phone has a very small, um, computational power. Now, how do we relate this with, um, with blockchain? So in layer 1 and layer 2 on Ethereum, uh, on Ethereum, if you want to perform a transaction, you know how high the gas fee is. The gas fee on Ethereum is very high. So which means that, you know, there is- there are lots of optimizations and, you know, different things that you have to do in order for- if you want to swap $50, you don't have to pay maybe $70 in order to swap $50. So however, on StarkNet layer 2, uh, blockchain, the layer 2 blockchain, now number one, the gas fee is very- What StarkNet does is that You know, it's going to take a lot of transactions. It will bundle them, and for all of them, it will now generate- you know, the computational power of that one is going to be very, very big. However, we don't care about- like, we don't really, really care about the computational power of that one because that's like our system. our system has enough computational power. So the one that we care more about is our phone, that the computational power is small. So on, on StarkNet, it is basically like- it is more like you don't really pay for, for, for gas or anything on StarkNet. You can perform whatever computation on, uh, on StarkNet. So it is not going to take whole lot of computations, bundle them together, and generate a succinct proof of computation for them. Now that succinct proof of computation will now be verified on Ethereum. So it is- so the gas fee that you are just paying there is just like, you know, after it has bundled everything together and this thing, so the gas fee is now relatively very small. for the user. It's now relatively, relatively very small. If you look at it now, currently the swap cost for performing transactions now is now below a cent on StarkNet. So which means that gas fee is no longer a problem, or it is solving the scalability issue. You can perform lots of loads of transactions, it's going to handle all of them, bundle them, and what, have the proof of computation on the, uh, and send the proof of computation to Ethereum, which is the base layer. I hope that answered the question. If it doesn't, we are- I'm going to explain. All right, thank you very much. So Getting started installation. So if you want to run, uh, if you want to run this, you should have installed SCAP, and you should have installed SCAP. So if you should run this on your terminal to know if- so if you should run this, then when it shows, you know, the SCAP version 2.6.4 the Cairo version is 2.6.3 and the Sierra version is 1.5.0. So that's going to show you that yes, you are on the, you're on the right track. So let's write hello world. So to write hello world in Cairo is actually very simple. So it's just like, you know, if you want to write the Rust, in Rust, if you want to write a program in Rust, you can simply make use of the package manager. You can use cargo new, but in this case we are using what? Scab. So Scab is our package manager. So we are going to say scab new hello_world, right? So with this now I can Now let us look at- let's try to understand what is happening here. I hope everyone is, everyone is with me. Is there anyone that has written Rust before here? How many people have written Rust before?

47:48 B: Okay, nobody.

47:53 A: It's not a problem. So if you should look at this place now, so the starting point is we have this scav.toml, so which is like the, the, uh, the configuration, the package configuration. So you can see in this configuration the name of the project is hello world, the version is 0.1.0, and the edition is this. So if you are going to have some dependencies in our in our project, we are going to have them here. And .gitignore is simply, uh, ignoring- so these are what we don't want to push into our GitHub. So in this place now we have the src, which is like the source, the starting point of everything, and inside it we have the lib.chiro. So the lib.chiro is our starting point. The lib.py is our starting point. So if you look at this one now, in this case we have the function, the main function is here, and this main function, it is calling another function which is this fib. So it's generating the Fibonacci of 6. And so this is just- so, but that's not what we want to do now. What we want to do now is we want to write hello world. So how do we write hello world in StarkNet? So in this place, so it's telling us that we have the TOML, meaning terms of use minimal language. So that's okay. We have the last line is the dependency. The other file created by scrap is the src/lib.skyroad. So let's delete all the contents and put in the following. So we have the mod hello world. And so if you should put this mod hello world in this place Then we would have to, we have to create another file here and call it hello world inside this same src. So it's going to be hello_world.py. So then in this place we can have function. Of course, the entry point of any programming language is what, uh, the entry point of this particular programming language- don't let me say any- is the main. So we have function main. And we have println Hello World. Now I'm going to, I'm going to explain something here.

51:09 B: So we have-

51:11 A: Someone's trying to say something.

51:22 B: Try to zoom in on your IDE, it's very tiny.

51:25 A: Oh, okay, okay, okay. How is it now?

51:31 B: Very, very good. Thank you.

51:35 A: Okay, so here we have function main, println. So this println here, you know, in, in Rust we call them what? Macros. So macros are like shortcuts or shorthands for some stuff. So the folder structure that we have now, we have lib.skyyro, hello_world.skyyro. Now there's something that I want us to understand here. Yes, the normal thing is that we can write our- the starting point, the starting point of our program is this lib.skyyro, is this lib.skyyro, which means that inside this lib.skyyro we can you know, write any program that we like in this place, this place. But if you want to use another thing, if you want to use another package, you'd have to like make a reference to them. So we call this what, uh, here now this is a module. Now if you have this, uh, lib- if you have this lib.pyplot, this hello world here now can see that's what we are- it uses the name of this directory as the module name. So this one is a directory, you can also make it a folder. So when you make it a folder and it is hello_world, then inside the folder also you need to have the lib.chiro inside. So if you want to build, now we have 2 things. Building and what's running. In Rust also, in, in the Rust programming language, you can decide to build. So when you are building, it's like you are compiling from one particular language to another. You are compiling from one particular language to another. So in this case, which is for Cairo, if you are building, it means that you are converting your Cairo programming language into what? Into Sierra. So Sierra into- it's going to convert, it's going to build it into Sierra and what? Alcazim. So in that case, if you should say scab build and we put the name of the, uh- so if you say scab build, it's going to compile what we have. So if you look at it now, we now have these targets. Now in the target we have dev and we have, uh, we have the Sierra code there. So this is the Sierra- this is not formatting it. So this is the Sierra of what, uh, the Sierra version of what we've written here. this hello world that we have here. So that's the CIRL version of it, and we can also run it. So if you are going to run the program, so we say what? sciro run. So when you run it, it prints what we have here, which is the hello world. So we have it in this place. So you can see, this is my first Cairo program. Yeah, so you can see is my first Cairo program. All right, so the anatomy of a Cairo program. So let's review this hello world. Now we have this fn main. So this fn simply, it stands for what? Function. And main, main is also the entry point of any function. So you see that these lines define a function named main. The main function is special. It is always the first code that runs in every executable Cairo program. Here the first line declares the function named main that has no parameters and returns nothing. So if there were parameters, they'll be- they are going to go into the parentheses. So which means that, let's say that this function- we have a function that is going to take in some parameters, that is going to take in- that has parameters, or that is going to take in some arguments. So inside we can have Uh, let's say another function, and let's say this is going to take, uh, name, and the name is- so as you are writing the name, you are putting in the- what's it called- the, uh, the type. So So we also don't want it to return anything. So we can decide we want it- maybe we want it to return something, or we don't want it to return anything. If you want it to return something, then you need to indicate the arguments of what you want it to return. If you don't want it to return anything, it's the same thing as it is returning void. So which means you can have something like this, and in this case now we say print line. And so which means that when you have a particular name, when you pass in a name as an argument, then it's going to return that name here. So we can call that function in this place. So let's see, I can put in my name here and say Right, so if I should run it- what's that? It seems, uh, it says what? Oh, oh, oh, okay. The, the data type. Yes. Okay. Yes. So the reason actually is used when you are making use of fields, you don't use double quotations, use a single quotation.

58:14 B: Is it not supposed to be 252? There's no 256. 252.

58:28 A: Yeah, 252.

58:33 B: Sorry.

58:33 A: Yeah, so this is my first programming lab. Now I'm going to return this. Uh, let me use a debugger. Oh, so this actually- what it's returned here is You know, felt- no, I think it's because here we have- so felt has to be due to previous Error must be string literal. HS UPANI POG. Okay, so this what we have here is the same as this, um, this wise Mr. Amosa. So the, uh, the reason it is showing this way is because it has- if you want to get this, it has to be in debug mode. Now you'd remember that I said that there is the arithmetization process. So arithmetization process, it means that everything that you are seeing is converted into- everything that you are seeing is converted into numbers. So the meaning of the, uh, the basic data type in Cairo is what is FELD252. Now what does FELD252 mean? It means this FELD, it means field elements. So the field element, because you know the Cairo, it has the prime number that it is making use of under the hood. So the strings and everything, everything has to be converted into number. So this one is still in the normal mode. That's the reason we have that. So is it that second print line calls a Cairo macro. I think I have talked about this. So if you had called the function instead, it should be entered as print line without this. So that's telling us the difference between a function and a macro. So we have a quiz here. Should we, should we do this? So we have, what is the name of the initial function that a Cairo program runs? So what is the name of the function? Like, what's the name of- whatever you tell me that I'm going to put here. What? Main, main, main, main. Do we all have function? Oh, it's master. Is it not master? Yes, it's main, main. I think it's master. How many people choose me and how many people are with me? Master, if you are, if you are with Master, raise your hand. Okay, I have Abel with me. Ah, no, nobody is going for Master. Okay, so if you are for me, let me see your hands.

62:43 B: Me.

62:47 A: Okay, it is what you people did there. Submit. Number 2, which command will run the code in your projects? So which command will run the code in your project? Is it- it is scab build, right? Scab pyro run. Ah, it was scab build that we used first. Now what's different between scab What's the difference between scab build and scab kyber run? Scab build will only build this and convert it to C and K, but scab run, kyber run will build and also run it. Compile that. Okay, so let's submit. Ah, So this one is not master.

63:41 B: Wow.

63:41 A: Okay, so let's move to the next. Common programming concepts. So in this, in this case, we are going to- it's going to cover common programming concepts. Basically, you are going to learn about variables, basic types, functions, comments, and control flows. So these foundations will be in every Cairo program, and learning them early will give you a strong core. So variables and mutability- what's the meaning of a variable and what's the meaning of mutability? So I said that something varies, it means it changes, right? And the meaning of mutability- mutability is just the ability of something to change, right? So, so that Cairo uses an immutable memory model. I think I have mentioned that. So when you hear the word immutable memory model, immutable memory model simply means that what the memory does not change when it starts running in memory, it does not change. Do we understand? Hey, it's like we are sleeping.

64:54 B: Yes, we do.

64:57 A: All right. So, meaning that once a memory cell is written to, it can't be overwritten but only read from. To reflect this immutable memory model, variables in Cairo are immutable by default. However, the language abstracts this model and gives you the option to make you- to make your variables mutable. Let's explore how and why Cairo enforces immutability and how you can make your variables mutable. So when a variable is immutable, once a value is bound to a name, you can't change that value. Yeah, yeah. So to illustrate this, generate a new project called variables in your Cairo project directory using scav new variable. So in our projects, we are going to cd out of this. and create another one. We say what? Scab new variables. Then, so now then in your new variable, so we have this in the src/lib.skyro. So when you say that let x equals 5, you can print line that the value of x is this. x equals 6, the value of x is what is this. Let's, let's check something. So in the src here, we are going to delete everything here. So, uh, we say let x equals 5. So here we are assigning that x- our- we are binding our x to 5. Then to prove that, we can just println. So the value of x is 5. Now let us now say that- let us now make x- we are changing our x to 6. And let's check, let's check our value and say that the value of x is, is this again. Now we are going to, we are going to run that, we are going to build that. So let's, let's look at something. If you try to, to build it, scav.build, so you can see that we have an error. Now what's the error? It says cannot assign to an immutable value. Cannot assign to what? An immutable value. What does immutable mean? It means it cannot be changed. So since this one, it's is, uh, is immutable. So we said that variables are immutable by default. The first thing is they are immutable. So which is that if you want to make it to be mutable, then you have to be explicit. And how do you make it- how do you make it mutable? We add the keyword mut in front of it. So adding this mut keyword in front of it is going to make it what? Mutable. So let's try that again. Okay, so in this case now, on next- okay, sorry, there's a mistake here. We are returning void, not- so if you should build it now. So let's run it. What's happening? So, Scarp, Scarp, Chiron again. Oh, I'm not supposed to add this. So you can see that in this case, the first time the value of x is 5, and then x is now 6, which means that it has changed it. Do we understand that?

69:37 B: Yes.

69:37 A: Yes, but I have a question still, please. All right, please go on. What is the importance of that print line? I don't know what it is doing here. The importance of what? Print line, that print line.

69:55 B: Print line? Yes.

70:01 A: Yeah, it's the one that is giving us this output here.

70:10 B: Okay, okay.

70:10 A: It's like stdout, um, standard output. You know, this is the macro. Inside of it you have the standard output. So it's because of this that we are able to see this in our terminal.

70:23 B: Okay.

70:25 A: All right, yes.

70:34 B: When you're printing it, you need to pass input parameters or you can just pass one.

70:37 A: In my-

70:40 B: When you're printing, when you're printing, you need to like pass that the value and like the statement and the value, or you can just pass one.

70:50 A: Okay, okay. So do you mean how is he able to get this x into this place, right?

71:00 B: I'm like, is it necessary?

71:03 A: Is it necessary? Yes, to pass it here.

71:08 B: Yes, 2 variables, or I can just pass in one, like I can just pass x. So do I need to have something that describes the value of x?

71:25 A: Uh, I don't get your question.

71:27 B: Um, like when you're printing the line, can I just pass in println x?

71:33 A: Oh, okay. Do you mean something like this?

71:40 B: No, I mean, can I do the x? Yeah, maybe, or x alone instead of like doing a whole string.

71:50 A: Oh, it's just to make it verbose, right? It's just to make it verbose. All right, so we now know how to, you know, uh, make a variable mutable. Now the next one, we have what we call constants, right? So like immutable variables, constants are values that are bound to a name and are not allowed to change. But there are a few differences between constants and variables. First, you're not allowed to use mut with constants. Constants are just immutable by default. They are always immutable. Do you get? Ah, some people are sleeping.

72:33 B: We get.

72:39 A: We are here. We are here. Okay, so const keyword instead of let keyword. You make use of the const keyword instead of the let keyword, and the type of the value must be annotated. So when you want to pass a constant, you make use of the const keyword. You have to specify the- so for example, now if I want to- let's say I want to make use of- I want to use constants instead of this. I'm going to say Number 1 is I'm going to use const. Number 2 is I cannot use muts. Number 3 is I have to specify, I have to specify that, okay, this is uint8 or something. I have to specify that this is uint8. And Yeah, so let's try this. It is not- it's not going to run, or will it? Is this program going to run? It's not going to run. Why?

73:48 B: No.

73:48 A: Why is it not going to run? Because you already made it constant. It is- you can't assign it. Constant. Thank you. So let's, let's try it first. Print line unknown. Oh, what's this saying? x equals 6. Like, why is x equals 6 again? It's a constant. So let's delete this. Could not compare variables due to error. What's the error? Skip token expected. Const cannot be inside a function block. It should be outside of it. Oh, so the value of x is 5. So yeah, constant variables can be declared with any usual data type, including structs, including structs, enums, and fixed-size arrays. So here we have const 1 hour in seconds. Yes, so one of those things also, one of those conversions is that you write constants in capitals. So you write constants in capitals. So 1 hour in seconds. This one, struct instance, enum instance. So this one you have, you can see any struct equals to- this is a struct, this is an enum, this is an enum. Any, you have the any struct, you have the enum. So yeah, this is an array. So you will see that for each of them, the data type- we have the const, the name of the variable in capitals, the data type, then that constant. So nonetheless, it's possible to use const evaluate int macro to create a constant. So this one, you can use this to create a constant. Well, the best is just to It's just to, um, use it that way. So yeah, we want to talk about shadowing. How many of us understand what shadowing is in- what is shadowing in Solidity? Before we move here, what's shadowing in Solidity?

76:53 B: You said?

77:00 A: What is shadowing? Yes. Okay, so shadowing deals with the variable name, right? So it takes precedence when it is in the function block compared to when it is in the state level. So now, so when you have the same the same variable, the same variable name in, um, in, uh, in the contract, right? So the one in the function takes precedence over the one in the state. So it shadows, it shadows the one in the state. Okay, okay, yeah. So shadow, Shadow simply means when you have functions that have the same name, right? So when you have variables that have the same name, so it's- that's the meaning of shadowing. So yeah, they said that variable shadowing refers to the declaration of a new variable with the same name as the previous variable. So can we not say that the first variable is shadowed by the second, which means that the second variable is what the compiler will see? When you use the name of the variable, in effect, the second variable overshadows the first, taking any use of the variable name for itself until either it itself is shadowed or the scope ends. Now let's look at what we have here. Let x equals 5. Let x equals x plus 1. You know, if- let me delete this. When we say let x equals 5, I will say- if you say, uh, x equals to x plus 1, now this will not go because we are reassigning x. And if you want to be- if you want to make this to work, we can do 2 things. Number one is either we can change this to a mutable variable, or we can shadow it. So shadow it- shadowing means that we are going to what? Redeclare it. Now, redeclaring it in this case means that this x that we have here, which is 5, we are now using it again here, but then assign it It's- it is now assigned to another thing there. So let's print line and say that, yeah, the value of x here is this, and the value of x here is this, right?

80:00 B: Yes.

80:01 A: So What does that give us? So you can see that the value of x here is 5 and value of x here is 6. The reason it works here is because of this let keyword. We are declaring this same name and it shadows the first one. Now in this, in this case also, when you don't have- when you have these curly brackets, you are declaring another scope. When you have this, uh, what's it called, you are declaring another scope. So in this scope now, we are, we are shadowing that value again. So let's have, let's have that. So we declare a scope and we let x equals to x times 2 in this case. So the value of x in the inner scope is this, the value of x in the outer scope- now what do we think the value of x is going to be in this inner scope, and what do we think the value of x is going to be in the outer scope? Can someone, can someone tell us what that would be before we run it? Yeah, you are sure of yourself? The inner scope will be 12 while the outer scope will be 6. Uh, how will it be 12? x is starting from 5. Yes, but you still- we still redeclared it as x is equals to 5 plus 1, so it's going to go into the curly bracket as 5 plus 1, that's 6, and it becomes In the curly bracket, it's now redeclared as 6 times 2, but outside the curly bracket is still going to be in the parent scope because the curly bracket is more like a child scope compared to the one outside. So I think it's 12 and 6. 12 and 6. Okay, let's see. So the value of x in the inner scope is 12. And the value of x in the outer scope is 6. Thank you very much for that. Now it says that the program first binds x to a value of 5, then it creates a new variable x by repeating let x equals taking the original value and adding 1 to it. So the value of x is then 6. Then within an inner scope created with the curly brackets, the third let statement also shadows x and creates a new variable multiplying the previous value by 2 to give x a new value of 12. When the scope is over, the inner shadowing ends and x returns to being 6. When you run this program, we have this. So you answer that shadowing is different from marking a variable as mute because we get the compile-time error if you accidentally try to reassign to this variable without using the let keyword- by using let. So I think I've explained That's okay. So we have another quiz. Who is going to take this quiz? I think maybe we should have someone to sacrifice for this next one. You are so sure of yourself. Don't go and try it before coming here. Okay, Oluwafemi Ayoola, are you in this class? Okay, yes, sir. Yeah, answer this question. Who wants to be a pioneer? Which syntax creates a variable that can be reassigned? Option A, let minuteX equals 99. Option B, const minutesPerHour equals 60. Option C, let y equals 10. The time starts now. Let y is equal to 10. Let y equals 10. That creates- yes, sir. If you got it, you can be a 1,000- I'll be 1 millionaire. I'll be- how is it called? Who wants to be a millionaire? So that's your answer, right? That's- that's- yes, sir. Are you sure before we move ahead?

84:27 B: Yes, sir.

84:28 A: It's the first-

84:29 B: it's the first option.

84:33 A: No, it's the first. Thank you very much. Who is that? The first one. No, so we have 2 people on the turntable. Who is the second person? The first option. All right, we will take note of that, that you said it's the first option. So number 2, who is taking the first? Maybe you should take the first one here, then Ayoola is going to answer his own. What will be the output of this program? You have let initial equals 9, let initial equals this, and let initial equals this, and final balance is initial balance. What's the answer? The program must compile.

85:16 B: 6. 6.

85:21 A: Okay, what's Victoria saying?

85:27 B: 6. 6.

85:32 A: All right, so the first one, the answer is incorrect. Let y equals 10 is not correct. Let y equals 10 is not correct. Number 2, final answer is 6. So let's try number 1. Let's try this with your last answer. So you see that which syntax creates a variable that can be reassigned? Eh, fine word. A variable that can be reassigned. Let's mute x equals So this mut here, it means that it is mutable, you get. It can be changed. Okay, so let's mut x equals what?

86:21 B: 99.

86:24 A: Then the next one is data types, right? So data types, you know, in, in normal programming language, there's something called what?

86:35 B: Types.

86:35 A: data structures and algorithms. So you start with the fundamental data structures, then you build up on it. Now, in Cairo, it's that every value in Cairo is of a certain data type, which tells Cairo what kind of data is being specified, so it knows how to work with that data. This section covers 2 subsets of data types: scalars and compound. So keep in mind that Cairo is a statically typed language, which means that it must know the types of all variables at compile time. The compiler can usually infer the desired type based on the value and its usage. In cases where many types are possible, you can use a cast method where we specify the desired output type. So let's look at this. So we have let x- x here is what? Float 252.3. And y is what?

87:26 B: Int32.

87:28 A: x.tryInto unwrap. Let me copy this and try to explain what I can. Doesn't go. So we say let's x, so is felt 252 equals to 3.

88:03 B: And what-

88:08 A: let y, so y is unit 32 equals to x dot .unwrap. Now, is this going to work? Yes. Why? Here we have 3. We are assigning 3 into what? Into x, which is filled to 5. So You know, I've said something before, that the basic data type on Cairo is Felt252. Felt252 is the real data type on Cairo, which means that all those uint8, uint32, uint this, uint that, all of them, they are derived from Felt252. And Felt252 is also used to denote short string. And when you are using it to denote short string, what you are simply doing is you are converting that short string, huh? You are converting that short string into number. It will convert the short string into number. That's what it is, what it is basically doing. So let's, let's try this. Does it work? Yes, it works. The value of y is 3. Now this, in this case, now we are- we have float 252. We want to cast. When you say cast, it's not- when you say cast, cast here, you know, you are casting it into another thing. So the data type of this x was previously float 252, which is big. You are now casting it into something small. So we have to use try into, uh, we have to use try into. try_int2 is like if you are- if you want to convert from, let's say, uint16 to uint8, right? You know, there are some possibilities where it's going to work and there are some possibilities where it's going to fail. So when there are some possibilities where it's going to fail, that's when you use try_int2. But if there is no possibility for it to fail, if let's say you are converting from uint8 to uint16, that one you just need to use pass and it will go. Or you can just use- I think int2 also works. Now scalar types. A scalar type represents a single value. Cairo has 3 primary scalar types: floats, integers, and booleans. So you may recognize this from other programming languages. Let's jump into how they work in Cairo. So float type in Cairo, if you don't specify the type of a variable or argument, its type defaults to float element. When you hear the word Felt, the Felt stands for what? Field element. The Felt stands for what? Ah, they slept. Field element.

91:10 B: Field element.

91:12 A: So it is represented by Felt 252. In, in the context of Cairo, when we say a field element, we mean an integer in the range of 0 less than or equal to x less than or equal equal to p. You see that there is no negative value here, where p is a very large prime number currently equal to this. So this is the very large prime number that is used in, um, in Cairo. When adding, subtracting, or multiplying, if the result falls outside of the specified range of the prime number, an overflow or underflow occurs, and an appropriate multiple of p is added or subtracted. So when you say that something is a field element, There is basically no, um, no overflow, underflow actually. Okay, there is underflow, overflow actually. So, but what we mean by underflow, overflow is that when it gets beyond the largest value, the mode value, it wraps around. So let's say that the p is 5, when you get 6, instead of writing 6, you write 1. So you have to be you have to understand how it works. So the most important difference between integers and field elements is division. So in division, uh, that's where the problem is. So in integers, right, we have int8, int16, int32, int64, int128, and int256. Use size is also Um, uint32. So each variant has, has an explicit size. So we have, um, this subtract 2. If you want to subtract 2 uint8, right, if you subtract 2 uint8, you get at most uint8. So all integers, all, uh, integer types previously mentioned fit into float252 except int256, which needs 4 more bits to be sorted. Under the hood, int256 is basically a struct with 2 fields. So you have 2, uh, with low and high. So Cairo also provides support for signed integers, so which is the i. These integers can represent both positive and negative values with sizes ranging from i8 to i 1 to 8. Each signed variant can store between this and this, where n is the number of bits I want to use. So you can write integer literals in any of the forms shown. Note that numbers which are- that can be multiple numeric types allow a type suffix. So if you have 57, you can have type suffix. So this will specify So let's say, let's look at something in our- so if I say let x equals to 5, in this case now, my data type of my, of my x is failed because I did not specify. I did not specify. So if I want to make it to be a uint8_t, now you see I just have to do this. Now let, let me do something here. Is this, is this possible? So let's see if this is going to run. You can see it does not run because that value is more than uint8. Uh, variables, we have passed this. So data types, so we've talked about that, we've talked about- so numerical operations Numerical operations, we have-

95:24 B: oh, sorry, another question.

95:28 A: Yes, what's that? Can you expand more on the difference between division and integer? Ah, okay. The difference between division in, um, in a field element and an integer is that for a field element For a field element, when you, when you divide, when you divide 2 numbers, you are going to get another field element. When you divide 2 numbers, you are going to get- that one is not easy, it's a bit of mathematics. Like, if I start explaining, it might get more confusing. So for example, let's say, and Firstly, you want to- we- you need to firstly understand what is even the meaning of a field element. So for you to understand what a field element is, these are the things that you need. You need to understand what- there are some things that we call, um, what's it called itself? So we start from, uh, a group. Is it even a group that I start from? Yes, we start from a- we have what we call morphemes. We have group, we have magma, semi-group, then we have field. So we now get to a field. So a field element- listen, number 1, a field element is an element that when you have, uh, sorry, we start from structure. A field structure, it has, uh, it has some binary operations. it is closed, it is associative, it has inverse element, it has identity element. So when you have all of that, then you now have- for a field element, you need to- this thing, you have it over 2 operations. So for the other one also, it has, uh, identity, inverse, and, uh, field elements, uh, inverse, and it has identity element inverse element, and it's also closed. So when you have that, then you can have a field element. So for a field element, for example, now if- I mean, if I'm doing, uh, for a field element, it's going to mostly be in a, uh, a prime number. Like, you have to be- it has to be to the modulus of a prime number. So let's say that we are taking a prime number of 7. So if I ask you that what is 2- 2, which is a field element in prime 7, and 7 and 5. So if I ask you that what is 2 divided by 5, in that case, what is 2 divided by 5? So it might require some calculation for you to get it. So in that case, now 2 divided by 5 is going to most likely be Ah, it's going to require some calculations for me to get it, but 2 divided by 5 in- for integers, in fact, not even for- not even integers now, for a real number. 2 divided by 5 for integers, it does not have a value. 2 divided by 5 for integers does not have a value. For, um, for real numbers, 2 divided by 5 is going to be 0.4 for real numbers. So I don't want to explain it too much so it will not get too confusing, but if you need the explanation, maybe, maybe later, or you can read more on it actually. So Sum, we have- you can sum 2 numbers, 5+ 10, get difference, subtract, multiplication, times, division. So the division here is called- this one is quotient, and this one is called what?

99:31 B: Remainder.

99:33 A: Right. So each expression in these statements uses the mathematical operators and evaluates to a single value. So the Boolean type, we have f, uh, function main, let c equals to true. We have the true, so it can either be true or false. So string types, so string types, we have short string. So this, so they are telling us that a short string is ASCII string. where each character is encoded on 1 byte. So this one represents this, this one represents this. So currently uses Feld252 for short strings as the Feld252 is on 251 bits. So a short string is limited to 31 characters. So, and you can also see that you use just 1 quote, you don't use double quotes to represent string literal. So byte string. So this one is a byte string, long string, byte string. With the byte string, shorts are left. Typecasting. Typecasting is simply moving from one, uh, type to another. Like when you are moving from one type, you want to move a variable from one particular type to another type. That's typecasting. So let's see what's happening here. So if you have a float 252 I want to convert it to uint8. So you know that all of them, they are still numbers, so you can actually like convert. So but in this case now, we use try into. You use try into when you are converting from an higher to a lower. Do you get? From an higher to a lower, and there is possibility of what?

101:21 B: Of error.

101:23 A: So that's why you put this unwrap, but you can handle the error better later. talk about it, how to handle the error better later. So, uh, the next one we have from unit 8 to unit 16. You can see that this is from lower to higher, so you just use into, and that goes from higher to lower and from lower to higher into lower to higher into lower to higher into. This one felt- well, 252 is actually lower than int256, so use int2. So this one is bigger than this, so you use try int2 and use unwrap, or you can handle error better. So we also have the tuple. So the tuple is just like an ordered list of elements. So, and you can specify, you can see here we are, you can have it as a compound data type. So you have int2, int32, int4, and the bool, like that. And one good thing is also you can destructure a tuple. You can destructure tuple elements. So here x will be equal to 500, y will be equal to 6, and z will be equal to true, as you can see here. So then the unit data type So the unit data type is also like, is also like the nothing. The unit data type is a type which has only one value. This. So this unit data type that is here, you might not understand, understand why it is called unit very well right now, but just understand that yes, this is called unit data type, and you can also call it, you can also call it what, void. So you use it to represent when something is returning nothing. So we have the quiz, Who Wants to Be a Millionaire? So he's going to be taking this quiz session with us. All right, is Daniel here?

103:40 B: Daniel.

103:41 A: Okay, he's not here. He has slept. How about Aguela? Aguela Joshua? Okay, can someone volunteer? Someone that hasn't done before? Okay, Mary wants to be a a million naira. Let's start. Question number 1, the largest number represented by the type I128 is what?

104:13 B: I'd say B.

104:17 A: Is what? B.

104:20 B: What? The last option.

104:25 A: 2 raised to the power 127. minus 1. Okay, yeah, what we have when computing x minus 1? What's the answer?

104:47 B: I'd say it will be 20.

104:53 A: It will always This one, Abby?

104:56 B: Yeah.

104:58 A: Okay, determine whether the program will pass the compiler or not. So look at this, will it pass? Does it compile?

105:12 B: Yeah, yeah, it will compile.

105:14 A: What does it return? What will it- what will it print in this std::out?

105:22 B: 2.

105:23 A: Are you sure?

105:23 B: Yeah.

105:24 A: Okay, so the first one you are correct, the second one you are correct, but the last one, the program does compile, is wrong. Why? It should return x. It should return x.

105:51 B: Why?

105:51 A: The program is not going to compile. That's just the answer.

105:54 B: Why will it not compile?

106:03 A: The program will not compile. So is that- it's looking like 2 does not exist. It's just a felt 32. It is felt 252, not 32.

106:21 B: Okay, okay.

106:23 A: Then we have function. So functions are prevalent in Cairo code.

106:38 B: You've got-

106:38 A: Make case as the content, like that, like that, like that. So we define a function in Karel by entering fn followed by a function name and set of parentheses. The curly brackets tell the compiler where the function body begins and ends. So you can call any function we've defined by entering its name followed by a set of parentheses because another function is defined in the program. It can be called from the- from inside the main function. Note that we defined another function before the main function in the source code. You could have defined it after as well. Cairo doesn't care where you define your functions, only that they are defined somewhere in the scope that can be seen by the caller. So let's start a new project with scab named functions. So we don't really need to start another project, but let's just call another function like this. So we can actually just write- how do we start a function? fn, right?

107:56 B: Another- Yes.

107:57 A: Another what? Function. So, but in this function, we want to take it- taking an argument. So we are going to take in an argument of field 252, right? And we are going to Let's just show it here.

108:42 B: Wow!

108:42 A: So we can call this function in this place. So another function like this, right? And run it. So from this main, it calls this function outside here. So I know you know how to call functions already. Then now in a function, we have 2 types of functions. We have what we call a statement and an expression, right? So function bodies are made up of a series of- function bodies are made up of a series of statements optionally ending in an expression. A series of statements optionally ending in an expression. After that, so far the functions we've covered haven't included any ending expression, but you have seen an expression as part of a statement. So a statement is, you know, when you have 2- you can have 2 expressions that are connected together. So that's a statement. So for example, an equal- an equation is a statement that shows that 2 or more expressions are equal. That's for you to know, that's like a statement. So when you have more than one expression connected together, that's a statement. So because Kaggle is an expression-based language, these are important decisions to understand. So statements are instructions that perform some actions and do not return a value. Expressions evaluate to resultant value. Let's look at some examples. If you have this function, let y equals to 6. So it's saying that function definition are also statements. The entire preceding, um, example is the statement itself. So this let x equals to let y equals to 6. So this let y equals to 6 here, it makes y equals to 6. So it's not saying let x equals to that y. So when you run this program, you say that it is missing a token. So it's missing a terminal token. So unused variable, unused the y is not used. Okay, let y equals to 6 statement does not return a value, so there isn't anything for x to bind to. So it is different from what happens in other languages such as C and Ruby. So let x equals to y equals 6, both x and y have the value 6. That is not the case in what? In Cairo. So in this case now we say let y equals to this. This is an expression, this is a statement is an expression. So this expression x+ 1 is returned. So y will now be equal to x+ 1, which is 4. So the value of y here will be equal to 4. So functions with return values- whenever you have a function that returns anything, you have the data type of what is returned. So we have a quiz again. Who wants to be a millionaire? Who is volunteering? Anybody? Nobody? Okay, we have Bolaji Mubarak. Is he here? Ah, he's sleeping. Matthew Ajayi. Matthew Ajayi. Wait, can someone who is online actually volunteer? Nobody? Okay. Abel. Did I get the spelling? Did I get the pronunciation right? The keyword for declaring a new function in Cairo is what? What's the keyword?

112:53 B: fn.

112:56 A: Is that, Abel?

112:59 B: Yes.

113:01 A: All right. Determine whether the following- the program will pass the compiler. If it passes, write the expected output of the program if it were executed. Is it going to pass? And if it passes, what's going to be the output?

113:23 B: 0.

113:26 A: The first question, will it compile? Yes, it passes, it compiles. And it returns 0. I hope you did not test it. No, I'm looking at it. block like this is what? When you have a curly brace like this, you know what the curly brace is? Yes, it's a statement. It's a statement. Statement. A statement. That's true, right?

114:03 B: Okay, wait. Okay, true.

114:06 A: 1 and 2. There's no 1 and 2. 1 and 2? Are you sure? There's no 1 and 2. So, uh, let's go for 2 only. 2 only, the statements. Okay, number 4, determine whether the program will pass the compiler. If it passes, write the expected output. Is this going to pass? I don't think this- I don't know, I'm not sure this is supposed to pass, so it does not compile. The first one, fn, correct. The next one, the program does compile. So the program is not going to compile. Why? The program is not going to compile. Why?

115:18 B: I think it's because of line 1, like the way you were doing it before, where you were adding like x then colon, the type U or something.

115:28 A: Yes, the type of this x. So what's the type of this x? So it's not going to compile. Uh, you're supposed to put the, the type. The next one, this, it is a statement. It is not- uh, this is incorrect. It's not a statement. This is a scope. No, but it is not 3. There's no 3 only there. So it's, um, it can be, it can be a return expression also. Determine whether the program will pass the compiler. This is going to, is going to, um, is going to pass. So it's an expression and it's synthetic, uh, scope. Yeah, so the program will compile and it will return 3. So it does not compile. So the reason is because of parameters. It needs arguments. Okay, so comments. Comment is very simple. This is how you comment. It's similar to, you know, multi-line comments. You can also comment this way. You can also comment this way, right? So what is the syntax for introducing comments? It's like this, right? What's the syntax for introducing comments? It's hash.

117:08 B: What's it?

117:18 A: All right, so control flow. So the ability to run some code depending on whether a condition happens. So that's the control flow. So the if expression, look at- so in the if expression here, you just have if You cannot put it- putting- you have if, then the condition. And number 2- number 1 is that the condition must return true. Like, it must return true. You cannot, you know, in JavaScript you can just do if number- no, it must return a boolean. So if number is equivalent to 5, not if number, do this, else this. You also have the else if. So let's- so look at this one now. Is this going to compile? Let's check. If you should run this. So, so it's not compiling. So this one you have to say if this equals to 3, then it runs successful. All right, so handling multiple conditions. Else if. So if, else if, else if, then else.

119:02 B: Huh.

119:03 A: Then we also have if in let. So you can say let number equals to if condition. So which means that the return value of this thing, you are assigning it into this, into this number. And we have the repetition with loop. So you can have loop here. You loop- when you have this loop, it should continue unless you have a break value. So we have the loop. Uh, you can also return values from, from loop. So let result equals to- then we have the conditional loop, which is the while. While this is greater than this. So please, let's just take it. So determine whether the program will pass the compiler. If it passes, write the expected. So if you look at this, let x equals y, let y equals x. Now the problem here is what? x, x what? You have to- you need, you need- when you, when you are making use of the if You need it to return. Data type here is uint, not boolean, so it's not going to compile. What happens in, in Cairo? If, if an if condition evaluates to true, the program skips the if block. So the program executes the block of code associated with the if. So the 2, uh, this is the condition. What is the result of running a Cairo program with an if statement where the condition evaluates to false and no else block is provided? When it returns to false and no- so the program panics.

120:52 B: No.

120:53 A: No. So the script skips it. So what feature does Cairo provide to repeat until a specified condition is met or gas runs out? So use loop or while. So, uh-uh, you should clap for me now. I cannot see it.

121:23 B: Thank you.

121:31 A: The next one, we have the common collections. So when you hear common collections in, uh, in programming language, what you are referring to are arrays, right? Collections are, is, you know, all those linked lists, array, all those ones. So Cairo provides a set of common collections type that can be used to store and manipulate data. So these collections are designed to be efficient. So we have 2 arrays and dictionaries. So an array is a collection of elements of the same type. You can create, you can create and use array methods by using the array traits from the core library. An important thing to note is that arrays have limited modification options. Arrays are in fact queues. Do you know the meaning of queue? What else? What's the meaning of queue? Can anyone explain the meaning of queue here? Oh, most of you don't. Yeah, yes. What's queue? Queue is just like, um, you see, um, first in, first out. Yes, so first in, first out. Well, maybe that doesn't work in Nigeria. So when you're in a queue, if you are the first person on the queue, you'll be the first to attend, first person to be attended to. So we have queue, we have stack. Stack means what? First in, last out. So whose values can't be modified? This has to do with the fact that once the memory slot is written to, it cannot be overwritten but only read from. You can only append items to the end of an array and remove items from the plot. So creating an array- creating an array is done with the Array.create new. Here's an example. So you have let mut- this mut, it means that you can, you know, modify it. a.append, you are appending 0, appending 1. So when required, you can pass the expected type. You can pass the expected type. So Updating an array, a.append. You can use append to update it. When you want to remove from it, use pop, pop front. So use pop- when you use this pop front, there are 2 possibilities. It returns an option. So option means it can either give you a sum- like when you use option, the option is just an enum with some value. And so some means that it has something in it, and None means it has nothing. So when you use this unwrap, it means that you should panic if there is nothing inside it. So the above code will print the first value of, uh, this is 10. So reading elements from an array. So you can get, uh, to access array elements, you can use get or at. So using array.indexOf is equivalent to using the substitution this. So these 2 are the same. The get method. For the get method, the get function returns options with the box pointing to the reference, which means it returns an option to a box type, char pointer, containing a snapshot to the elements at the specified index. if that element exists in the array. If the element doesn't exist, get returns None. This method is useful when you expect to access indices that may not be within the array's bounds and wants to handle such cases gracefully without panic. So we have let immutable_dict equals this. Let index to access, we have 1. Match array.get, we have 20 sum when it returns some. Now this match, we have not talked about it. Use match to handle different, uh, return values. So since we know that this i.get is going to return some or None, so this match is going to help us when it returns some value, it should give us this, but if it returns None, then it should do this. So we've talked about the@ it's going to return as a particular distance. Then we have the array macro. This array macro is going to- instead of writing array create new, append 1, append 2, append 3, you can make use of this macro to, um, to get value. So storing multiple types with enums. So you can make use of Uh, enum to store, uh, different types. We have data, we have integer, float, tuple, like that. Now this thing that we have here, they are also what? They are also macro. This thing that we have here, they are also macro. So it's going to implement copy and drop. We still talk about, um, traits and generics. So we also have span. Span is a struct that represents a snapshot of an array. It is designed to provide safe and controlled access to the elements of an array without modifying the original array. So span is particularly useful for ensuring data integrity and avoiding borrowing issues when passing arrays between functions or when performing read-only operations, as introduced in references and snapshots. So all methods provided by array can also be used with snap and span except for the append method. So to create a span, you use- you call the span. So yeah, which call to the find utility function will cause a runtime panic? So we have We have this mutable v span this. So we know that from span here, desired to save and control access to this thing without modifying the original array. Span is particularly useful for ensuring data integrity and avoiding borrowing issues when passing arrays between functions. So which means that anything that requires muting, uh, what's it called, Mutability. So find it here. And let's mute i equals to 0. Let's mute i equals to 0. Then you loop From if i to till break. If i equals to till, else this. So in this case now, it is starting from, uh, it's going to start from this. n is 3, till is 3. What is n? So since we have 3 things here This one will most likely give a- determine whether the program will pass the compiler. So this is an array, you're accessing at 0, self.s.append. So this one now is a snapshot, it shouldn't pass. Determine whether the Wait, yeah, that could have meant dereferencing. Determine whether the program will pass the compiler. This- this i plus or equal to 1. Does that compare? Oh, so it compares. Okay, oh, it's the last one. If 2 equals to 4, then for an array of length 3, plus it returns 4. Okay, this, it does not compile. Then we have the dictionary also. So dictionary is just like the mapping in JavaScript. So you can insert- so you have- you insert this into this. You can also get the value. So stuff like that. Scorching dictionary. Let's try and finalize. Let's- okay, let's talk about the ownership model. So, uh, understanding Cairo's ownership system. Now Cairo is a language built around a linear type system that allows us to statically ensure that in every Cairo program a value is used exactly once. So this linear type system helps prevent runtime errors by ensuring that operations that could cause such errors, such as writing twice to a memory, are detected at compile time. This is achieved by implementing an ownership system and forbidding copying and dropping values by default. So, so that Cargo uses a linear type system. In such a type system, any value must be used and must only be used once. Used there means that value is either destroyed or moved. So destruction can happen in several ways. variable goes out of scope, a struct is destructured, explicit destructuring using destruct. So moving a value simply means passing the- passing that value to another function. So when you pass a value from one place to another function, it means that you have moved the value. So this results in somewhat similar constraints to the raw ownership model, but there are some differences. In particular, The whole ownership model exists in part to avoid data races and concurrent mutable access to the memory. This is obviously impossible in Cairo since the memory is immutable. Instead, Cairo leverages its types, its linear type system, for 2 main purposes: ensuring that all code is provable and thus verifiable, abstracting away the immutable memory of the Cairo VM. So ownership in Cairo- ownership applies to variables and not to values. Ownership applies to variables and not values. A value can safely be referred to by many different variables, even if they are mutable variables, as the value itself is always immutable. Variables, however, can be immutable, so the compiler must ensure that Constant variables aren't accidentally modified by the programmer. This makes it possible to talk about ownership of a variable. The owner is the code that can read and write if mutable. So this- that means that variables, not values, follow similar rules to Rust. Number 1, each variable in Cairo has an owner. Each variable in Cairo has an owner. So the owner is usually the- what? The scope. There can only be one owner at a time. There can only be one owner at a time. So which means that when you move out of a scope, when you move to a different scope, you move the owner. So when the owner goes out of scope, the variable is destroyed. So when the owner is destroyed, the variable is destroyed. So now that we are pass basic characters that we want to include all these. Okay, you understand? So in this case, now we say that let x equals to what? Hello. Right now, if you have- let me- let's write this for our own so that we can follow it along. So here we have fn main. So we say let s equal to hello, right? So in this case now, this variable s, the owner of this variable s is this scope, this main scope. Now, I know that the variable x refers to a chosen variable, is valid from the point at which it is declared until the end of this code. So where does this- it is valid from this place up to the end here, up to the end here, which means that anything- if you should, from this place now, you should say print line and you put x here, it is not going to work. Because number one, the S is not even used there, then it is already out of scope at this point, right? So then We now- let's say that we now create another scope. You know, this is a scope here. Before this scope ends, you now create another scope here. And this scope, we now say that let x equals to hello, and we do stuff with it. And then, so if you should say no Let's use another variable here. So let's say that let m equal to world. Let m equal to world. So in this place, we can print m here. But by the time we get here, uh, let's try to print m again. But at this point, m is already out of scope. So at this point here, m is already out of scope. That's the reason we had this- we had this error. m is already out of scope. So a variable has scope in which it is valid. In other words, there are 2 important points here. When x comes into scope, it is valid. It is only valid until it goes out of scope. So at this point, the relationship between scope and when variables are valid is similar to that in other programming languages. Now we build on top of this understanding by using the array type we introduced in the previous one. So So let's say that moving values, we have let immutable this equals to this, we have this, then array.append, we append 1 and 2. So here we now have a function here. Let me- we are going to write that. So we have let mutableArray equals to, you know, we are creating a Array of int32 elements equals Okay, so this is going to like give us a- so let's push some values. Now in this case, we now have How does the type system ensure that the current program never tries to write the same memory to? So we already have this full array, so we have a function here. So if I should try to- so okay, before that I'm going to print, I'm going to print this now before and after, right? So in this place now we know our array, this is our array here. Right, so I'm going to print, I'm going to print it here. After printing it, then we are passing that array into this function, which is another scope. Then we are going to try to print it again. Now we have an error. Why do we have this error? Wait, wait, wait. Okay. Oh, we are supposed to use the- is there anything? Oh, we are using zip expression, so it's append, not Okay, so in this place now we have Adidas Pop Front. Green. So in this case now, in the first instance, it's actually possible that it has even moved it in this midline. It moves in that case also. Oh, okay. That's- you should implement the debugger. Yeah.

143:26 B: Yes.

143:33 A: So this is what I'm looking at. These are- this is the error that I'm looking for. So here we have an error, but must use- so you see that variable was previously moved here. In fact, it did not even get to this place. So from here it has already moved this array. From here, this array, it moved it from here to here. So which means by the time you are calling it here, there is nothing left. So assuming it did not even move it here, maybe, you know, it moved it here. So when it is getting out of this print line, it is get- it is out of scope already. So which means that this array here is nothing. So that's the ownership model. So let me I'm going to delete this. So which means that yes, it entered this first case. But now let's have- let's try to like pass it in 2 times. So saying that variable was previously moved. So which means that in this case now it moved it. There was movement. How is there a movement? We have an array, so it was declared here, which means it's supposed to be valid from here to here. However, when it got to this place, it was moved to another scope. This is another scope here. So when it was moved to another scope, the scope ended here. So coming back to this place, there's no array again. Coming back to that place, there's no array. So yeah, so one of the ways in which we can you know, get away with that is when we are moving something, when we are moving from one function to another, we are moving from a scope to another, if that data type, if it implements the copy trait, if it implements the copy trait, then it's not going to move. Um, it's going to simply copy it. It's going to simply copy it. So an example is if you should have this structs. So I'm going to use, um, let me copy all this. I'm going to like this. So in order to- I'm going to comment this first so that we can see what is happening. So normally this is a is a point. So we created a point here, we moved it, you know, we moved it here into the scope. And you know, it has already- it has left the scope, so there is nothing remaining in this place again. So if you should try to run this code, it's going to tell us that could not compile. Due to previous error. What's the previous error? 3000 implementation in the context P point. Where is P? Fn full P for point. So let's just say print line. Let's see. We are going to print something different here so that, so that it doesn't- we don't want to move it beyond the fact that here Could not compile this because variable was not dropped. Oh, okay, not use the variable. So we say that No implementation for default formatter. Why? Go in out of debug. Oh, oh, oh, okay. So the mistake actually is that the drop trades Yeah, it's only this copy that I don't want it to have. So normally it's supposed to have the drop sheets. So let me just move some. So yeah, it now has it. So you can see that, I see that it's saying that it was previously moved. So that's the error that we have now. So however, when it is moving, when it is moving, if it implements the copy trait- so we still talk about traits and their meaning. So if it implements the Copy trait, then it's going to, it's going to run that successfully. So which means that when it is also moving, another thing that it also checks for is it checks if it implements the Copy. So if it implements the Copy trait, then instead of moving it, it's going to, it's going to copy it. Instead of moving it, it's going to copy it. So we need to, um, take note of that. So do we have questions while we proceed? Do you have questions? No question?

151:10 B: Sorry, sir, I have questions. I'm so lost.

151:14 A: All right. Yeah, what's the question? What's the question? Someone wanted to ask a question. What?

151:55 B: Sorry, I said like, what you did, I didn't understand. Sorry, like if you can run through again. Sorry? Like this how you're doing the Snapchat. Snapchat is not Snapchat. I think that was the last thing you were doing. Uh, this snap.

152:31 A: No, we've not got to Snapchat yet though. I was just talking about the ownership model. Do you understand the ownership model now? First, do you understand? So the number one is that variables, they have owners, and they have just one owner, which is the scope in which they were declared. So which means that all variables, they are valid from their point of declaration till the owner is out of scope, right? So when it moves from one scope to another, so it's going to move. So when it moves, the drop trait- so drop trait is just It's like, uh, in drop trace is just like a way of clearing the memory when you are, you know, when the, when the, when it is, when a variable, when it goes out of scope, it implements the drop trace. So the drop trace is going to clear the memory. It's like dereferencing. How is it called? You are, you know, when you are creating a memory, when you are creating a variable, you are allocating the memory. So when you are dropping it, you are clearing, you are freeing that memory that you allocated for it. So that's what the drop trait is doing. So when you move, uh, when you move something, it's, it moves the ownership of variable to another scope. I mean, the scope, when it ends, then it means that it's going to drop that particular variable, right? So however, however, when, uh, that variable, if it implements copy, if it implements copy, then, uh, instead of moving it, it's going to copy it. So which means that when it goes out of scope you still have that variable. You still have that variable. So, so that's what they are now talking about, the copy array data with clone. So if you do want to deeply copy the data of an array, you can use a common trait called clone. So return values and scope. Let's look at this. We have let a1 equals to givesOwnership. So this givesOwnership here is going to give- it gives its ownership to, uh, you know, ownership of what is returned. It gives it to a1. Now a2 now comes into scope. a3 takes the ownership of a2. No, no, this one, it gives you- it takes the ownership of a2. This function takes and gives back. It takes the ownership of a2 and then give it back to a3. So, and after that, so yeah, a3 goes out of scope in this place. Now a3 goes out of scope and is dropped. a2 was moved. So A2 was moved to this one, so nothing happens. A1 goes out of scope and is dropped. Please, do you have question on this memory, uh, this ownership model? Because I know that's where a lot of questions usually are. So that I can, um, I can try to explain it better. Now, is there any question on it again?

156:41 B: Yeah, question.

156:44 A: All right, what's that?

156:47 B: Okay, so when you say, uh, variable is owned, right, is it the same thing as Block scoped in- Block scoped, like, then let me see, um, the variable is block scoped since the main, the main structure is, um, the function main in Cairo. So it's kind of like, I feel like it's kind of the same logic when you say a variable is block scoped, like, it can only be accessed by that block. the parent block?

157:23 A: Um, so I would have said yes, it is yes. However, you know, we have different programming languages and there are some of them that are garbage collect- that have garbage collectors. So by garbage collector, it means that it's where you are scoping, scoping of variables it allocates and deallocates memory for you, so you are not the one managing it. Like in programming that paradigm, the way it is is that if you are programming a programming language like C and C++, now you have to allocate your memory and deallocate it all by yourself. So which means that when you are bringing a variable into scope, you are allocating the memory for it, and when you are uh, when it is going out of scope, you are deallocating it, you have to handle it by yourself. For JavaScript or Java, they are garbage collectors. So which means that handling, you know, when you bring a memory into space or this, all those things, it is JavaScript, it is the one that is doing everything. Like, you don't think about it. So in- you can put something in a block scope And you are still going to be able to access it outside of the scope in JavaScript. So that's the reason I cannot say yes to, to your, um, to what you've said. So that, that's for garbage collectors. But for this one now, you have- you are the one that you want to define the behavior. So that's the reason it now has those rules. So, and the rules are very simple. when you create a variable, the variable is going to be valid till the end of that particular scope. Till the end of that particular scope. However, before the end of the scope, the variable can be moved. The variable can be what? Can be moved. So when it is moved from that scope to another scope, now in that particular scope where it is, when it finishes the scope there, it is going to be dropped in that scope. It's going to be dropped. So which means that outside of that scope, if you try to access it again, then yeah, uh, that's going to be a null reference. You are going to be making reference to a null, uh, do you get? So it's like that, that's just simply it. This, this one now This is a scope. This is a full scope. This is the main scope. The main scope started from here and it's ending here. This P1 is starting from here. Normally it should start here and end here. However, when it got to this place, it was moved from P1 to this. So when it was moved from here to here, It means that it has- its scope is now from here to here. When the scope ended here, it was dropped. So which means that P1 no longer exists here. P1 no longer exists here. Do you get? So it cannot- it cannot- it doesn't happen. However, I also know that if this P1, if it implements copy, instead of moving it from here to here, it was copied from here to here. So which means that this one copied it. So even when it was dropped, it doesn't matter. This one again copied it. Do we get- do you, do you understand, Victoria?

161:08 B: Yes, yes, that makes sense. Uh, Abraham is asking me an important question.

161:14 A: All right. Uh, when we implement copy trait and the variable is used in another scope and makes changes to that variable, so which value do we get back when we reference it again, the original or the changed value? So when you say that you copy something, what does it mean? You've copied it, right? So which means that it is the copied version that you are using in that other place. Do you get it? So which means that the original is still It's actually something that we can, we can actually try out. So in this case now, so since we have, since this is, uh, you know, we now actually there are some, there are some, this thing that implements copy. Okay, let this one that, okay, this implements copy. So let's say that in this case now We have, uh, so I, I don't want to use something that is going to- I, I, I don't want to use something that is going to like move. Okay, that is going to move it again. So actually everything is just going to like- it should still be 5 in this place. So let's, let's print this first. Let's run this first. So 5, 5, right? So inside this place, let's now say that when we get- we change this p to- so if we change this to p.x, we change it to 10 here. Is it mutable? It is not mutable. So let's say that it is mutable. But so if you now change it to mutable, it's going to be another bug. So let's not change it to mutable yet. So let's not change it to mutable yet. But the simple distinction is that since you copied it, this P1 here is still existing here. So which means that in the next case, at this particular place, this one, it was, it was already dropped. You know, this P1, the P1 that was inside this one, at the end of the scope here, it was already dropped. So it doesn't exist again. It is still this P1 that you are still calling in this place again. So that's, that's the, that's the case. I hope that answers your questions, Abraham. All right, so the next one is now references and what are snapshots. So reference and snapshots, you all know what snapshots means, right? Snapshot is just like when you, when you take a snapshot, when you camera something. So when you come out, when you take the picture of something, can you modify it? Ah, everyone has left. How many minutes? Yes, you want another? I'm, I'm still here.

165:04 B: Yes, yes, sir. No, sir.

165:09 A: So what was the case? You can't.

165:14 B: Sorry, what's the question again?

165:16 A: Well, in a normal- when you snap, when you take the snapshot or something, can you modify that thing?

165:26 B: Like the one I've snapped or the original?

165:30 A: Original.

165:32 B: No, no.

165:33 A: No, thank you. So When you take the snapshot of something, you cannot modify the original. You just have a copy of things that you can just- so when you make reference to something, when you make reference to something, you can, you can still change it. That's the way it works here. So for, for the ownership, this thing, the way it now works is for snapshots, since it is, uh, Since it is a snapshot, it is not mutable, it is immutable. You cannot change it, you get. So which means that you can take 1 million snapshots of one variable, it doesn't matter. But for reference, you can only take one mutable reference and unlimited immutable reference. One mutable reference and unlimited immutable. So in the previous chapter, we talked about how Cairo ownership system prevents us from using a variable after we've moved it, protecting us from potentially writing twice to the same memory cell. However, it's not very convenient. Let's see how we can retain ownership of a variable in the calling function using snapshots. Using what? Snapshots. So in Cairo, a snapshot is an immutable view. It is what? Immutable view. of a value at a certain point in time. Remember that memory is immutable, so modifying a value can actually create a new memory cell. The old memory cells still exist, and snapshots are available that refer to that old value. In this sense, snapshots are a view into the past. Here is how you, you define and use a calculate length. So function. So here we have this array. Now you take the snapshot. You can see that snapshot is using at, at. So here now you've appended it, you mutated this. Let first length equals to this. So the second length equals to this calculate using. So the length of the current where the snapshot was taken, this first name. The length of the array is what? This is just a string. So you see that in this case now, the data type, we have included this@. Whenever you see this@, it means what? Snapshots, as you are taking the snapshot. So 2 things, you can either have this@ or ref. You can either have that@ or ref. ref. So when you are using that@, you can use it for- in a lot of times. But when you are using this ref, you can only use ref once. That is immutable. So you see that we can achieve the behavior we want in this mutable reference, actually mutable values. So the actual mutable references are actually mutable values passed to a function that implicitly return at the end of the function. You turn ownership to the calling context. By doing so, they allow you to mutate the value passed while keeping ownership of it by returning it automatically at the end of the execution. In Cairo, a parameter can be passed as mutable mutable reference using the ref modifier. So in Kyrie, a parameter can only be passed as mutable reference using ref modifier if the variable is declared as mutable with mut. So, so if you look at this one now, you can see that we have ref. We have a ref here. So, and we pass this because this variable is what is mutable here. So we pass this now. So small recap, at any given time, a variable can only have one owner. You can pass a variable value, a variable by value, by snapshot, or by reference to a function. You can pass it by value, snapshot, or reference. If you pass by value, ownership of the variable is transferred to the function. If you want to keep ownership of the variable and know that your function won't mutate it, you can pass it as a snapshot using@. If you want to keep ownership of the variable and know that your function will mutate it, you can pass it as what? A mutable reference, ref. Do we have any question? Do you have any question as regards that?

170:27 B: No.

170:32 A: Hello, any question? No question. Actually, the way I want us to have this class is this particular one we are going to go through. So this is like the first part of it. So the second part of it, we are going to be writing a smart contract using Cairo. We are going to be writing a proper smart contract using Cairo. So we need to learn how the things work here so that- so I believe everyone here can write smart contracts in Solidity already. So we are just going to be looking at how do we convert the mental models we've developed for Solidity see, to write this smart contract here. So struct- now this struct, you can just think of it as a way of- you can call it a structure. It's like a structure. So a struct or structure, yes, is a custom data type that lets you package together and name multiple related values that make up a meaningful group. If you're familiar with an object-oriented language. It's always like an object data attribute. In this chapter, we compare and contrast tuples with structs to build on what you already know and demonstrate where structs are a better way to group data. So we already know tuple, right? If you want to declare a tuple, yeah, so you know that the name of the tuple, then you have all those, uh, the data types inside it. Now, but if you have a, a, uh, what's it called, a struct- look at this struct now. You have struct User, active bool, username, this and that and that. So one of those things that you can do is that, look, let's look at this, this thing that we have here. And this is here. So we have this structure User, we have active username, emails, um, sign in and the likes. So in this place, when we want to create a user, we can say what? User, we create the name of the structure and then we start assigning all of these things. Now another thing that we now have in structure is that in structure we can have what we call what implementations. We can have implementations of what, of structures. So we can have using the- you can, uh, we can have traits. 2 things actually. Traits- oh no, no, we can have implementations. So we can implement some traits on, on structures. Traits are different Traits are just like collection of behavior. So structure, you can think of structure as a body. So you can implement some behaviors on the body. So if, um, the way we have this structure now, we can implement- let me say I can implement, yeah, um, user traits. So For user. And for this one, what I'm just going to do is- in fact, I can implement- let me- I want to implement default. So implementing default simply means yes. So with this default now, it means that I will be able to, uh, let's say if I want to, I can say let's, let's check something. Let user3 equal to- so in this place now I can do user default, or let's now say that if I declare that The data type of this one is user, so I don't even need to, to even put user here. I can just say what's default, default. So let's, let's check something. You want to print line, you want to print user query. Let's see what it's going to give us. So variable- what's the question? Cannot be created from a numeric literal. Username- what's username? Oh, it's by type. Let me change, let me change them to fields. Sorry, um, felt 252. So what's happening? 1, true. User 1, user 2. Trait has no default, no implementation. Oh, I'm supposed to- for if I implement defaults for- no, let me just use user. Or what was this? Debug. Oh, okay, I'm going to use derive debug. Yes, so all this macro that we have here, they also could not compile due to this word unused variable, unused variable, self type not found, type self. So it's And this is time. What's the game? Okay, implement defaults. I think this one is it. This one is off that they make use of. Let me check where is the Cairo brick. Oh, there's no implement here. Cool. Sorry. Yes, okay, it's off.

178:29 B: Maybe-

178:30 A: okay, let's see this. So in this place- oh, I was still using default here. Even at that, still A rock design. Oh, this one is even not a type, not a type. Invalid paths. User 3, user defaults. Okay. Okay, maybe we come back to this implementation when, when we see it in, in the book. So method syntax, an example program using structs. So in this case now we have function area- no, this is not using structs yet. Okay, struct, we have Rectangle. So let rectangle equals to this, area equals to area of rectangle. So this, it takes in- yes, it takes in this rectangle and produces the area. Okay, that's a good example. Another one, so method syntax. Yes, so to define method, you make use of traits. So you have traits, rectangle traits. So fn_area and function area, it takes in the rectangle struct and it produces this type. Okay, so yeah, generate_traits. So actually, whenever you do this, you're supposed to add this macro generate_traits. So let's use the generate_traits in the following. So when we use that macro, it's going to- that's what you use. So the one that I was doing, that one is, um, so you can see, yes, implements, implements, you can implement of this, and it's going to give you It's going to give you all of that. So methods with several parameters, and can have it too. Methods without self parameter. So yeah, methods, methods can have 2 too. So the ones that have self and the ones that don't have self. So for the ones that don't have self, so it means that they are, they are what we call what? They are the ones that we call constructors. But the ones that don't have- the ones that have self, those ones are methods, or is it behavior? Those ones are the constructors. So in JavaScript, there are methods and constructors. So, uh, the last very big, uh, what's it called here, is the enums. So enums too, they are like, they are like Enumeration. So enum, all the directions, all the possible directions you have: north, east, south, west. So enumerating message, quit, echo, move, like that's implementation of enum. So then we can now- when you, you know, when you use enum, you are going to list everything that is associated with, with it. when you list everything that is associated with it. So you can now match. You can use match to what? To exhaust, to exhaust, uh, the thing. So if you look at this message now, are we- yes, this message. So you know that it has kids and kids echo move. So you are using this match to say that if the message is quit, you should do this. If it is echo, you should do this. If it is move, you should do this like that. Then we also have option. Option is like an enum with some or none. So you can also use match on this also. If it is some, you do this. If it is none, you do that. So, uh, that's that one. So the match, we are talking about the match control flow now. So you can see match, the name of what you want to match. You want to match this. If it is this, do this. If it is this, do that. If it is this, do this. So that's- so, and in match also, you can also have multiple. This is the OR operator. If it is this or this, do this. This one is for every other thing. It is either this or this, do this. For any other thing, then do this. So that's match. Concise control flow with if-lets. If-lets. So the if-let syntax lets you combine if and let into a less verbose way to handle values that match one pattern while ignoring the rest. So it's just going to match the one that handles a particular pattern. So let this equals to this, match this one. So instead of doing this, so you can just use if let this, then do this. So while let also So then this one is just about managing your Cairo projects. That's just what this one is about. So these are the major things. These are the major things. Then package- managing your Cairo projects, we have the packages and crates. So, you know, the packages are in the scrap.toml and the isolated code. So when you do scab new my_package, you are going to have something like this. And if you want to define a new module, you just use mod whatever it is and you start. So, and so this one is for inline if it is inside the lib.scalaio. But if it is outside it, you have to, uh, if it is out- no, everything still has to- if it is outside it, look at this one. Now you have src/garden.kairo file. This one, now you still have to declare it again in the, uh, in the lib.kairo file. Even though it's there, you still have to declare it. So you use the use keyword. So we have the src/garden vegetables.pyro, garden.pyro. So when you have this, so use the use garden vegetable, then inside this vegetables.pyro, that's where you have- this one is a struct, this is a struct there. So you can now start using it in this place. So something like that in this place also. So first of all, Hosting, add to waitlist, sit at table. So this is to tell you about like how you can have a module inside another module. You can see that this module now, Front of House, inside it you have 2 modules. You have Hosting and you have Serving. Inside this Hosting, that's where you have these 2 functions. Inside the Service, that's where you have all these functions. So if you want to, this is the structure for hosting, serving. Inside this you have this and this, you have this and that. So, and when you want to reference also, you make use of use. Then you can start from the beginning or you start from a particular place where, where it is known. So look at this now, this absolute path starts from restaurants Then front of house, front of house restaurant is the package name. Then front of house hosting how to waitlist. This one is relative. If you're already in, you're already in this place, so you can call this one straight. You start from front of house hosting how to waitlist. So do you have any question? We are going to- do you have any question?

189:03 B: So far so good.

189:08 A: Yeah. You can further share it. Yes, that's on the snapshots. But the thing is, it is- what you are changing is not the picture actually. It is actually the person that you snapped. So is it possible that, you know, using the picture, you're not like modifying the person's traits? So that's going to be the analogy.

189:40 B: All right.

189:49 A: There is no question. So we would have to, uh, we'd have to stop here. So the exercise, the exercise is basically just to, just to read up on this number 8, generic types. So when you use generic type How do you use it? Then error handling, testing Cairo program, and advanced Cairo feature. So like just this remaining parts. So the next class that we are going to have- so please, if you have any question, you can ask, you can ask any question that you have. So by God's grace, in the next class that we are going to have, we are going to be writing smart contracts. in Cairo. So, and one of those things that I would actually also want you to do is, yeah, those contracts that you've written in Solidity before, just put them- just have them beside you. So you're going to be rewriting them in Cairo, and we'll be exploring the mental models that are used for writing smart contracts in Cairo and Solidity. So why is there no question? Is it that you would not understand everything I'll be saying or what? Or you fully understand, understood everything? Well, which, which is the case?

191:21 B: Okay, for me, I don't- there are some concepts that were pretty new. And it was kind of like feeling overwhelming at some point. But I'm sure if I go back and read what you've said and probably go back and do those quizzes again, I will actually like get it more. But for now, I'm just overwhelmed.

191:47 A: Yeah, of course, you'd- you have to, you have to do your, your assignments too. So that's it. I think there's still- there is enough time between now and the next class. So please, I'm pleased to everyone go through it again. And in fact, the best is, um, the Cairo book. You can see that we are almost even done with it. These other ones, the main- the major parts of them, we've explained them. So generic types is simple, uh, error handling too. is simple testing, advanced Cairo features, and appendix. Then this, uh, this smart contract in Cairo is just like, uh, all those msg.sender, msg.value, and address. These are how you are going to get all those, all those stuff. That's what is here. So let's all, um, go through it, read up on it, and Everything. So that's the end of today's class. In the absence of questions and answers, then we are going to call it a day. Thank you for attending.

193:09 B: Thank you so much, sir.

More from this hub

Something wrong, or a video you want off this page? Write to wethem.xyz@gmail.com and it is fixed or taken down within 48 hours.