Files
GopherGate/target/doc/tokio_macros/attr.main.html
2026-02-26 12:00:21 -05:00

175 lines
16 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="Marks async function to be executed by the selected runtime. This macro helps set up a `Runtime` without requiring the user to use Runtime or Builder directly."><title>main in tokio_macros - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Italic-81dc35de.woff2,FiraSans-Regular-0fe48ade.woff2,FiraSans-MediumItalic-ccf7e434.woff2,FiraSans-Medium-e1aa3f0a.woff2,SourceCodePro-Regular-8badfe75.ttf.woff2,SourceCodePro-Semibold-aa29a496.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2"href="../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../static.files/normalize-9960930a.css"><link rel="stylesheet" href="../static.files/rustdoc-ca0dd0c4.css"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="tokio_macros" data-themes="" data-resource-suffix="" data-rustdoc-version="1.93.1 (01f6ddf75 2026-02-11) (Arch Linux rust 1:1.93.1-1)" data-channel="1.93.1" data-search-js="search-9e2438ea.js" data-stringdex-js="stringdex-a3946164.js" data-settings-js="settings-c38705f0.js" ><script src="../static.files/storage-e2aeef58.js"></script><script defer src="sidebar-items.js"></script><script defer src="../static.files/main-a410ff4d.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-263c88ec.css"></noscript><link rel="alternate icon" type="image/png" href="../static.files/favicon-32x32-eab170b8.png"><link rel="icon" type="image/svg+xml" href="../static.files/favicon-044be391.svg"></head><body class="rustdoc attr"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><rustdoc-topbar><h2><a href="#">main</a></h2></rustdoc-topbar><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../tokio_macros/index.html">tokio_<wbr>macros</a><span class="version">2.6.0</span></h2></div><div class="sidebar-elems"><section id="rustdoc-toc"><h2 class="location"><a href="#">main</a></h2><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#non-worker-async-function" title="Non-worker async function">Non-worker async function</a></li><li><a href="#runtime-flavors" title="Runtime flavors">Runtime flavors</a><ul><li><a href="#multi-threaded" title="Multi-threaded">Multi-threaded</a></li><li><a href="#current-thread" title="Current-thread">Current-thread</a></li><li><a href="#local" title="Local">Local</a></li></ul></li><li><a href="#function-arguments" title="Function arguments">Function arguments</a></li><li><a href="#usage" title="Usage">Usage</a><ul><li><a href="#using-the-multi-threaded-runtime" title="Using the multi-threaded runtime">Using the multi-threaded runtime</a></li><li><a href="#using-the-current-thread-runtime" title="Using the current-thread runtime">Using the current-thread runtime</a></li><li><a href="#using-the-local-runtime" title="Using the local runtime">Using the local runtime</a></li><li><a href="#set-number-of-worker-threads" title="Set number of worker threads">Set number of worker threads</a></li><li><a href="#configure-the-runtime-to-start-with-time-paused" title="Configure the runtime to start with time paused">Configure the runtime to start with time paused</a></li><li><a href="#rename-package" title="Rename package">Rename package</a></li><li><a href="#configure-unhandled-panic-behavior" title="Configure unhandled panic behavior">Configure unhandled panic behavior</a></li></ul></li></ul></section><div id="rustdoc-modnav"><h2 class="in-crate"><a href="index.html">In crate tokio_<wbr>macros</a></h2></div></div></nav><div class="sidebar-resizer" title="Drag to resize sidebar"></div><main><div class="width-limiter"><section id="main-content" class="content"><div class="main-heading"><div class="rustdoc-breadcrumbs"><a href="index.html">tokio_macros</a></div><h1>Attribute Macro <span class="attr">main</span>&nbsp;<button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><rustdoc-toolbar></rustdoc-toolbar><span class="sub-heading"><a class="src" href="../src/tokio_macros/lib.rs.html#306-308">Source</a> </span></div><pre class="rust item-decl"><code>#[main]</code></pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Marks async function to be executed by the selected runtime. This macro
helps set up a <code>Runtime</code> without requiring the user to use
<a href="../tokio/runtime/struct.Runtime.html">Runtime</a> or
<a href="../tokio/runtime/struct.Builder.html">Builder</a> directly.</p>
<p>Note: This macro is designed to be simplistic and targets applications that
do not require a complex setup. If the provided functionality is not
sufficient, you may be interested in using
<a href="../tokio/runtime/struct.Builder.html">Builder</a>, which provides a more
powerful interface.</p>
<p>Note: This macro can be used on any function and not just the <code>main</code>
function. Using it on a non-main function makes the function behave as if it
was synchronous by starting a new runtime each time it is called. If the
function is called often, it is preferable to create the runtime using the
runtime builder so the runtime can be reused across calls.</p>
<h2 id="non-worker-async-function"><a class="doc-anchor" href="#non-worker-async-function">§</a>Non-worker async function</h2>
<p>Note that the async function marked with this macro does not run as a
worker. The expectation is that other tasks are spawned by the function here.
Awaiting on other futures from the function provided here will not
perform as fast as those spawned as workers.</p>
<h2 id="runtime-flavors"><a class="doc-anchor" href="#runtime-flavors">§</a>Runtime flavors</h2>
<p>The macro can be configured with a <code>flavor</code> parameter to select
different runtime configurations.</p>
<h3 id="multi-threaded"><a class="doc-anchor" href="#multi-threaded">§</a>Multi-threaded</h3>
<p>To use the multi-threaded runtime, the macro can be configured using</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attr">#[tokio::main(flavor = <span class="string">"multi_thread"</span>, worker_threads = <span class="number">10</span>)]</span></code></pre></div>
<p>The <code>worker_threads</code> option configures the number of worker threads, and
defaults to the number of cpus on the system. This is the default flavor.</p>
<p>Note: The multi-threaded runtime requires the <code>rt-multi-thread</code> feature
flag.</p>
<h3 id="current-thread"><a class="doc-anchor" href="#current-thread">§</a>Current-thread</h3>
<p>To use the single-threaded runtime known as the <code>current_thread</code> runtime,
the macro can be configured using</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attr">#[tokio::main(flavor = <span class="string">"current_thread"</span>)]</span></code></pre></div><h3 id="local"><a class="doc-anchor" href="#local">§</a>Local</h3>
<p><a href="../tokio/index.html#unstable-features">Unstable API</a> only.</p>
<p>To use the <a href="../tokio/runtime/struct.LocalRuntime.html">local runtime</a>, the macro can be configured using</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attr">#[tokio::main(flavor = <span class="string">"local"</span>)]</span></code></pre></div><h2 id="function-arguments"><a class="doc-anchor" href="#function-arguments">§</a>Function arguments</h2>
<p>Arguments are allowed for any functions, aside from <code>main</code> which is special.</p>
<h2 id="usage"><a class="doc-anchor" href="#usage">§</a>Usage</h2><h3 id="using-the-multi-threaded-runtime"><a class="doc-anchor" href="#using-the-multi-threaded-runtime">§</a>Using the multi-threaded runtime</h3>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attr">#[tokio::main]
</span><span class="kw">async fn </span>main() {
<span class="macro">println!</span>(<span class="string">"Hello world"</span>);
}</code></pre></div>
<p>Equivalent code not using <code>#[tokio::main]</code></p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">fn </span>main() {
tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.unwrap()
.block_on(<span class="kw">async </span>{
<span class="macro">println!</span>(<span class="string">"Hello world"</span>);
})
}</code></pre></div><h3 id="using-the-current-thread-runtime"><a class="doc-anchor" href="#using-the-current-thread-runtime">§</a>Using the current-thread runtime</h3>
<p>The basic scheduler is single-threaded.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attr">#[tokio::main(flavor = <span class="string">"current_thread"</span>)]
</span><span class="kw">async fn </span>main() {
<span class="macro">println!</span>(<span class="string">"Hello world"</span>);
}</code></pre></div>
<p>Equivalent code not using <code>#[tokio::main]</code></p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">fn </span>main() {
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap()
.block_on(<span class="kw">async </span>{
<span class="macro">println!</span>(<span class="string">"Hello world"</span>);
})
}</code></pre></div><h3 id="using-the-local-runtime"><a class="doc-anchor" href="#using-the-local-runtime">§</a>Using the local runtime</h3>
<p>Available in the <a href="../tokio/index.html#unstable-features">unstable API</a> only.</p>
<p>The <a href="../tokio/runtime/struct.LocalRuntime.html">local runtime</a> is similar to the current-thread runtime but
supports <a href="../tokio/task/fn.spawn_local.html"><code>task::spawn_local</code></a>.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attr">#[tokio::main(flavor = <span class="string">"local"</span>)]
</span><span class="kw">async fn </span>main() {
<span class="macro">println!</span>(<span class="string">"Hello world"</span>);
}</code></pre></div>
<p>Equivalent code not using <code>#[tokio::main]</code></p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">fn </span>main() {
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build_local(tokio::runtime::LocalOptions::default())
.unwrap()
.block_on(<span class="kw">async </span>{
<span class="macro">println!</span>(<span class="string">"Hello world"</span>);
})
}</code></pre></div><h3 id="set-number-of-worker-threads"><a class="doc-anchor" href="#set-number-of-worker-threads">§</a>Set number of worker threads</h3>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attr">#[tokio::main(worker_threads = <span class="number">2</span>)]
</span><span class="kw">async fn </span>main() {
<span class="macro">println!</span>(<span class="string">"Hello world"</span>);
}</code></pre></div>
<p>Equivalent code not using <code>#[tokio::main]</code></p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">fn </span>main() {
tokio::runtime::Builder::new_multi_thread()
.worker_threads(<span class="number">2</span>)
.enable_all()
.build()
.unwrap()
.block_on(<span class="kw">async </span>{
<span class="macro">println!</span>(<span class="string">"Hello world"</span>);
})
}</code></pre></div><h3 id="configure-the-runtime-to-start-with-time-paused"><a class="doc-anchor" href="#configure-the-runtime-to-start-with-time-paused">§</a>Configure the runtime to start with time paused</h3>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attr">#[tokio::main(flavor = <span class="string">"current_thread"</span>, start_paused = <span class="bool-val">true</span>)]
</span><span class="kw">async fn </span>main() {
<span class="macro">println!</span>(<span class="string">"Hello world"</span>);
}</code></pre></div>
<p>Equivalent code not using <code>#[tokio::main]</code></p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">fn </span>main() {
tokio::runtime::Builder::new_current_thread()
.enable_all()
.start_paused(<span class="bool-val">true</span>)
.build()
.unwrap()
.block_on(<span class="kw">async </span>{
<span class="macro">println!</span>(<span class="string">"Hello world"</span>);
})
}</code></pre></div>
<p>Note that <code>start_paused</code> requires the <code>test-util</code> feature to be enabled.</p>
<h3 id="rename-package"><a class="doc-anchor" href="#rename-package">§</a>Rename package</h3>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio <span class="kw">as </span>tokio1;
<span class="attr">#[tokio1::main(<span class="kw">crate </span>= <span class="string">"tokio1"</span>)]
</span><span class="kw">async fn </span>main() {
<span class="macro">println!</span>(<span class="string">"Hello world"</span>);
}</code></pre></div>
<p>Equivalent code not using <code>#[tokio::main]</code></p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio <span class="kw">as </span>tokio1;
<span class="kw">fn </span>main() {
tokio1::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.unwrap()
.block_on(<span class="kw">async </span>{
<span class="macro">println!</span>(<span class="string">"Hello world"</span>);
})
}</code></pre></div><h3 id="configure-unhandled-panic-behavior"><a class="doc-anchor" href="#configure-unhandled-panic-behavior">§</a>Configure unhandled panic behavior</h3>
<p>Available options are <code>shutdown_runtime</code> and <code>ignore</code>. For more details, see
<a href="../tokio/runtime/struct.Builder.html#method.unhandled_panic"><code>Builder::unhandled_panic</code></a>.</p>
<p>This option is only compatible with the <code>current_thread</code> runtime.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attr">#[cfg(tokio_unstable)]
#[tokio::main(flavor = <span class="string">"current_thread"</span>, unhandled_panic = <span class="string">"shutdown_runtime"</span>)]
</span><span class="kw">async fn </span>main() {
<span class="kw">let _ </span>= tokio::spawn(<span class="kw">async </span>{
<span class="macro">panic!</span>(<span class="string">"This panic will shutdown the runtime."</span>);
}).<span class="kw">await</span>;
}</code></pre></div>
<p>Equivalent code not using <code>#[tokio::main]</code></p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attr">#[cfg(tokio_unstable)]
</span><span class="kw">fn </span>main() {
tokio::runtime::Builder::new_current_thread()
.enable_all()
.unhandled_panic(tokio::runtime::UnhandledPanic::ShutdownRuntime)
.build()
.unwrap()
.block_on(<span class="kw">async </span>{
<span class="kw">let _ </span>= tokio::spawn(<span class="kw">async </span>{
<span class="macro">panic!</span>(<span class="string">"This panic will shutdown the runtime."</span>);
}).<span class="kw">await</span>;
})
}</code></pre></div>
<p><strong>Note</strong>: This option depends on Tokios <a href="../tokio/index.html#unstable-features">unstable API</a>. See <a href="../tokio/index.html#unstable-features">the
documentation on unstable features</a> for details on how to enable
Tokios unstable features.</p>
</div></details></section></div></main></body></html>