{"id":99,"date":"2025-08-20T21:09:23","date_gmt":"2025-08-21T04:09:23","guid":{"rendered":"https:\/\/www.rusttutorial.com\/?page_id=99"},"modified":"2025-08-20T21:09:57","modified_gmt":"2025-08-21T04:09:57","slug":"rust-constants","status":"publish","type":"page","link":"https:\/\/www.rusttutorial.com\/rust-tutorial\/rust-constants\/","title":{"rendered":"Rust Constants"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">When you\u2019re programming in Rust, sometimes you want values that should <strong>never change<\/strong> during the execution of your program. For example, mathematical constants like \u03c0 (pi), or configuration values like the maximum number of connections allowed. In these cases, you can use Rust <strong>constants<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='what-are-constants'>What Are Constants? <a href=\"#what-are-constants\" class=\"anchor\" id=\"what-are-constants\" title=\"Anchor for What Are Constants?\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A <strong>constant<\/strong> is a value bound to a name that is <strong>always immutable<\/strong> (you can\u2019t change it).<\/li>\n\n\n\n<li>You declare constants using the <code>const<\/code> keyword, not <code>let<\/code>.<\/li>\n\n\n\n<li>Constants are evaluated at <strong>compile time<\/strong>.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Unlike <a href=\"https:\/\/www.rusttutorial.com\/rust-tutorial\/rust-variables\/\">variables<\/a>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>You <strong>cannot<\/strong> use <code>mut<\/code> with constants.<\/li>\n\n\n\n<li>You <strong>must<\/strong> specify the type of a constant.<\/li>\n\n\n\n<li>By convention, constants are written in <strong>ALL_CAPS_WITH_UNDERSCORES<\/strong>.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='defining-constants'>Defining Constants <a href=\"#defining-constants\" class=\"anchor\" id=\"defining-constants\" title=\"Anchor for Defining Constants\">#<\/a><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Here\u2019s the syntax for defining a constant in Rust:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">const<\/span> MAX_POINTS: u32 = <span class=\"hljs-number\">100<\/span>_000;\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">Explanation:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>const<\/code> \u2192 keyword to declare a constant.<\/li>\n\n\n\n<li><code>MAX_POINTS<\/code> \u2192 name of the constant (uppercase by convention).<\/li>\n\n\n\n<li><code>u32<\/code> \u2192 type annotation (required).<\/li>\n\n\n\n<li><code>100_000<\/code> \u2192 value (using underscores makes big numbers easier to read).<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='constants-vs-variables'>Constants vs. Variables <a href=\"#constants-vs-variables\" class=\"anchor\" id=\"constants-vs-variables\" title=\"Anchor for Constants vs. Variables\">#<\/a><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Feature<\/th><th>Variables (<code>let<\/code>)<\/th><th>Constants (<code>const<\/code>)<\/th><\/tr><\/thead><tbody><tr><td>Mutability<\/td><td>Immutable by default, can be mutable with <code>mut<\/code><\/td><td>Always immutable<\/td><\/tr><tr><td>Type annotation<\/td><td>Optional (Rust can infer)<\/td><td>Required<\/td><\/tr><tr><td>Evaluation<\/td><td>At runtime<\/td><td>At compile time<\/td><\/tr><tr><td>Naming convention<\/td><td>snake_case (<code>my_variable<\/code>)<\/td><td>UPPER_CASE (<code>MY_CONSTANT<\/code>)<\/td><\/tr><tr><td>Keyword<\/td><td><code>let<\/code><\/td><td><code>const<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id='using-constants-example'>Using Constants Example <a href=\"#using-constants-example\" class=\"anchor\" id=\"using-constants-example\" title=\"Anchor for Using Constants Example\">#<\/a><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The following example shows how to use constants:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Rust\" data-shcb-language-slug=\"rust\"><span><code class=\"hljs language-rust\"><span class=\"hljs-keyword\">const<\/span> PI: <span class=\"hljs-built_in\">f64<\/span> = <span class=\"hljs-number\">3.14159<\/span>;\n<span class=\"hljs-keyword\">const<\/span> SECONDS_IN_MINUTE: <span class=\"hljs-built_in\">u32<\/span> = <span class=\"hljs-number\">60<\/span>;\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">fn<\/span> <span class=\"hljs-title\">main<\/span><\/span>() {\n    <span class=\"hljs-keyword\">let<\/span> radius = <span class=\"hljs-number\">5.0<\/span>;\n    <span class=\"hljs-keyword\">let<\/span> area = PI * radius * radius;\n\n    <span class=\"hljs-built_in\">println!<\/span>(<span class=\"hljs-string\">\"Circle area = {}\"<\/span>, area);\n    <span class=\"hljs-built_in\">println!<\/span>(<span class=\"hljs-string\">\"One minute has {} seconds.\"<\/span>, SECONDS_IN_MINUTE);\n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Rust<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">rust<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">Circle area = 78.53975\nOne minute has 60 seconds.\n<\/code><\/span><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='where-can-you-use-constants'>Where Can You Use Constants? <a href=\"#where-can-you-use-constants\" class=\"anchor\" id=\"where-can-you-use-constants\" title=\"Anchor for Where Can You Use Constants?\">#<\/a><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Constants are useful when:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>You need a value used <strong>in multiple places<\/strong>.<\/li>\n\n\n\n<li>The value should <strong>never change<\/strong>.<\/li>\n\n\n\n<li>The value is <strong>known at compile time<\/strong>.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Examples:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Conversion factors (<code>SECONDS_IN_HOUR<\/code>, <code>BYTES_IN_KB<\/code>).<\/li>\n\n\n\n<li>Fixed limits (<code>MAX_USERS<\/code>, <code>MAX_CONNECTIONS<\/code>).<\/li>\n\n\n\n<li>Mathematical constants (<code>PI<\/code>, <code>E<\/code>).<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='common-mistakes-with-constants'>Common Mistakes with Constants <a href=\"#common-mistakes-with-constants\" class=\"anchor\" id=\"common-mistakes-with-constants\" title=\"Anchor for Common Mistakes with Constants\">#<\/a><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Forgetting the type annotation:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">const<\/span> PI = <span class=\"hljs-number\">3.14<\/span>; <span class=\"hljs-comment\">\/\/ ERROR: missing type<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">Trying to use <code>mut<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">const<\/span> mut LIMIT: u32 = <span class=\"hljs-number\">10<\/span>; <span class=\"hljs-comment\">\/\/ ERROR: constants can't be mutable<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use <code>const<\/code> when you want values that never change.<\/li>\n\n\n\n<li>Constants are different from variables:\n<ul class=\"wp-block-list\">\n<li>Always immutable.<\/li>\n\n\n\n<li>Type annotation required.<\/li>\n\n\n\n<li>Known at compile time.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>By convention, name them in <strong>UPPER_CASE<\/strong>.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<div class=\"wth-question\">Was this Helpful ?<\/div>\n\t<div class=\"wth-thumbs\">\n\t\t<button\n\t\t\tdata-post=\"99\"\n\t\t\tdata-post-url=\"https:\/\/www.rusttutorial.com\/rust-tutorial\/rust-constants\/\"\n\t\t\tdata-post-title=\"Rust Constants\"\n\t\t\tdata-response=\"1\"\n\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t>\n\t\t\t<svg\n\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\tfill=\"none\"\n\t\t\t\tstroke=\"currentColor\"\n\t\t\t\tstroke-width=\"2\"\n\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t>\n\t\t\t\t<path\n\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t><\/path>\n\t\t\t<\/svg>\n\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t<\/button>\n\n\t\t<button\n\t\t\tdata-response=\"0\"\n\t\t\tdata-post=\"99\"\n\t\t\tdata-post-url=\"https:\/\/www.rusttutorial.com\/rust-tutorial\/rust-constants\/\"\n\t\t\tdata-post-title=\"Rust Constants\"\n\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t>\n\t\t\t<svg\n\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\tfill=\"none\"\n\t\t\t\tstroke=\"currentColor\"\n\t\t\t\tstroke-width=\"2\"\n\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t>\n\t\t\t\t<path\n\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t><\/path>\n\t\t\t<\/svg>\n\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t<\/button>\n\t<\/div>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>When you\u2019re programming in Rust, sometimes you want values that should never change during the execution of your program. For example, mathematical constants like \u03c0 (pi), or configuration values like the maximum number of connections allowed. In these cases, you can use Rust constants.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":9,"menu_order":6,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-99","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Rust Constants<\/title>\n<meta name=\"description\" content=\"You may want values that should never change during the execution of your program. In these cases, you can use Rust constants.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.rusttutorial.com\/rust-tutorial\/rust-constants\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Rust Constants\" \/>\n<meta property=\"og:description\" content=\"You may want values that should never change during the execution of your program. In these cases, you can use Rust constants.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.rusttutorial.com\/rust-tutorial\/rust-constants\/\" \/>\n<meta property=\"og:site_name\" content=\"Rust Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2025-08-21T04:09:57+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.rusttutorial.com\/rust-tutorial\/rust-constants\/\",\"url\":\"https:\/\/www.rusttutorial.com\/rust-tutorial\/rust-constants\/\",\"name\":\"Rust Constants\",\"isPartOf\":{\"@id\":\"https:\/\/www.rusttutorial.com\/#website\"},\"datePublished\":\"2025-08-21T04:09:23+00:00\",\"dateModified\":\"2025-08-21T04:09:57+00:00\",\"description\":\"You may want values that should never change during the execution of your program. In these cases, you can use Rust constants.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.rusttutorial.com\/rust-tutorial\/rust-constants\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.rusttutorial.com\/rust-tutorial\/rust-constants\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.rusttutorial.com\/rust-tutorial\/rust-constants\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.rusttutorial.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Rust Tutorial\",\"item\":\"https:\/\/www.rusttutorial.com\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Rust Constants\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.rusttutorial.com\/#website\",\"url\":\"https:\/\/www.rusttutorial.com\/\",\"name\":\"Rust Tutorial\",\"description\":\"Learn Rust Programming from Scratch\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.rusttutorial.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Rust Constants","description":"You may want values that should never change during the execution of your program. In these cases, you can use Rust constants.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.rusttutorial.com\/rust-tutorial\/rust-constants\/","og_locale":"en_US","og_type":"article","og_title":"Rust Constants","og_description":"You may want values that should never change during the execution of your program. In these cases, you can use Rust constants.","og_url":"https:\/\/www.rusttutorial.com\/rust-tutorial\/rust-constants\/","og_site_name":"Rust Tutorial","article_modified_time":"2025-08-21T04:09:57+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.rusttutorial.com\/rust-tutorial\/rust-constants\/","url":"https:\/\/www.rusttutorial.com\/rust-tutorial\/rust-constants\/","name":"Rust Constants","isPartOf":{"@id":"https:\/\/www.rusttutorial.com\/#website"},"datePublished":"2025-08-21T04:09:23+00:00","dateModified":"2025-08-21T04:09:57+00:00","description":"You may want values that should never change during the execution of your program. In these cases, you can use Rust constants.","breadcrumb":{"@id":"https:\/\/www.rusttutorial.com\/rust-tutorial\/rust-constants\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.rusttutorial.com\/rust-tutorial\/rust-constants\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.rusttutorial.com\/rust-tutorial\/rust-constants\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.rusttutorial.com\/"},{"@type":"ListItem","position":2,"name":"Rust Tutorial","item":"https:\/\/www.rusttutorial.com\/"},{"@type":"ListItem","position":3,"name":"Rust Constants"}]},{"@type":"WebSite","@id":"https:\/\/www.rusttutorial.com\/#website","url":"https:\/\/www.rusttutorial.com\/","name":"Rust Tutorial","description":"Learn Rust Programming from Scratch","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.rusttutorial.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.rusttutorial.com\/wp-json\/wp\/v2\/pages\/99","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rusttutorial.com\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.rusttutorial.com\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.rusttutorial.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rusttutorial.com\/wp-json\/wp\/v2\/comments?post=99"}],"version-history":[{"count":3,"href":"https:\/\/www.rusttutorial.com\/wp-json\/wp\/v2\/pages\/99\/revisions"}],"predecessor-version":[{"id":102,"href":"https:\/\/www.rusttutorial.com\/wp-json\/wp\/v2\/pages\/99\/revisions\/102"}],"up":[{"embeddable":true,"href":"https:\/\/www.rusttutorial.com\/wp-json\/wp\/v2\/pages\/9"}],"wp:attachment":[{"href":"https:\/\/www.rusttutorial.com\/wp-json\/wp\/v2\/media?parent=99"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}