 I'm Chiko. Actually, my name is Shinizu, but it's a very hard to pronounce English speaker. So, I'm from Chiko. Chiko is another pronunciation, actually. Today I'm here as a program here. Actually, I'm working at Mojir Japan, and I'm a kind of developer relationship guy, so I have several talks. My job is speaking technical things in front of developers, and actually that's there. I'm here as a program here. Anyway, so before starting the workshop, can I take your portraits from here? Does anybody care about who don't want to be taken picture here? If you care with my hands, I'll try to avoid it. Okay, that's it just for my report. Anyway, that's it. Okay, so quick question. Have you ever... Okay, what about... Is it working? Okay, have you ever written some rust code before? Raise your hand. Okay, so have you ever been like some C called C++? Call the mic yourself. Okay, good. So, most of them you have experience of compiling a code. Okay, good. Actually, I have several experience of the workshop of last programming in Japan, but most of attendees from the JavaScript community, so they don't understand how to compile or something like that. Okay, but they are very nice environment to me. And here is today's goal. Today, we have two awards and first half we are, how to say, overview of our last fundamental things, especially syntax things and last two very troublesome features like ownership. And second half, we are going to implement some unique, traditional unique one, cat. It's a concatenate, actually it's a concatenate, but it shows the content of what will take as far as if to the terminal that we are going to build like the cat command with last. And if we have extra time for after writing a finisher, writing cat command early, so please extend it by yourself. That is today's goal, contents I bring to you. And today's goal, my goal is how to say these things. Okay. Here is my self-introduction, skip. And programming last, so I heard that this is the second workshop, actually last meetup in Singapore, and so all of you may know what last is. But so give a brief summary. Last is a programming language, actually. Originally, actually it is made from Mozilla. Actually Mozilla supported it, give a huge support, but it's not a Mozilla product actually. And it's a characteristics that has a more safety, more improved C++ with more safety program in easy way. And it's also, it is a modern language, so its design is care about the multi-processes. So it's easy to handle thread or something like that. And that is a brief summary of last. And here is, and we can also, last syntax is barrier, is affected by Ruby thing, so we can write a list processing program like this. This is an example code, which is summation of integer values, contained in the, actually some accurate, all positive integer values contained in the list. So in the first one, in this line, V actually is a big array, at least, and we can make a filter, filter out to say, give a light filter with this kind of fire order function way. So last has a closure, and we can define a higher order function in the last. And we can sum all things and return its value as a result. But these, how to say, it is very hard to compile efficiently such kind of codes, but the last comparable is very, has a smart enough to expand such kind of higher order function to the more lighter way. So very efficient. That is a basic, very interesting feature of last, actually. And so last week, before starting at the workshop, we should check out, we need a compiler installation. Have you installed all the last compiler? Okay. So if we have a Wi-Fi connection, actually, I know he has no Wi-Fi connection for us, but if we have some internet connectivity, we can try some last syntax here. It is an online execution environment, so we can like last code and run on the browser. So please write when you get back to your home. Okay. Okay, so compiler skipping. So let's start with hello world. So it's a very boring thing, but it's a traditional thing. So we can start with hello world. So by the way, when we install last compiler, cargo, a command named cargo is also installed, which is a package manager of the last programming environment, like an NPM for Node.js or a bundle in Ruby or something like that for Python. And so we can actually, we do not run last compiler directly, but we can compile the last command, last program via cargo command. So let's just open your time now and put this command. Then we will get the hello world. Cargo, as I mentioned before, cargo is a kind of package manager system, but it also has a functionality to create a skeleton code or template for the last program. And we can create a new last program like this way. So it creates a folder inside which contains these files inside. So we have one cargo file which is a dependency list of the project. And it's also a folder that contains the main RS. It's an entry point. It contains the main entry point inside it. Actually, it is the last command, and cargo creates a hello world already. So we need to make a hello world. We need just all things we should do is a typing command. Cargo also builds our last program by typing by this command, which compiles our program in the debugging way as a debugging release. So its compiled image is contained in this target under the target folder. And it's like this. So to run our build, to run our hello world command, we just run cargo command with run command. So we get our world here. Okay. So did you get it? Okay. So for our world here. So you have already like... So Rusty. Rusty has an entry point. Rusty's main function calls the main. So all Rust programs can start with the main function, actually. And it's a symbol similar to C or C++, actually. And here is a keyword of fn. fn is a short days of function, which means a defined function. So it's a keyword. To define the function, we start with a fn keyword and the function name and the list of arguments and the function body for us. That's the basics. That's the function division. And we have another... We listed about, how to say, we like to see function body inside these brackets. And for now, we have only one line inside the main function, which is a print name. Of which name is a println. Actually, it is not a function, but it is similar to... You can assume that it's a function call of the println function. But actually, it is not a function. It is a macro. So it comparably expands when before building this program. But for now, we can... From the higher level, there's no difference between the function and the macro. So we can think... For now, we can think this is a function call. Okay. So let's be... By the way, is this too easy? Too fundamental? Okay. Let's move on to the variables. So variables... Variables is... Is... How to use variables is also similar to C way. How do we... In C or C++? So before you start using... Before using... We need to declare a function name before using... Before using... Before using it. For example, in this line, we declare one variable of its name. And it is... I'll say... It has a string value. Combined with a string value. And println macro, we can embed... The variable value is here, like this way. So we... Like this... In these brackets, how to say... Embed... The println macro will embed the variable... This value of this variable here. Okay. And this is... For now, the last... The last syntax is... So different from ordinary as C-like programs. But here is a little bit difference. Actually... Last... In other programming language, C-like programming language, equal means assignment of the... Which means some... Some functions... Some variables have own memory space or have some area on the memory, and assignment means writing a value into its memory space. But in the last... It is a little bit different. Value... Exists first. And then we name its value with equal. Symbolic equal. In this first line, we call this first line as variable binding. So it is literally different concept. It's similar to the assignment, but totally different concept in the programming as a procedure. So... And here is the most... First, last characteristic. All last... All values in last are immutable. We cannot make a reassignment like this way. So this program calls a compiler error. So let me see. So we try to... Okay. This program does not use the compiler error because we declare one variable and it's bind to this text and use text in the printed function. So we can... We can compiler learn this program like this. But in other programming language, there are such kind of things that we can write such this kind of code. And we anticipate that this will also learn well. So we... We will... We will expect that when we get... We get two lines. Hello, Chico and hello, Simi. We'll be printed to the console. But the reality is totally different because it causes this kind of compiler error. So this is because the assignment is occurred. Because all rest values are immutable. So when we write this kind of code, that means a mutable has to modify a value assigned to the variable. So that will be that may cause some bugs in the program. So that strictly prohibits this kind of code. So that we would like to do these kind of things. So we have two options. To do that, we have two options. The first option is very simple. We make another bindings by this. So we can... In other programming languages, we cannot use this kind of code. This kind of code. In other programming languages, this code is the declaration of the same variables. But in the last it's treated as a different way. So it is making another bindings which have the same name. Eventually the same name. So it is totally... Inside Rust, this program is treated like this way. But we can do like we can like this way. So so far now we get expected result. That is a very troublesome feature of the last. And the second option to write this kind of code is make values mutable. Decreate a variable that's mutable. So we can declare a variable name to... we can make variable mutable by using this kind of declaration with Mute keyword. With this keyword we can make this value is mutable so this code does not occur. It causes compile error. Okay. That is the last characteristics. So mutab... So... But mutability is a mutable variable causes some bindings. Carefully this kind of keyword. But... Also mutable variables causes some very difficult situation. I say causes more serious compile error. Like I say buy. When we are... For example, we pass this value to the function as a value of arguments or make some other value from this kind of mutable variables. So we have two options. Mutable is very very effective or very easy way easy to understand for C-Programmers. But you need to be careful how to use it. Okay. So that is a variable. So when we use about... When we declare a variable we have to think about name and which it is mutable or immutable. We should think about two things, actually. And here is a function. So let's move on to the function declarations. It is similar to the C functions, but last is affected by functional program languages. So its function signature is like this. So here is let's say these two here they are... This is a list of arguments. So this function has two arguments of each area value of two... This function gets two integer values as an argument. And also this is notation of the term value types. So that means that this add function gets two integer values and returns the integer values. Okay. And the function the function the term value is the last compiler treats the last expression evaluated value of the last expression as the term value functions as the term value. So this a plus b is the last expression in this function. So that means this evaluated value of this expression is the term value of this function. So please... And so you need to see this program carefully. So this line does not end with a semicolon or something like that. So you do not put a semicolon after this expression. Because you know, last compiler, if you put the last... I would give some demo for that. So we can like function like this way. So this program does not cause any compiler errors. Okay. Yeah. So this is run correctly as we expected. But if you put a semicolon after this line like this, it is very natural for C programmers. But when we do that that will cause this... cause compiler error. Type error actually. Because you know, last compiler create this semicolon as a term... terminate of the expression that... just to compare, I expect there are some other... another expression porous here. And we have no expression here. So that means there is a blank blank expression here. And the last compiler thinks this function returns value. And that there are signatures in signature, this function is... returns integer value. So there is a contradiction between the implementation and the signature. So that... that's the reason why this kind of the compiler error. So be careful about how to use semicolons. Okay. Let's say that that function definition is after you write. I don't even see how to put a function prototype at the top. Do we have to do that for last also? Last... last is how to say pull up... actually last pull up this function definition so you can write function after before it's used or after. So that is only my how to say... how to say my custom or custom actually. I'm familiar with this kind of writing. Okay. Okay. And the last compiler has a strong type inference functionality. So in most case we does not need to need to annotate variable types. But we can do that like this way. But how to say type notation of string is very difficult to but so like this way. Yup. Like this way. Okay. Okay. Good. And here is a list of primitive last primitive types. So we have Boolean value types and character values character actually character is different from as a C character in C or C++ because bandwidth is totally different. In C character is 8 bit. With this bandwidth but in last it is it represents the unicode character. So it is 4 bytes I believe. And we have several integer types like this. This number means its size. So which means I32 means integer value with 32 bit size like this. And U means unsigned. I means assigned integer. And we also to floating point types. And we have three actually string types. Actually this is a static string is static string which is allocated in the data segment. So we cannot modify its value. Yeah. And str is actually the size. So str is some range between the strings. So we have long if when we have a sub string we can, sub strings are represented with this type of types. And a string the type means is a type like string buffer in Java. So we can it has a memory area. We can use string this value like use as a buffer. So string, to manipulate strings, we will use this. Yeah. So does it be the static in generalized data type? Like for example can I have like a static array of 8 bit unsigned numbers or something? No. Yeah. So static strings no, no. Actually it is a string is a kind of a vector or this implement as a this vector. But in that program the vector in order vector this string is totally different things because you know string, string this type implements some different methods which does not vector have. So that's the reason. Why? And I'm sure as how to say and there's a no other types so we can like the list of arrays static like in the static way but it is a does not allocated in data segment it will be allocated in the stacks. Oh wait so in the upcoming AVR backend like this is not going to be usable for like flash score, program stuff but you need some kind of other annotation like if you have a hardware architecture in the update system program then it doesn't give you a ready made solution. Yeah, yeah, yeah. Do you know anything about the plans for how to do that? Actually I have no idea about it but we can how to say we can link last program with C programs so if you do that if you want to do that you write these things in C or C++ as it's called and link them to the last code. It's unsafe things but there must be more I would say a good way but I'm sure. Okay, let's move on to the arrays so we have we can write an array a little like this and the array is an array not a list so it has a fixed size we cannot change the size. If you want to change arrays like things like size you should use a vector which is a list. So we can use an array arrays and we can get an array's length with this function like this. The array has a method which name is LEN which denotes an array's length. Yup. Is that an array that can compile time because the type already has the length? Are there any more generated from this array? Ah, it is a when we it is not replaced to a fixed array when compiled it is solved as a function call actually. The last method is implemented as a function of which first argument is object itself so give us a context to tell that that is the reason. Okay. And slice slice means a range of the list or arrays so we can we can refer or sub arrays or substrings is a slice so we can make a array which contains these from new array like this which contains this area to decide this element and this element so which contains that array like this. From the last program from the viewpoint last program there is no difference between the slice and array itself but in the compiled code it is the difference of this how to say slice is implemented as a pointer so there's no memory consumption that means that this how to say this when we create a new slice that does not mean memory copy or something like that. And lastly have another that type which is which name is a tuple which is a combination of values so we can create a defined table like this. So what's the difference between a tuple and an array? Tuple has a one big difference is tuple contains several different types inside as a member and also access how to access is different each access to each member is different to the array so actually how to say tuple is used as define a temporal structure, data structure for data structure. Okay let's move on to the control for last have several control for expressions first one is conditional branch is described with if expressions actually it is expression not statements so if we can write a code like this so if has a evaluation value so this means y will be binding to the value 1 because x is greater than 0 so if x is binding to the value like minus 1 y will bind to 0 like this and loops infinite loop can be described with loops statement it is a statement actually and we can also use wire statement as well and a false statement is not good for we cannot use false statement for define to describing loops because you know it is a scan it means a scan of iterators and we can define simple so if we want to count that value from 0 to 10 we can write we will write in this way so in this in this section we define a sequence actually define an iterator which contains the value of 0 to 10 and we can scan for each each item in that iterator with this with this program and iterator can be translated to the animator means the iterator with index value and so we in this section we create an iterator and by calling an iterate method of it over it the iterator will be changed to the animator and each item of the animator consists of is a tuple of which had two elements inside it first element is an index over index of an item and second one is the value of the element okay and here's the last things of the set or thing data type it is a vector it's a list actually so we can define the vector with this macro and we can scan vector with all operator or something like that like this yeah scanning vector we can scan in code like in various way but with animator we can say for example vector have several methods and vectors ITER method by calling this method the vector changes to the generate how to say create a new iterator and which returns animator okay anyway anyway let's move on to the more what should I say so I can see all of you are boring to above my explanation move let's write a new code about this so here is a skeleton code could you implement this function and it is an implement function so who is a function returns which summarize or add all values of every item inside given vectors so we have two vectors so you need to add all values inside this vector and also add this bit element in the B2 so let's could you write it okay return type return type will be integer skeleton code so could you create a new project for this exercise and implement it what should I do what should I write I forgot something so writing when we write C way C like code it will be this kind of code it is a one example we can write more functional programming way like with folder method or something like that that this is a basic last code C program familiar with writing this kind of code so different between these two lines yeah very different it is actually it is different because in vector as I say because you know this is this line this four statement means scanning over the trait which is generated from V1 but second one is scanning over the V2 that is totally little bit different because you know actually this when we call this method V1 does not have ownership over the vector value elements but vector actually implements some aspects of the iterated features so that is why we can do write this kind of code for example you can't fold over the vector because fold is a method of make theory first start of without plastic this line can change without plastic plastic or white right yeah actually this is this last that three operators function call or something like function call and this value type does not implement this function so we cannot write this way yep yep so you can call type type because you know let's compare different type by other operation type in it will like this way yeah yeah difference yep so does that mean that in the previous version it was result before plus i in that version does that mean that it implicitly dereference is i because you know if you write without plastic close deref i then it works which kind of make sense because i is not really a value pointer I guess or when you write it yeah I say yeah yeah difference you get confused with the situation but okay difficult why okay so yep because this i is a reference to this easy element inside these iterators so it is notated as a reference so that is a pointer but this method the argument is value, integer value so we can for example this code can be does not this code will, does not cause compiler error maybe yep this i doesn't actually we expected i is a integer value but actually it is a pointer so that's the reason why compiler I think it's quite clear to everyone why this doesn't work and how to fix it, yeah but the reason is why does the other one work why does it work when you write pointer like in the other one second one why does that work yeah I'm sure actual reason but I assume that this operator has different many type I would say implementation which contains when we internally it is defined as an integer value we treat it translated as a function call of which argument will be type is first argument is an integer value and first and second is a reference to the integer so so this function and this function is a different thing so that's the reason why this second code works well I assume that okay very complicated anyway we can actually last has a difference value actually difference and we can difference by putting a series before the values it's a C way so I have talked about mentioned so we moved on to another topic it is a ownership actually last has a ownership actually last has a ownership is a very complicated things because you know last because this program this code calls compiler actually so the reason is the reason the reason is actually by say say as a first line variable v is binded to the vector with this vector but in the third line the v v2 is also actually binded to this vector but actually in this third line means v1 no longer owns actually no longer binded to this value let's try I'll do my demo for example for example for example what should I do this code works well as my other expectation but this code does not can be compiled because in compiler last actually in last values can be binded only variables so we cannot make association between two variables to all same values actually because that idea we call ownership like this kind ideas actually so ownership is very complicated because it is because ownership transfer occurred when we call functions actually actually we can specify we can give specify a call function like like this some function called v2 but it means the ownership over this value is transferred to the functions variable in the function arguments so v1 have no longer holds ownership v2 does not no longer connected make association between these values so if we do this kind of this kind of that type type yeah yeah that's what we say that we call this kind compiler because value is moved here and move to this variable and v2 does not hold no longer hold this difference actually ownership over this object so to avoid this kind of compiler we have two options first one is we need to return the argument value with return value like with tuple using tuple first assignment oh yeah okay so when we return the value to the return value we can avoid this kind of compiler also second option is we can use we can declare this argument as a reference difference means so reference like this so and we can when we declare this function like in this way so that means when we call this function the ownership how to say is temporarily moved to the argument and returns ownership automatically returns when the function is finished that's a basic idea okay and here's some okay that's a basic idea of ownership actually okay so wrap up last assignment actually binding in last is very complicated it is not assignment actually and it is how to say ownership transferring transfer actually so when we should as we will suffer this kind of trouble very well but that is last anyway actually I consumed one error already so let's move on to the hands-on implementation of cat command so let's start with creating a new project directory with cargo command could you create a new project directory for cat command and implement it and write its main program like this we can get for an argument command line argument with as a vector like this by calling this method actually mv is a name space and this function returns the iterator of the command line argument and correct method to convert the iterator into the vector so by calling this method chain we can get a vector of the command line argument and it will be binded to this variable can you do that okay for the first line we get the command line argument a vector which contains the command line argument and we can say its length and we can change a switch control flow according to the hands-on arguments argument is specified or not in this way but we can give a command line by the cargo command like this way okay so now we can get a command line argument argument so we can let's move on to next things next things that is a leading file so so to lead files we should import some functions into the namespace so which is developing to the IO namespace and by writing these three lines we can call functions to manage our access files like file this function and in last we can use files in C way so we get file actually file this by calling this method open method we get lead whole contents as a string with by this by this line and we have two new syntax here try that is how to say try as you know file access can be paired so try is a macro which how to say continue if when its argument is a is a success when actually try macro terminates this function when file this function called pairs we cannot when we cannot open this file so that this function here return immediately here and in last week our error is 100 to ways how to say result type which consist of two values first one is okay second is error okay represents success case represents error case by using match it is a pattern match statement combination of this result type and pattern match we can branch our control flow according to the some operations failure with success in this way so first could you write these lines as these four lines to your main program yeah so does that code clone mean that we are copying the copy is a string but by type byte I mean it's real copy just reference to the same it's a real copy because because actually I I define this function by different but there is an actual value in this way so that's why I like I am calling a string here to avoid some trouble in the cause with from the ownership problem yeah we can change we can like this actually this line like this by using reference we can we can change we or when we this function this way like this we can we do not need to clone make a copy of a string but this function gets a reference to the string it is different when we remove the integer from it's type notation that means this function gets a string value so that is different and in this in this case we can I guess there is no longer I'll say and in this case and we call this function without copying a string value so this I guess but the value does not contain there I'll say the first element of the command line argument so to avoid the trouble trouble I put to this I made a copy of it okay and after writing that could you implement the read file function like this okay and now we have we have implemented simplest command now so read file function implement the reading returns file content content of content of a specified the file which is specified as a command line argument and this file name comes from the command line argument when we call the function and in this way and as you know reading file can be paired and we can this and this program shows a file content only when each reading file success in error case it shows a stack trace why this reading file is paired by this command for example there is no file of which name is who here so that we will failed here in last weekend error I'll say error handling is down we will use this result type for error handling errors I'll say this is two things two values okay and error and each of them have a pair load okay contains some result of some tasks error contains the reason why it's task fails and we can detect this task failed or success with a pattern matching its return value with this match statement that's the basic idea of last error handling and yep so because you talked about panic and effect program do you use to debug like last programs like for C++ or CIF or LLDB we can use ZDB yeah we can use ZDB but in actually last problem doesn't need debugging when we pass the comparers because you know many bugs can be found in comparers it's very rare to use debug aspects but when we want we try to debug the program we can use ZDB yeah so we we would like to build a release build without no debug symbols we can specify release the command will be create a release build yep okay that is the basic okay very quick things that my content is over so could you expand we have 30 minutes to 9pm so I will expand by yourself the current cat command yeah by the way this slide is published on the guitar pages so you can see sure actually I will say it is very hard without I will say I will say I will post it and it positively consists of content sample course inside it yeah we can yeah I will say that that is hmm that is easy to me yeah that's the reason main reason as long as I from my experience I have no there is it is there to use let's see directly almost thing can be done with hmm very anyway so everyone doing half of them doing something what should I do I remember I remember probably I think maybe maybe probably the method yeah yeah yeah yeah yeah yeah yeah yeah okay so almost my content that I will share the slide so actually anyway that I I am happy actually that is a very actually it is for C program that's the design is to and actually published on the so that's it thanks