1"use strict";(self.webpackChunk_neon_website=self.webpackChunk_neon_website||[]).push([[65],{8453:(e,t,n)=>{n.d(t,{R:()=>o,x:()=>l});var s=n(6540);const i={},r=s.createContext(i);function o(e){const t=s.useContext(r);return s.useMemo(function(){return"function"==typeof e?e(t):{...t,...e}},[t,e])}function l(e){let t;return t=e.disableParentContext?"function"==typeof e.components?e.components(i):e.components||i:o(e.components),s.createElement(r.Provider,{value:t},e.children)}},8931:(e,t,n)=>{n.r(t),n.d(t,{assets:()=>c,contentTitle:()=>l,default:()=>h,frontMatter:()=>o,metadata:()=>s,toc:()=>a});const s=JSON.parse('{"id":"primitive-types","title":"Primitive Types","description":"In JavaScript, the primitive types are types of values that aren\'t objects. This page gives some simple examples of how to construct instances of various JavaScript primitive types, and you can follow the links to more detailed information in the API documentation.","source":"@site/docs/primitive-types.md","sourceDirName":".","slug":"/primitive-types","permalink":"/docs/primitive-types","draft":false,"unlisted":false,"tags":[],"version":"current","frontMatter":{"id":"primitive-types","title":"Primitive Types","sidebar_label":"Primitive Types"},"sidebar":"docs","previous":{"title":"Hello World!","permalink":"/docs/hello-world"},"next":{"title":"Objects","permalink":"/docs/objects"}}');var i=n(4848),r=n(8453);const o={id:"primitive-types",title:"Primitive Types",sidebar_label:"Primitive Types"},l=void 0,c={},a=[{value:"Numbers",id:"numbers",level:2},{value:"Strings",id:"strings",level:2},{value:"Booleans",id:"booleans",level:2},{value:"Undefined",id:"undefined",level:2},{value:"Null",id:"null",level:2}];function d(e){const t={a:"a",code:"code",h2:"h2",p:"p",pre:"pre",strong:"strong",...(0,r.R)(),...e.components};return(0,i.jsxs)(i.Fragment,{children:[(0,i.jsxs)(t.p,{children:["In JavaScript, the ",(0,i.jsx)(t.strong,{children:(0,i.jsx)(t.a,{href:"https://developer.mozilla.org/en-US/docs/Glossary/Primitive",children:"primitive types"})})," are types of values that aren't objects. This page gives some simple examples of how to construct instances of various JavaScript primitive types, and you can follow the links to more detailed information in the API documentation."]}),"\n",(0,i.jsx)(t.h2,{id:"numbers",children:"Numbers"}),"\n",(0,i.jsxs)(t.p,{children:["The ",(0,i.jsx)(t.a,{href:"https://docs.rs/neon/latest/neon/context/trait.Context.html#method.number",children:(0,i.jsx)(t.code,{children:"Context::number()"})})," method constructs a JavaScript number from any Rust number compatible with the ",(0,i.jsx)(t.a,{href:"https://doc.rust-lang.org/std/primitive.f64.html",children:(0,i.jsx)(t.code,{children:"f64"})})," type. This includes both integers and floating-point numbers, so you can conveniently construct a JavaScript number from a literal number:"]}),"\n",(0,i.jsx)(t.pre,{children:(0,i.jsx)(t.code,{className:"language-rust",children:"let i: Handle<JsNumber> = cx.number(42);\nlet f: Handle<JsNumber> = cx.number(3.14);\n"})}),"\n",(0,i.jsxs)(t.p,{children:["For types that aren't implicitly convertible to ",(0,i.jsx)(t.code,{children:"f64"}),", you can explicitly cast a number with Rust's ",(0,i.jsx)(t.a,{href:"https://doc.rust-lang.org/std/keyword.as.html",children:(0,i.jsx)(t.code,{children:"as"})})," operator:"]}),"\n",(0,i.jsx)(t.pre,{children:(0,i.jsx)(t.code,{className:"language-rust",children:"let size: usize = std::mem::size_of::<u128>();\nlet n = cx.number(size as f64)\n"})}),"\n",(0,i.jsxs)(t.p,{children:["Keep in mind that for some types, explicit casts to ",(0,i.jsx)(t.code,{children:"f64"})," might be lossy, so you'll want to check whether the conversion you're doing is appropriate. Another option is to use the ",(0,i.jsx)(t.a,{href:"https://doc.rust-lang.org/std/convert/trait.TryFrom.html",children:(0,i.jsx)(t.code,{children:"TryFrom"})})," API, which can help avoid loss of precision."]}),"\n",(0,i.jsx)(t.h2,{id:"strings",children:"Strings"}),"\n",(0,i.jsxs)(t.p,{children:["The ",(0,i.jsx)(t.a,{href:"https://docs.rs/neon/latest/neon/context/trait.Context.html#method.string",children:(0,i.jsx)(t.code,{children:"Context::string()"})})," method constructs a JavaScript string from a reference to a Rust string."]}),"\n",(0,i.jsx)(t.pre,{children:(0,i.jsx)(t.code,{className:"language-rust",children:'let s: Handle<JsString> = cx.string("foobar");\n'})}),"\n",(0,i.jsx)(t.h2,{id:"booleans",children:"Booleans"}),"\n",(0,i.jsxs)(t.p,{children:["The ",(0,i.jsx)(t.a,{href:"https://docs.rs/neon/latest/neon/context/trait.Context.html#method.boolean",children:(0,i.jsx)(t.code,{children:"Context::boolean()"})})," method constructs a JavaScript Boolean value."]}),"\n",(0,i.jsx)(t.pre,{children:(0,i.jsx)(t.code,{className:"language-rust",children:"let b: Handle<JsBoolean> = cx.boolean(true);\n"})}
1),"\n",(0,i.jsx)(t.h2,{id:"undefined",children:"Undefined"}),"\n",(0,i.jsxs)(t.p,{children:["The ",(0,i.jsx)(t.a,{href:"https://docs.rs/neon/latest/neon/context/trait.Context.html#method.undefined",children:(0,i.jsx)(t.code,{children:"Context::undefined()"})})," method constructs a JavaScript ",(0,i.jsx)(t.code,{children:"undefined"})," value."]}),"\n",(0,i.jsx)(t.pre,{children:(0,i.jsx)(t.code,{className:"language-rust",children:"let u: Handle<JsUndefined> = cx.undefined();\n"})}),"\n",(0,i.jsx)(t.h2,{id:"null",children:"Null"}),"\n",(0,i.jsxs)(t.p,{children:["The ",(0,i.jsx)(t.a,{href:"https://docs.rs/neon/latest/neon/context/trait.Context.html#method.null",children:(0,i.jsx)(t.code,{children:"Context::null()"})})," method constructs a JavaScript ",(0,i.jsx)(t.code,{children:"null"})," value."]}),"\n",(0,i.jsx)(t.pre,{children:(0,i.jsx)(t.code,{className:"language-rust",children:"let n: Handle<JsNull> = cx.null();\n"})})]})}function h(e={}){const{wrapper:t}={...(0,r.R)(),...e.components};return t?(0,i.jsx)(t,{...e,children:(0,i.jsx)(d,{...e})}):d(e)}}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.