Reference before assignment in Rust











up vote
-1
down vote

favorite












I am playing around with Rust references:



fn main() {
let str = String::from("Hallo");
let &x = &str;
}


This produces the following error:



error[E0507]: cannot move out of borrowed content
--> src/main.rs:3:9
|
3 | let &x = &str;
| ^-
| ||
| |hint: to prevent move, use `ref x` or `ref mut x`
| cannot move out of borrowed content


What is going on here?










share|improve this question




















  • 2




    What are you trying to achieve? Your expectations?
    – Rajeev Ranjan
    Nov 17 at 19:48















up vote
-1
down vote

favorite












I am playing around with Rust references:



fn main() {
let str = String::from("Hallo");
let &x = &str;
}


This produces the following error:



error[E0507]: cannot move out of borrowed content
--> src/main.rs:3:9
|
3 | let &x = &str;
| ^-
| ||
| |hint: to prevent move, use `ref x` or `ref mut x`
| cannot move out of borrowed content


What is going on here?










share|improve this question




















  • 2




    What are you trying to achieve? Your expectations?
    – Rajeev Ranjan
    Nov 17 at 19:48













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I am playing around with Rust references:



fn main() {
let str = String::from("Hallo");
let &x = &str;
}


This produces the following error:



error[E0507]: cannot move out of borrowed content
--> src/main.rs:3:9
|
3 | let &x = &str;
| ^-
| ||
| |hint: to prevent move, use `ref x` or `ref mut x`
| cannot move out of borrowed content


What is going on here?










share|improve this question















I am playing around with Rust references:



fn main() {
let str = String::from("Hallo");
let &x = &str;
}


This produces the following error:



error[E0507]: cannot move out of borrowed content
--> src/main.rs:3:9
|
3 | let &x = &str;
| ^-
| ||
| |hint: to prevent move, use `ref x` or `ref mut x`
| cannot move out of borrowed content


What is going on here?







rust






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 17 at 20:49









Shepmaster

143k11268399




143k11268399










asked Nov 17 at 19:45









Felix Ha

1209




1209








  • 2




    What are you trying to achieve? Your expectations?
    – Rajeev Ranjan
    Nov 17 at 19:48














  • 2




    What are you trying to achieve? Your expectations?
    – Rajeev Ranjan
    Nov 17 at 19:48








2




2




What are you trying to achieve? Your expectations?
– Rajeev Ranjan
Nov 17 at 19:48




What are you trying to achieve? Your expectations?
– Rajeev Ranjan
Nov 17 at 19:48












2 Answers
2






active

oldest

votes

















up vote
3
down vote



accepted










Adding to wiomoc's answer: depending on what language(s) you've previously known, variable declaration in Rust might be a little different. Whereas in C/C++ you explicitly have to declare that you want a pointer/reference variable:



int *p = &other_int;


In Rust it's enough to just use let, so the above would in Rust be



let p = &other_int;


and when you write



let &s = &string;


It pattern-matches that, so the Rust compiler reads it roughly as "I know I have a reference, and I want to bind whatever it is referring to to the name p". If you're not familiar with pattern-matching, a more obvious example (that works in Rust as well) would be



let point = (23, 42);
let (x, y) = point;


The second line unpacks the right-hand side to match the left-hand side (both are tuples of two values) and binds the variable names on the left to the values at the same position in the structure on the right. In the case above, it's less obvious that it's matching your "structural description".



The result of let &x = &str;, i.e. "I know &str is a reference, please bind whatever it refers to to the variable x" means that you're trying to have x be the same as str, when at that line all you have is a borrowed reference to str. That's why the compiler tells you you can't create an owned value (which x would be, because it's not being created as a reference) from a reference.






share|improve this answer










New contributor




