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 From 5af797722e80d48ae28494b514c4a1c153340662 Mon Sep 17 00:00:00 2001 From: S Stewart Date: Wed, 25 Dec 2019 01:00:42 -0600 Subject: rust: update lang name to be correct Sorry if this annoys anyone, just feels a bit annoying to not have the language's name be capitalized :P --- rust.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'rust.html.markdown') diff --git a/rust.html.markdown b/rust.html.markdown index 92794e69..23dcbd4f 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 -- cgit v1.2.3 From e98cb424b36a2cda2e55b3bb9db369740d4bd562 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Hou=C5=A1ka?= Date: Sat, 8 Feb 2020 23:29:46 +0100 Subject: [rust/en] Small stylistic fix. --- rust.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'rust.html.markdown') diff --git a/rust.html.markdown b/rust.html.markdown index 23dcbd4f..413939bf 100644 --- a/rust.html.markdown +++ b/rust.html.markdown @@ -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. -- cgit v1.2.3 From 33921c9e6c30e07bbc1a6187ac364429a84d04bf Mon Sep 17 00:00:00 2001 From: polazarus Date: Wed, 12 Feb 2020 21:18:41 +0100 Subject: [Rust] Change misleading method and add two other methods The `get_bar` method consumes `self`. The name is misleading and does not respect the language naming convention. This PR renames it to `into_bar` and provides `bar` (a getter) and `bar_mut` (to get a mutable reference). --- rust.html.markdown | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'rust.html.markdown') diff --git a/rust.html.markdown b/rust.html.markdown index 413939bf..83682c01 100644 --- a/rust.html.markdown +++ b/rust.html.markdown @@ -176,7 +176,13 @@ fn main() { impl Foo { // 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 } } -- cgit v1.2.3 From 602dbf5267ff15ab137bee2dbedad7f500d039a2 Mon Sep 17 00:00:00 2001 From: polazarus Date: Wed, 12 Feb 2020 21:25:39 +0100 Subject: Change call --- rust.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'rust.html.markdown') diff --git a/rust.html.markdown b/rust.html.markdown index 83682c01..e835de12 100644 --- a/rust.html.markdown +++ b/rust.html.markdown @@ -188,7 +188,7 @@ fn main() { } 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) // -- cgit v1.2.3