Learn Rust, Contribute to SAFE, and Save the World

// Comments
// 5.4. Comments

fn main() {

let what_I_learned_today = [0,1]
println!("{}: Comments are made with //", what_I_learned_today[1]);

let questions = [0,1]
println!("{}: no questions today", questions[1]);

}

// comments seem to be pretty straight forward

// if
// 5.5. if

fn main() {

let what_I_learned_today = [0,1,2]
println!("{}: if statements are the way code makes decisions \n 
https://en.wikipedia.org/wiki/Decision-making ", what_I_learned_today[1]);

println!("{}: if statements are composed of two parts if and else \n ", what_I_learned_today[2]);

println!("for example if have a question print question else print good bye");

let do_I_have_questions: bool = false;
let questions = [0,1]

if do_I_have_questions == true {
     println!("{}: insert question here", questions[1]);
} else {
    println!("no questions today \n");
    println!("Good Bye :) ");
}

}

// if statements are something we do as humans on a daily basis
// Hour of Code - Bill Gates explains If statements - YouTube

2 Likes

Really great to see someone learning Rust with zero programming experience! Thanks for the ongoing updates too, itā€™s been fascinating to see how you are learning the fundamental concepts. As a tip, you can format code in the comments using fences:

```rust
fn main() { println!("hi") }
```

Which results in the nicely formatted:

fn main() { println!("hi") }
3 Likes

Pour les francophones, jā€™ai trouvĆ© cette page.

http://blog.guillaume-gomez.fr/Rust/1/1

4 Likes

Itā€™s always nice see people eliminate excuses for not doing the things you want to do.

2 Likes

cā€™est vrai, mon brave!

http://doc.rust-lang.org/book/for-loops.html
5.6. for loops

What I learned today:
1: for loops repeat a section of code for a specified number of times
2: enumerate counts the number of times a loop has occurred

Comments: I would like to thank everyone for their support it has been really helpfull

Questions: does enumerate only work on loops?

4 Likes

Good question. I think yes but itā€™s not really important to know that until you find something else that can be used with enumerate.

Nice point, also in rust there are (like python) iterators and ranges, these are very powerful.

Anyway all I mean is that loops are hidden inside iterators in a neat way. but perhaps a little hidden at first. For example here is a pretty neat small xor function for doing a one time pad type operation (tho this is not a one time pad, but you will see what I mean). This takes two different sizes of data and create an enclosed iterator inside another iterator and xors bits together to give a result of an xored stream. Usually this is a good few lines of code, in rust itā€™s as simple as

/// Helper function to XOR a data with a pad (pad will be rotated to fill the length)
pub fn xor(data: &[u8], pad: &[u8]) -> Vec<u8> {
    data.iter().zip(pad.iter().cycle()).map(|(&a, &b)| a ^ b).collect()
}

so zip combines 2 iterators, cycle, mean loop continually (as long as you are called, then advance one more step, looping around at the end back to the beginning), and the map maps a function to the results (like a mini function). Here the a and b are the iterator values from each loop (iterator). We take them by reference (so a view of them but not a copy) and xor these together to give a result. The & which I Called a view is actually a borrow in rust, so borrows a peek at a variable (while borrowed it cannot be mutated, we have temporary ownership (this is really powerful)). Then we call collect() which fires the whole thing into a vector (or collects the results into a container, if you like).

5 Likes

Canā€™t wait to get into rust. Got a couple of hardware projects to finish off first.

Reading some of the intro into rust, it reminded me of an old language with the ā€œletā€ and your example triggered a memory of some programming I did in an language from the early nineties (not used by anyone for over 15 years now). The iterators were powerful. I realise that rust is a different and more powerful language to that one, but some features seem familiar. Canā€™t wait now.

3 Likes

To be true, Iterator always giving me headache. Python is a language that I didnā€™t learned yet and I donā€™t think I will someday. After @dirvineā€™s explanation I dig more deeper trying to clear it up in my mind and I find this:

That explain a lot and now I can clear it up in my brain. They have explanation for each function and good neat examples to better understand how it work.

2 Likes

I do this for fun. Itā€™s like at school when we do math and calculating the result one step at a time.
In bold is the next step.
In italic is the result

let data = [3, 8, 5];
let pad = [6, 7];

data.iter().zip(pad.iter().cycle()).map(|(&a, &b)| a ^ b).collect()
[3 , 8, 5].iter().zip([6, 7].iter().cycle()).map(|&a, &b| a ^ b).collect()
[3 , 8, 5].iter().zip([6, 7].iter().cycle()).map(|&a, &b| a ^ b).collect()
[3, 8, 5].iter().zip([6, 7, 6, 7, 6, 7, 6, 7, ā€¦]).map(|&a, &b| a ^ b).collect()
[3, 8, 5].iter().zip([6, 7, 6, 7, 6, 7, 6, 7, ā€¦]).map(|&a, &b| a ^ b).collect()
[[3, 6], [8, 7], [5, 6]].map(|&a, &b| a ^ b).collect()
[[3, 6], [8, 7], [5, 6]].map(|&a, &b| a ^ b).collect()
[[3 ^ 6], [8 ^ 7], [5 ^ 6]].collect()
[[3 ^ 6], [8 ^ 7], [5 ^ 6]].collect()
[5, 15, 3].collect()
[5, 15, 3].collect()
[5, 15, 3]

1 Like

https://doc.rust-lang.org/book/while-loopacs.html
5.7 while loops

What I learned today:
1: while loops repeat until a condition is met
2: break is a clean way to end a loop

Comments: there are a lot of similarities between the way a computer program operates and the way the human brain operates.

Questions: Does anyone have any tips on ownership before I delve into it?

Very powerful, basics of concurrency

You can have as many readers of data as you want, As long as there is no writer (therefore data not mutable)

If there is any writer (mutable data) then you must only have that one writer have access, so you let them borrow it for as short a time as possible.

This is the very basis, but itā€™s a massive issue really and rust handles it really well.

8 Likes

Used this often in network programming, and I have one question, Can you reduce that to the ā€œrecordā€ (part file) level? Or is it simply write the record (write to file) then release the file?

Itā€™s really per object, if you can grab a bit of an object then cool (in rust for instance you can grab just a bit of an array as writeable in some situations). Again the iterators in rust are good to guide you here.

In general though itā€™s a per object rule AFAIK

1 Like

Excellent. That is very workable.

Thanks for the quick reply.

1 Like

good link with an explanation and introduction to some more advanced topics in Rust such as RefCell.

Progress is slow learning RUST, the language seems more complicated than any language Iā€™ve learned before, or maybe recent health issues is slowing me down, but Iā€™ll keep trudging on. Nothing else, I rent a small VPS with extra capacity, and can become a farmer come Alpha-4

3 Likes

I havenā€™t learned it myself, but having peeked I think I understand what you mean! It has some quite novel concepts - although Iā€™ve found similar things in my learning of JavaScript (closures and Promises/Futures for example).

I suspect that the win comes later, with fewer bugs to chase due to better language design and better syntax checking and feedback from the compiler, so keep at it :slight_smile:

2 Likes