Learn Rust, Contribute to SAFE, and Save the World


#41

// http://doc.rust-lang.org/stable/book/comments.html
// 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


#42

// http://doc.rust-lang.org/stable/book/if.html
// 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
// https://youtu.be/m2Ux2PnJe6E


#43

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") }

#44

Pour les francophones, j’ai trouvé cette page.

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


#45

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


#46

c’est vrai, mon brave!


#47

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?


#48

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.


#49

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).


#50

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.


#51

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:
https://doc.rust-lang.org/stable/std/iter/trait.Iterator.html

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.


#52

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]


#53

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?


#54

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.


#55

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?


#56

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


#57

Excellent. That is very workable.

Thanks for the quick reply.


#58

http://nercury.github.io/rust/guide/2015/01/19/ownership.html

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


#59

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


Introduce yourself
#60

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: