blob: aaf68a998547b03606461d22282f8c9700dde7b8 [file] [log] [blame]
<!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."><meta name="keywords" content="rust, rustlang, rust-lang, main"><title>main in tokio - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" href="../normalize.css"><link rel="stylesheet" href="../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" href="../ayu.css" disabled><link rel="stylesheet" href="../dark.css" disabled><link rel="stylesheet" href="../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../storage.js"></script><script defer src="sidebar-items.js"></script><script defer src="../main.js"></script><noscript><link rel="stylesheet" href="../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../favicon.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]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle">&#9776;</button><a class="sidebar-logo" href="../tokio/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><h2></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../tokio/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><div class="sidebar-elems"><h2><a href="index.html">In tokio</a></h2></div></nav><main><div class="width-limiter"><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><a href="../help.html">?</a></div><div id="settings-menu" tabindex="-1"><a href="../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../wheel.svg"></a></div></div></form></nav><section id="main-content" class="content"><div class="main-heading"><h1 class="fqn">Attribute Macro <a href="index.html">tokio</a>::<wbr><a class="attr" href="#">main</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../clipboard.svg" width="19" height="18" alt="Copy item path"></button></h1><span class="out-of-band"><a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">&#x2212;</span>]</a></span></div><div class="item-decl"><pre class="rust attr"><code>#[main]</code></pre></div><details class="rustdoc-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="multi-threaded-runtime"><a href="#multi-threaded-runtime">Multi-threaded runtime</a></h2>
<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="attribute">#[tokio::main(flavor = <span class="string">&quot;multi_thread&quot;</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>
<h2 id="current-thread-runtime"><a href="#current-thread-runtime">Current thread runtime</a></h2>
<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="attribute">#[tokio::main(flavor = <span class="string">&quot;current_thread&quot;</span>)]</span></code></pre></div>
<h3 id="function-arguments"><a href="#function-arguments">Function arguments:</a></h3>
<p>Arguments are allowed for any functions aside from <code>main</code> which is special</p>
<h3 id="usage"><a href="#usage">Usage</a></h3><h4 id="using-the-multi-thread-runtime"><a href="#using-the-multi-thread-runtime">Using the multi-thread runtime</a></h4>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attribute">#[tokio::main]
</span><span class="kw">async fn </span>main() {
<span class="macro">println!</span>(<span class="string">&quot;Hello world&quot;</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">&quot;Hello world&quot;</span>);
})
}</code></pre></div>
<h4 id="using-current-thread-runtime"><a href="#using-current-thread-runtime">Using current thread runtime</a></h4>
<p>The basic scheduler is single-threaded.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attribute">#[tokio::main(flavor = <span class="string">&quot;current_thread&quot;</span>)]
</span><span class="kw">async fn </span>main() {
<span class="macro">println!</span>(<span class="string">&quot;Hello world&quot;</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">&quot;Hello world&quot;</span>);
})
}</code></pre></div>
<h4 id="set-number-of-worker-threads"><a href="#set-number-of-worker-threads">Set number of worker threads</a></h4>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attribute">#[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">&quot;Hello world&quot;</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">&quot;Hello world&quot;</span>);
})
}</code></pre></div>
<h4 id="configure-the-runtime-to-start-with-time-paused"><a href="#configure-the-runtime-to-start-with-time-paused">Configure the runtime to start with time paused</a></h4>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attribute">#[tokio::main(flavor = <span class="string">&quot;current_thread&quot;</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">&quot;Hello world&quot;</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">&quot;Hello world&quot;</span>);
})
}</code></pre></div>
<p>Note that <code>start_paused</code> requires the <code>test-util</code> feature to be enabled.</p>
<h4 id="rename-package"><a href="#rename-package">Rename package</a></h4>
<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="attribute">#[tokio1::main(<span class="kw">crate </span>= <span class="string">&quot;tokio1&quot;</span>)]
</span><span class="kw">async fn </span>main() {
<span class="macro">println!</span>(<span class="string">&quot;Hello world&quot;</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">&quot;Hello world&quot;</span>);
})
}</code></pre></div>
</div></details></section></div></main><div id="rustdoc-vars" data-root-path="../" data-current-crate="tokio" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.66.0-nightly (5c8bff74b 2022-10-21)" ></div></body></html>