summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorDivay Prakash <divayprakash@users.noreply.github.com>2019-09-17 19:24:29 +0530
committerGitHub <noreply@github.com>2019-09-17 19:24:29 +0530
commit69e1b56672ded1eb0e94bc15e01044fc67b989bb (patch)
tree34acc3893c4dedefcd1f376d14b33dabe43d0047
parentffb9c5f909d860a98d3d86ed49bc169413e0dd65 (diff)
parentfdd278fde1fc7e5c26a853200cade201bb86e48f (diff)
Merge pull request #3618 from Emerentius/master
[Rust/en] minor fixes to string slices and borrow system
-rw-r--r--rust.html.markdown11
1 files changed, 6 insertions, 5 deletions
diff --git a/rust.html.markdown b/rust.html.markdown
index 71bc16b5..92794e69 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
@@ -290,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;
@@ -299,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.
@@ -309,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
}
```