dario is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    up vote
    1
    down vote













    You dont need that &at let x



    let str = String::from("Hallo");
    let x = &str;


    Or if you want to declare the type manually



    let string = String::from("Hallo");
    let x: &str = &string;





    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














       

      draft saved


      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53354917%2freference-before-assignment-in-rust%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      3
      down vote



      accepted










      Adding to wiomoc's answer: depending on what language(s) you've previously known, variable declaration in Rust might be a little different. Whereas in C/C++ you explicitly have to declare that you want a pointer/reference variable:



      int *p = &other_int;


      In Rust it's enough to just use let, so the above would in Rust be



      let p = &other_int;


      and when you write



      let &s = &string;


      It pattern-matches that, so the Rust compiler reads it roughly as "I know I have a reference, and I want to bind whatever it is referring to to the name p". If you're not familiar with pattern-matching, a more obvious example (that works in Rust as well) would be



      let point = (23, 42);
      let (x, y) = point;


      The second line unpacks the right-hand side to match the left-hand side (both are tuples of two values) and binds the variable names on the left to the values at the same position in the structure on the right. In the case above, it's less obvious that it's matching your "structural description".



      The result of let &x = &str;, i.e. "I know &str is a reference, please bind whatever it refers to to the variable x" means that you're trying to have x be the same as str, when at that line all you have is a borrowed reference to str. That's why the compiler tells you you can't create an owned value (which x would be, because it's not being created as a reference) from a reference.






      share|improve this answer










      New contributor




      dario is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















        up vote
        3
        down vote



        accepted










        Adding to wiomoc's answer: depending on what language(s) you've previously known, variable declaration in Rust might be a little different. Whereas in C/C++ you explicitly have to declare that you want a pointer/reference variable:



        int *p = &other_int;


        In Rust it's enough to just use let, so the above would in Rust be



        let p = &other_int;


        and when you write



        let &s = &string;


        It pattern-matches that, so the Rust compiler reads it roughly as "I know I have a reference, and I want to bind whatever it is referring to to the name p". If you're not familiar with pattern-matching, a more obvious example (that works in Rust as well) would be



        let point = (23, 42);
        let (x, y) = point;


        The second line unpacks the right-hand side to match the left-hand side (both are tuples of two values) and binds the variable names on the left to the values at the same position in the structure on the right. In the case above, it's less obvious that it's matching your "structural description".



        The result of let &x = &str;, i.e. "I know &str is a reference, please bind whatever it refers to to the variable x" means that you're trying to have x be the same as str, when at that line all you have is a borrowed reference to str. That's why the compiler tells you you can't create an owned value (which x would be, because it's not being created as a reference) from a reference.






        share|improve this answer










        New contributor




        dario is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.




















          up vote
          3
          down vote



          accepted







          up vote
          3
          down vote



          accepted






          Adding to wiomoc's answer: depending on what language(s) you've previously known, variable declaration in Rust might be a little different. Whereas in C/C++ you explicitly have to declare that you want a pointer/reference variable:



          int *p = &other_int;


          In Rust it's enough to just use let, so the above would in Rust be



          let p = &other_int;


          and when you write



          let &s = &string;


          It pattern-matches that, so the Rust compiler reads it roughly as "I know I have a reference, and I want to bind whatever it is referring to to the name p". If you're not familiar with pattern-matching, a more obvious example (that works in Rust as well) would be



          let point = (23, 42);
          let (x, y) = point;


          The second line unpacks the right-hand side to match the left-hand side (both are tuples of two values) and binds the variable names on the left to the values at the same position in the structure on the right. In the case above, it's less obvious that it's matching your "structural description".



          The result of let &x = &str;, i.e. "I know &str is a reference, please bind whatever it refers to to the variable x" means that you're trying to have x be the same as str, when at that line all you have is a borrowed reference to str. That's why the compiler tells you you can't create an owned value (which x would be, because it's not being created as a reference) from a reference.






          share|improve this answer










          New contributor




          dario is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          Adding to wiomoc's answer: depending on what language(s) you've previously known, variable declaration in Rust might be a little different. Whereas in C/C++ you explicitly have to declare that you want a pointer/reference variable:



          int *p = &other_int;


          In Rust it's enough to just use let, so the above would in Rust be



          let p = &other_int;


          and when you write



          let &s = &string;


          It pattern-matches that, so the Rust compiler reads it roughly as "I know I have a reference, and I want to bind whatever it is referring to to the name p". If you're not familiar with pattern-matching, a more obvious example (that works in Rust as well) would be



          let point = (23, 42);
          let (x, y) = point;


          The second line unpacks the right-hand side to match the left-hand side (both are tuples of two values) and binds the variable names on the left to the values at the same position in the structure on the right. In the case above, it's less obvious that it's matching your "structural description".



          The result of let &x = &str;, i.e. "I know &str is a reference, please bind whatever it refers to to the variable x" means that you're trying to have x be the same as str, when at that line all you have is a borrowed reference to str. That's why the compiler tells you you can't create an owned value (which x would be, because it's not being created as a reference) from a reference.







          share|improve this answer










          New contributor




          dario is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          share|improve this answer



          share|improve this answer








          edited Nov 17 at 20:58









          Shepmaster

          143k11268399




          143k11268399






          New contributor




          dario is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          answered Nov 17 at 20:49









          dario

          463




          463




          New contributor




          dario is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.





          New contributor





          dario is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.






          dario is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.
























              up vote
              1
              down vote













              You dont need that &at let x



              let str = String::from("Hallo");
              let x = &str;


              Or if you want to declare the type manually



              let string = String::from("Hallo");
              let x: &str = &string;





              share|improve this answer

























                up vote
                1
                down vote













                You dont need that &at let x



                let str = String::from("Hallo");
                let x = &str;


                Or if you want to declare the type manually



                let string = String::from("Hallo");
                let x: &str = &string;





                share|improve this answer























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  You dont need that &at let x



                  let str = String::from("Hallo");
                  let x = &str;


                  Or if you want to declare the type manually



                  let string = String::from("Hallo");
                  let x: &str = &string;





                  share|improve this answer












                  You dont need that &at let x



                  let str = String::from("Hallo");
                  let x = &str;


                  Or if you want to declare the type manually



                  let string = String::from("Hallo");
                  let x: &str = &string;






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 17 at 20:21









                  wiomoc

                  339111




                  339111






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53354917%2freference-before-assignment-in-rust%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Costa Masnaga

                      Fotorealismo

                      Sidney Franklin