summaryrefslogtreecommitdiffhomepage
path: root/rust.html.markdown
diff options
context:
space:
mode:
Diffstat (limited to 'rust.html.markdown')
-rw-r--r--rust.html.markdown34
1 files changed, 25 insertions, 9 deletions
diff --git a/rust.html.markdown b/rust.html.markdown
index 6b75fa87..e835de12 100644
--- a/rust.html.markdown
+++ b/rust.html.markdown
@@ -1,5 +1,5 @@
---
-language: rust
+language: Rust
contributors:
- ["P1start", "http://p1start.github.io/"]
filename: learnrust.rs
@@ -22,7 +22,7 @@ currently available in the nightly builds. Rust has adopted a train-based releas
model with regular releases every six weeks. Rust 1.1 beta was made available at
the same time of the release of Rust 1.0.
-Although Rust is a relatively low-level language, Rust has some functional
+Although Rust is a relatively low-level language, it has some functional
concepts that are generally found in higher-level languages. This makes
Rust not only fast, but also easy and efficient to code in.
@@ -41,6 +41,7 @@ Rust not only fast, but also easy and efficient to code in.
// 1. Basics //
///////////////
+#[allow(dead_code)]
// Functions
// `i32` is the type for 32-bit signed integers
fn add2(x: i32, y: i32) -> i32 {
@@ -48,6 +49,9 @@ fn add2(x: i32, y: i32) -> i32 {
x + y
}
+#[allow(unused_variables)]
+#[allow(unused_assignments)]
+#[allow(dead_code)]
// Main function
fn main() {
// Numbers //
@@ -88,10 +92,8 @@ fn main() {
let s: String = "hello world".to_string();
// A string slice – an immutable view into another string
- // This is basically an immutable pair of pointers to a string – it doesn’t
- // actually contain the contents of a string, just a pointer to
- // the begin and a pointer to the end of a string buffer,
- // statically allocated or contained in another object (in this case, `s`)
+ // The string buffer can be statically allocated like in a string literal
+ // or contained in another object (in this case, `s`)
let s_slice: &str = &s;
println!("{} {}", s, s_slice); // hello world hello world
@@ -174,13 +176,19 @@ fn main() {
impl<T> Foo<T> {
// Methods take an explicit `self` parameter
- fn get_bar(self) -> T {
+ fn bar(&self) -> &T { // self is borrowed
+ &self.bar
+ }
+ fn bar_mut(&mut self) -> &mut T { // self is mutably borrowed
+ &mut self.bar
+ }
+ fn into_bar(self) -> T { // here self is consumed
self.bar
}
}
let a_foo = Foo { bar: 1 };
- println!("{}", a_foo.get_bar()); // 1
+ println!("{}", a_foo.bar()); // 1
// Traits (known as interfaces or typeclasses in other languages) //
@@ -256,11 +264,16 @@ fn main() {
// `while` loop
while 1 == 1 {
println!("The universe is operating normally.");
+ // break statement gets out of the while loop.
+ // It avoids useless iterations.
+ break
}
// Infinite loop
loop {
println!("Hello!");
+ // break statement gets out of the loop
+ break
}
/////////////////////////////////
@@ -281,7 +294,7 @@ fn main() {
// Reference – an immutable pointer that refers to other data
// When a reference is taken to a value, we say that the value has been ‘borrowed’.
// While a value is borrowed immutably, it cannot be mutated or moved.
- // A borrow lasts until the end of the scope it was created in.
+ // A borrow is active until the last use of the borrowing variable.
let mut var = 4;
var = 3;
let ref_var: &i32 = &var;
@@ -290,6 +303,8 @@ fn main() {
println!("{}", *ref_var);
// var = 5; // this would not compile because `var` is borrowed
// *ref_var = 6; // this would not either, because `ref_var` is an immutable reference
+ ref_var; // no-op, but counts as a use and keeps the borrow active
+ var = 2; // ref_var is no longer used after the line above, so the borrow has ended
// Mutable reference
// While a value is mutably borrowed, it cannot be accessed at all.
@@ -300,6 +315,7 @@ fn main() {
println!("{}", *ref_var2); // 6 , // var2 would not compile.
// ref_var2 is of type &mut i32, so stores a reference to an i32, not the value.
// var2 = 2; // this would not compile because `var2` is borrowed.
+ ref_var2; // no-op, but counts as a use and keeps the borrow active until here
}
```