From cd6abe0bfcf6f34cec65b12324cebee666e0a19a Mon Sep 17 00:00:00 2001 From: Anatolij Date: Thu, 20 Sep 2018 09:19:39 +0200 Subject: The source code can be run by Rust Playground The source code can be run by Rust Playground without warnings --- rust.html.markdown | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'rust.html.markdown') diff --git a/rust.html.markdown b/rust.html.markdown index 6b75fa87..1c0e324c 100644 --- a/rust.html.markdown +++ b/rust.html.markdown @@ -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 // @@ -256,11 +260,13 @@ fn main() { // `while` loop while 1 == 1 { println!("The universe is operating normally."); + break; } // Infinite loop loop { println!("Hello!"); + break; } ///////////////////////////////// -- cgit v1.2.3 From 501828c89180d88183aa05749911f4e660878c23 Mon Sep 17 00:00:00 2001 From: Anatolij Date: Thu, 20 Sep 2018 13:04:31 +0200 Subject: Add gescription related break statement Insert description related to break statement. --- rust.html.markdown | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'rust.html.markdown') diff --git a/rust.html.markdown b/rust.html.markdown index 1c0e324c..71bc16b5 100644 --- a/rust.html.markdown +++ b/rust.html.markdown @@ -260,13 +260,16 @@ fn main() { // `while` loop while 1 == 1 { println!("The universe is operating normally."); - break; + // break statement gets out of the while loop. + // It avoids useless iterations. + break } // Infinite loop loop { println!("Hello!"); - break; + // break statement gets out of the loop + break } ///////////////////////////////// -- cgit v1.2.3 From c5f6427b6001aa0bb260d1636641c5edf01d8998 Mon Sep 17 00:00:00 2001 From: Emerentius Date: Sun, 1 Sep 2019 11:54:15 +0200 Subject: rust: improve explanation of string slices --- rust.html.markdown | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'rust.html.markdown') diff --git a/rust.html.markdown b/rust.html.markdown index 71bc16b5..f8f6c5e4 100644 --- a/rust.html.markdown +++ b/rust.html.markdown @@ -92,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 -- cgit v1.2.3 From fdd278fde1fc7e5c26a853200cade201bb86e48f Mon Sep 17 00:00:00 2001 From: Emerentius Date: Sun, 1 Sep 2019 11:54:40 +0200 Subject: rust: update explanation of borrow system With the introduction of non-lexical lifetimes, borrows no longer last until the end of scope. This has always been active in the 2018 edition and is now also true in the 2015 edition as of Rust 1.36 --- rust.html.markdown | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'rust.html.markdown') diff --git a/rust.html.markdown b/rust.html.markdown index f8f6c5e4..92794e69 100644 --- a/rust.html.markdown +++ b/rust.html.markdown @@ -288,7 +288,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; @@ -297,6 +297,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. @@ -307,6 +309,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 } ``` -- cgit v1.2.3