Update book to add new example
This commit is contained in:
		
							parent
							
								
									00c91fff60
								
							
						
					
					
						commit
						195d3d3af8
					
				| @ -247,6 +247,8 @@ calls, literals, or if-expressions. Types of supported expressions include: | |||||||
| - **Function calls**, to invoke a predefined function with given parameters | - **Function calls**, to invoke a predefined function with given parameters | ||||||
| - **Associated function calls**, to invoke a predefined function on a certain | - **Associated function calls**, to invoke a predefined function on a certain | ||||||
|   *associated type* with given parameters. |   *associated type* with given parameters. | ||||||
|  |   - **Accessing function calls**, a shorthand to call associated function calls | ||||||
|  |     which have `&self` or `&mut self` as their first parameter. | ||||||
| - **Block-expressions**, which can return a value to the higher-level expression | - **Block-expressions**, which can return a value to the higher-level expression | ||||||
|   if they have a statement with a soft-return. Otherwise they return void. |   if they have a statement with a soft-return. Otherwise they return void. | ||||||
| - **If-expressions**, which can execute one of two expressions depending on the | - **If-expressions**, which can execute one of two expressions depending on the | ||||||
| @ -263,7 +265,7 @@ In formal grammar: | |||||||
|     <array> | <struct> | |     <array> | <struct> | | ||||||
|     <indexing> | <accessing> | |     <indexing> | <accessing> | | ||||||
|     <binary-exp> | <unary-exp> | |     <binary-exp> | <unary-exp> | | ||||||
|     <function-call> | <assoc-function-call> |     <function-call> | <accessing-function-call> | <assoc-function-call> | ||||||
|     <block> | <if-expr> | <cast> | |     <block> | <if-expr> | <cast> | | ||||||
|     ( "(" <expression> ")" ) |     ( "(" <expression> ")" ) | ||||||
| 
 | 
 | ||||||
| @ -278,6 +280,7 @@ In formal grammar: | |||||||
| <binary-exp> :: <expression> <binop> <expression> | <binary-exp> :: <expression> <binop> <expression> | ||||||
| <unary-exp> :: <unary> <expression> | <unary-exp> :: <unary> <expression> | ||||||
| <function-call> :: <expression> "(" [ <expression> ( "," <expression> )* ] ")" | <function-call> :: <expression> "(" [ <expression> ( "," <expression> )* ] ")" | ||||||
|  | <accessing-function-call> :: <accessing> "(" [ <expression> ( "," <expression> )* ] ")" | ||||||
| <assoc-function-call> :: <type> "::" <function-call> | <assoc-function-call> :: <type> "::" <function-call> | ||||||
| <if-expr> :: "if" <expression> <expression> [ "else" <expression> ] | <if-expr> :: "if" <expression> <expression> [ "else" <expression> ] | ||||||
| <cast> :: <expression> "as" <type> | <cast> :: <expression> "as" <type> | ||||||
| @ -296,6 +299,7 @@ test.first // Accessing | |||||||
| !bool_value // Unary | !bool_value // Unary | ||||||
| func(value, 14) // Function call | func(value, 14) // Function call | ||||||
| Test::get_field(&test); // Associated function call | Test::get_field(&test); // Associated function call | ||||||
|  | test.get_field(); // Same, but using a the dot-form shorthand | ||||||
| if varname {} else {} // If-expression | if varname {} else {} // If-expression | ||||||
| value as u32 // cast | value as u32 // cast | ||||||
| (value + 2) // Binop within parenthesis | (value + 2) // Binop within parenthesis | ||||||
|  | |||||||
| @ -22,6 +22,7 @@ or simply casting the value (e.g. `5 as u32`). | |||||||
| ## Table of Contents: | ## Table of Contents: | ||||||
| - [Hello World](#hello-world) | - [Hello World](#hello-world) | ||||||
| - [Borrowing and Pointers](#borrowing-and-pointers) | - [Borrowing and Pointers](#borrowing-and-pointers) | ||||||
|  | - [Harder Hello World](#harder-hello-world) | ||||||
| 
 | 
 | ||||||
| ### Hello World | ### Hello World | ||||||
| 
 | 
 | ||||||
| @ -120,3 +121,73 @@ This example will always return `17`. Notice also, how a **mutable** borrow was | |||||||
| passed to `mutate`-function. While borrows do not always need to be mutable, | passed to `mutate`-function. While borrows do not always need to be mutable, | ||||||
| this example would not work without the `mut`-keyword. Try it out for yourself | this example would not work without the `mut`-keyword. Try it out for yourself | ||||||
| to see why! | to see why! | ||||||
|  | 
 | ||||||
|  | ### Harder Hello World | ||||||
|  | 
 | ||||||
|  | A little bit harder example to the previous hello world would be | ||||||
|  | `hello_world_harder.reid` from [examples](../examples/hello_world_harder.reid) | ||||||
|  | 
 | ||||||
|  | ```rust | ||||||
|  | import std::print; | ||||||
|  | import std::String; | ||||||
|  | 
 | ||||||
|  | fn main() { | ||||||
|  |     let mut test = String::from("hello"); | ||||||
|  | 
 | ||||||
|  |     test.push(String::from(" world: ")); | ||||||
|  |     test.push_num(175); | ||||||
|  |      | ||||||
|  |     print(test); | ||||||
|  | 
 | ||||||
|  |     test.free(); | ||||||
|  |     return; | ||||||
|  | } | ||||||
|  | ``` | ||||||
|  | 
 | ||||||
|  | Let's go through this again line-by-line | ||||||
|  | 
 | ||||||
|  | ```rust | ||||||
|  | import std::print; | ||||||
|  | import std::String; | ||||||
|  | ``` | ||||||
|  | 
 | ||||||
|  | At the start are the standard imports, like in the original Hello World, but | ||||||
|  | notice that instead of importing just functions we import the type `String` as | ||||||
|  | well. When importing types, not only are the type imported, but also any binary | ||||||
|  | functions or associated functions related to it with it. | ||||||
|  | 
 | ||||||
|  | ```rust | ||||||
|  |     let mut test = String::from("hello"); | ||||||
|  | ``` | ||||||
|  | 
 | ||||||
|  | Next we create the initial string, just like in our original example, but this | ||||||
|  | time the message is not complete. We also use an associated function to create | ||||||
|  | the string instead one of the now-deprecated global functions. | ||||||
|  | 
 | ||||||
|  | ```rust | ||||||
|  |     test.push(String::from(" world: ")); | ||||||
|  |     test.push_num(175); | ||||||
|  | ``` | ||||||
|  | 
 | ||||||
|  | After that we edit the message by first pushing ` world: ` to it, and also | ||||||
|  | appending the number `175` at the end of it. For both of these operations we are | ||||||
|  | again using associated functions, but in a different short-hand syntax. | ||||||
|  | 
 | ||||||
|  | These two lines would be actually equivalent to the above ones: | ||||||
|  | ```rust | ||||||
|  |     String::push(&mut test, String::from(" world: ")); | ||||||
|  |     String::push_num(&mut test, 175); | ||||||
|  | ``` | ||||||
|  | 
 | ||||||
|  | ---- | ||||||
|  | 
 | ||||||
|  | ```rust | ||||||
|  | 
 | ||||||
|  |     print(test); | ||||||
|  | 
 | ||||||
|  |     test.free(); | ||||||
|  |     return; | ||||||
|  | ``` | ||||||
|  | 
 | ||||||
|  | After that we simply print the string, and free up the string (again with an | ||||||
|  | associated function) before returning. Nothing special there! | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user