What are examples and what are they used for?









up vote
5
down vote

favorite












The directory layout of a Rust project should look like this (source)



.
├── Cargo.lock
├── Cargo.toml
├── benches
│ └── large-input.rs
├── examples
│ └── simple.rs
├── src
│ ├── bin
│ │ └── another_executable.rs
│ ├── lib.rs
│ └── main.rs
└── tests
└── some-integration-tests.rs


What is the file simple.rs under examples? How do I execute it? How should the file look like?










share|improve this question



























    up vote
    5
    down vote

    favorite












    The directory layout of a Rust project should look like this (source)



    .
    ├── Cargo.lock
    ├── Cargo.toml
    ├── benches
    │ └── large-input.rs
    ├── examples
    │ └── simple.rs
    ├── src
    │ ├── bin
    │ │ └── another_executable.rs
    │ ├── lib.rs
    │ └── main.rs
    └── tests
    └── some-integration-tests.rs


    What is the file simple.rs under examples? How do I execute it? How should the file look like?










    share|improve this question

























      up vote
      5
      down vote

      favorite









      up vote
      5
      down vote

      favorite











      The directory layout of a Rust project should look like this (source)



      .
      ├── Cargo.lock
      ├── Cargo.toml
      ├── benches
      │ └── large-input.rs
      ├── examples
      │ └── simple.rs
      ├── src
      │ ├── bin
      │ │ └── another_executable.rs
      │ ├── lib.rs
      │ └── main.rs
      └── tests
      └── some-integration-tests.rs


      What is the file simple.rs under examples? How do I execute it? How should the file look like?










      share|improve this question















      The directory layout of a Rust project should look like this (source)



      .
      ├── Cargo.lock
      ├── Cargo.toml
      ├── benches
      │ └── large-input.rs
      ├── examples
      │ └── simple.rs
      ├── src
      │ ├── bin
      │ │ └── another_executable.rs
      │ ├── lib.rs
      │ └── main.rs
      └── tests
      └── some-integration-tests.rs


      What is the file simple.rs under examples? How do I execute it? How should the file look like?







      rust rust-cargo






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 2 days ago









      Shepmaster

      142k11264398




      142k11264398










      asked 2 days ago









      hellow

      3,61311941




      3,61311941






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted










          Examples are useful in library crates to show how the crate is used.



          An example can be an executable with a main method or a library; it can either be in a single file examples/example-name.rs or consist of several files in a subdirectory examples/example-name/, with the main method in main.rs. To compile a library example you need to specify its crate type in Cargo.toml:



          [[example]]
          name = "example-name"
          crate-type = ["lib"]


          Examples are compiled by cargo test to ensure that they are up to date with the crate. You can run a specific executable example by



          cargo run --example <example-name>


          and selectively build any example with



          cargo build --example <example-name>


          This is documented in the Cargo Reference.






          share|improve this answer


















          • 1




            Could you add to the answer what the file should look like; I would suppose it should be a binary (ie, have a fn main() item), but without clicking the links I cannot be sure.
            – Matthieu M.
            2 days ago










          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%2f53225235%2fwhat-are-examples-and-what-are-they-used-for%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          4
          down vote



          accepted










          Examples are useful in library crates to show how the crate is used.



          An example can be an executable with a main method or a library; it can either be in a single file examples/example-name.rs or consist of several files in a subdirectory examples/example-name/, with the main method in main.rs. To compile a library example you need to specify its crate type in Cargo.toml:



          [[example]]
          name = "example-name"
          crate-type = ["lib"]


          Examples are compiled by cargo test to ensure that they are up to date with the crate. You can run a specific executable example by



          cargo run --example <example-name>


          and selectively build any example with



          cargo build --example <example-name>


          This is documented in the Cargo Reference.






          share|improve this answer


















          • 1




            Could you add to the answer what the file should look like; I would suppose it should be a binary (ie, have a fn main() item), but without clicking the links I cannot be sure.
            – Matthieu M.
            2 days ago














          up vote
          4
          down vote



          accepted










          Examples are useful in library crates to show how the crate is used.



          An example can be an executable with a main method or a library; it can either be in a single file examples/example-name.rs or consist of several files in a subdirectory examples/example-name/, with the main method in main.rs. To compile a library example you need to specify its crate type in Cargo.toml:



          [[example]]
          name = "example-name"
          crate-type = ["lib"]


          Examples are compiled by cargo test to ensure that they are up to date with the crate. You can run a specific executable example by



          cargo run --example <example-name>


          and selectively build any example with



          cargo build --example <example-name>


          This is documented in the Cargo Reference.






          share|improve this answer


















          • 1




            Could you add to the answer what the file should look like; I would suppose it should be a binary (ie, have a fn main() item), but without clicking the links I cannot be sure.
            – Matthieu M.
            2 days ago












          up vote
          4
          down vote



          accepted







          up vote
          4
          down vote



          accepted






          Examples are useful in library crates to show how the crate is used.



          An example can be an executable with a main method or a library; it can either be in a single file examples/example-name.rs or consist of several files in a subdirectory examples/example-name/, with the main method in main.rs. To compile a library example you need to specify its crate type in Cargo.toml:



          [[example]]
          name = "example-name"
          crate-type = ["lib"]


          Examples are compiled by cargo test to ensure that they are up to date with the crate. You can run a specific executable example by



          cargo run --example <example-name>


          and selectively build any example with



          cargo build --example <example-name>


          This is documented in the Cargo Reference.






          share|improve this answer














          Examples are useful in library crates to show how the crate is used.



          An example can be an executable with a main method or a library; it can either be in a single file examples/example-name.rs or consist of several files in a subdirectory examples/example-name/, with the main method in main.rs. To compile a library example you need to specify its crate type in Cargo.toml:



          [[example]]
          name = "example-name"
          crate-type = ["lib"]


          Examples are compiled by cargo test to ensure that they are up to date with the crate. You can run a specific executable example by



          cargo run --example <example-name>


          and selectively build any example with



          cargo build --example <example-name>


          This is documented in the Cargo Reference.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 2 days ago

























          answered 2 days ago









          starblue

          44.3k1175135




          44.3k1175135







          • 1




            Could you add to the answer what the file should look like; I would suppose it should be a binary (ie, have a fn main() item), but without clicking the links I cannot be sure.
            – Matthieu M.
            2 days ago












          • 1




            Could you add to the answer what the file should look like; I would suppose it should be a binary (ie, have a fn main() item), but without clicking the links I cannot be sure.
            – Matthieu M.
            2 days ago







          1




          1




          Could you add to the answer what the file should look like; I would suppose it should be a binary (ie, have a fn main() item), but without clicking the links I cannot be sure.
          – Matthieu M.
          2 days ago




          Could you add to the answer what the file should look like; I would suppose it should be a binary (ie, have a fn main() item), but without clicking the links I cannot be sure.
          – Matthieu M.
          2 days ago

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53225235%2fwhat-are-examples-and-what-are-they-used-for%23new-answer', 'question_page');

          );

          Post as a guest














































































          Popular posts from this blog

          Kleinkühnau

          Makov (Slowakei)

          Deutsches Schauspielhaus