# Rust Scripting

**URL:** <https://internals.rust-lang.org/t/rust-scripting/12400>\
**Category:** language design\
**Created:** [May 21, 2020, 7:55pm UTC](https://internals.rust-lang.org/t/rust-scripting/12400 "2020-05-21T19:55:40Z")\
**Posts on this page:** 8\
**Page:** 2

<div class="post-metadata">

**Author:** ![redradist](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/redradist/32/6475_2.png) [@redradist](https://internals.rust-lang.org/u/redradist)\
**Post date:** [June 12, 2020, 4:09pm UTC](https://internals.rust-lang.org/t/rust-scripting/12400/21 "2020-06-12T16:09:59Z")

</div>

This crate shows the need for scripting with rust ...

Actually it is very easy from my point of view:

```rust
// Add main function only at compile time and 
// in case if program was run as rustc net.rs and 
// there was no main function it self
fn main() {
    ... wrap any code that was in file
}

```

I do not understand why we cannot do as it was described in this comment

In case if the was detected main function a generated code will look like this:

```rust
fn main() {
  ... some top-level code
  fn custom_main() {
    ...
  }
  custom_main();
}

```

Original `main` function that is in file will be with different name `custom_main`

---

<div class="post-metadata">

**Author:** ![Aloso](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/aloso/32/5039_2.png) [@Aloso](https://internals.rust-lang.org/u/Aloso)\
**Post date:** [June 12, 2020, 6:20pm UTC](https://internals.rust-lang.org/t/rust-scripting/12400/22 "2020-06-12T18:20:30Z")

</div>

Even if it is easy to implement, it's a major change to the language whose only benefit is that it saves you 12 key strokes. I don't see how this could be justified _as a language feature_.

However, it's absolutely possible to implement this in the playground, or in one of the previously mentioned tools.

The playground already detects if the code contains a main function. I guess it could also provide a button to automatically wrap the code in a main function.

---

<div class="post-metadata">

**Author:** ![skysch](https://avatars.discourse-cdn.com/v4/letter/s/f05b48/32.png) [@skysch](https://internals.rust-lang.org/u/skysch)\
**Post date:** [June 12, 2020, 10:34pm UTC](https://internals.rust-lang.org/t/rust-scripting/12400/23 "2020-06-12T22:34:48Z")

</div>

Rust items are not required to be in order, while statements are. It can be very confusing to have both of them in the same scope, and best practice would be to put statements inside a main function even if this were an option.

---

<div class="post-metadata">

**Author:** ![redradist](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/redradist/32/6475_2.png) [@redradist](https://internals.rust-lang.org/u/redradist)\
**Post date:** [June 16, 2020, 12:13am UTC](https://internals.rust-lang.org/t/rust-scripting/12400/24 "2020-06-16T00:13:27Z")

</div>

> [@CAD97](#):
>
> Add to this the fact that any "real" script will need access to cargo packages and it more and more looks like you should be using [cargo-script](https://crates.io/crates/cargo-script), which IIUC you can set up to wrap everything in a `fn main()` and get "top level expressions" that way.

How to configure cargo-script to wrap everything in a `fn main()` ? I have not found in documentation ...

---

<div class="post-metadata">

**Author:** ![mbrubeck](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mbrubeck/32/174_2.png) [@mbrubeck](https://internals.rust-lang.org/u/mbrubeck)\
**Post date:** [June 16, 2020, 1:26am UTC](https://internals.rust-lang.org/t/rust-scripting/12400/25 "2020-06-16T01:26:30Z")

</div>

> [@redradist](#):
>
> How to configure cargo-script to wrap everything in a `fn main()` ? I have not found in documentation ...

That was a suggested feature that has not been implemented:

> [@Rust Scripting](https://internals.rust-lang.org/t/rust-scripting/12400/6):
>
> cargo-script or a similar tool could be extended to auto-generate a main wrapper if the script does not already contain a main function. This is what rustdoc already does for running code found in doc comments. On a separate note, with my moderator’s hat on: @redradist, please note that the bar for changes to the Rust is very high. Before posting proposals, please consider looking more closely at alternatives and prior work, including ways of solving the problem outside of the core language.…

---

<div class="post-metadata">

**Author:** ![redradist](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/redradist/32/6475_2.png) [@redradist](https://internals.rust-lang.org/u/redradist)\
**Post date:** [June 19, 2020, 2:33pm UTC](https://internals.rust-lang.org/t/rust-scripting/12400/26 "2020-06-19T14:33:47Z")

</div>

> [@Aloso](#):
>
> Even if it is easy to implement, it's a major change to the language whose only benefit is that it saves you 12 key strokes. I don't see how this could be justified _as a language feature_.
> 
> However, it's absolutely possible to implement this in the playground, or in one of the previously mentioned tools.
> 
> The playground already detects if the code contains a main function. I guess it could also provide a button to automatically wrap the code in a main function.

The main reason is that script mode gives better readability in some cases: Instead of searching for main function, for scripting mode you will need to read file from top to bottom and it is much simpler and nicer as for me 😉

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [June 19, 2020, 6:35pm UTC](https://internals.rust-lang.org/t/rust-scripting/12400/27 "2020-06-19T18:35:10Z")

</div>

> [@redradist](#):
>
> Instead of searching for main function, for scripting mode you will need to read file from top to bottom

This definitely sounds odd.

"Instead of just (finding and) reading `fn main`, you just read the whole file from top to bottom!"

It sounds like the latter is more work and more likely to cause missed code.

And again, I'd like to point at Python again, where it's best practice for any non-extremely-trivial script to nest all of its "executed as script" code under a `if __name__ == " __main__"` header, rather than strewn about the file.

Or, in other words, a `fn main()`.

Top level statements for scripting makes the already easy case easier, and any moderate complexity a lot more potentially problematic.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [September 17, 2020, 6:35pm UTC](https://internals.rust-lang.org/t/rust-scripting/12400/28 "2020-09-17T18:35:10Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

[Previous page](https://internals.rust-lang.org/t/rust-scripting/12400.md?page=1)
