# Writing Rust Smart Contracts on NEAR
## Building Your lib.rs
### Importing Common NEAR Crates
```rust=
// To conserve gas, efficient serialization is achieved through Borsh (http://borsh.io/)
use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
use near_sdk::{env, near_bindgen, setup_alloc};
use near_sdk::collections::LookupMap;
// Boilerplate for setting up allocator used in Wasm binary.
setup_alloc!();
```
#### What is Borsh?
```
use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
```
Serialization protocol to send Info to Store Data on NEAR
#### env, near_bindgen, setup_alloc?
```
use near_sdk::{env, near_bindgen, setup_alloc};
```
| Name | Description |
| ------------ | ------------------------------------------------------- |
| setup_alloc | memory optimization |
| near_bindgen | Used to expose state and smart contract methods to user |
| env | Boilerplate for setting up allocator in Wasm binary |
#### Collections in NEAR
```rust
use near_sdk::collections::LookupMap;
```
[Collections](https://docs.rs/near-sdk/2.0.0/near_sdk/collections/index.html) are simply data structures used to optimize data storage on the NEAR blockchain
## Structs and Bindgen
```rust=
#[near_bindgen]
#[derive(BorshDeserialize, BorshSerialize)]
pub struct MiamiContract {
records: LookupMap<String, String>,
}
```
Sturcts are essentially like object in javascript. `key-value` pairs and method `implimentations`
```rust=
impl Default for MiamiContract {
fn default() -> Self {
Self {
records: LookupMap::new(b"a".to_vec()),
}
}
}
```
`Impl` are essentially used to add methods to a struct.
`Default` is used to initialize a smart contract in place of a custom `init` function. Init functions on Near utilize the `# `
## View and Change Methods on NEAR
- View Methods: Read and Return information from blockchain: No Cost
- Change Methods: Add or modify information to the blockchain: Gas fees are included
```rust=
#[near_bindgen]
impl MiamiContract {
}
```
### Change Method Implimentation
```rust=
pub fn set_message(&mut self, message: String) {
let account_id = env::signer_account_id();
self.records.insert(&account_id, &message);
}
```
### View Method Implimentation
```rust=
// `match` is similar to `switch` in other languages; here we use it to default to "Hello" if
// self.records.get(&account_id) is not yet defined.
// Learn more: https://doc.rust-lang.org/book/ch06-02-match.html#matching-with-optiont
pub fn get_status(&self, account_id: String) -> Option<String> {
return self.records.get(&account_id);
}
```
## Compiling Rust code
In your `Contract` folder
```bash=
cargo build --target wasm32-unknown-unknown --release
```
## Deploying Smart Contract
To install `near-cli`
```
npm install -g near-cli
```
To then `login` run `near login` in your terminal to store a FullAccess Key on your local machine
Create a sub account using
```
near create-account activate-miami.blockhead.testnet --masterAccount blockhead.testnet --initialBalance 30
```
Afterwards run
```
near deploy --accountId activate-miami.blockhead.testnet --wasmFile miami-contract/target/wasm32-unknown-unknown/debug/miami_contract.wasm
```
## NEAR Zero to HERO Tutorial
Check out the NEAR NFT Zero to Hero Tutorial!
https://docs.near.org/docs/tutorials/contracts/nfts/introduction
Check out this resource for information about the NEAR SDK!
https://www.near-sdk.io/