Index
- Guessing Game
- Common Programming Concepts
- Understanding Ownership
- Using Structs
- Enums and Pattern Matching
- Managing Growing Projects with Packages, Crates, and Modules
- Defining Modules to Control Scope and Privacy
- Paths for Referring to an Item in the Module Tree
- Bringing Paths into Scope with the use Keyword
- Separating Modules into Different Files
- Common Collections
- Error Handling
- Generic Types, Traits, and Lifetimes
- Writing Automated Tests
- Object Oriented Programming
- Adding dependancies
- Option Take
- RefCell
- mem
- Data Structure
- Recipe
- Semi colon
- Calling rust from python
- Default
- Crytocurrency With rust
- Function chaining
- Question Mark Operator
- Tests with println
- lib and bin
- Append vector to hash map
- Random Number
- uuid4
- uwrap and option
- Blockchain with Rust
- Near Protocol
- Actix-web
Blockchain with Rust
https://www.youtube.com/watch?v=vJdT05zl6jk&list=PLwnSaD6BDfXL0RiKT_5nOIdxTxZWpPtAvGeekLaunch
Build a cryptocurrency! - Blockchain in Rust
See the comments:
use super::*;
use std::fmt::{self, Debug, Formatter};
pub struct Block {
pub index: u32,
pub timestamp: u128,
pub hash: BlockHash,
pub prev_block_hash: BlockHash,
pub nonce: u64,
pub payload: String,
}
impl Debug for Block {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "Block[{}]:{} at: {} with: {}",
&self.index,
&hex::encode(&self.hash),
&self.timestamp,
&self.payload,
)
}
}
impl Block {
pub fn new(
index: u32, // These are all arguments, see we have not passed hash as its hard coded as vec![0;32] below
timestamp: u128,
prev_block_hash: BlockHash,
nonce: u64,
payload: String,
) -> Self { // Here self is the return type, look at the method systax in structs
Block {
index,
timestamp,
hash: vec![0; 32],
prev_block_hash,
nonce,
payload,
}
}
}
use std::fmt::{self, Debug, Formatter};
pub struct Block {
pub index: u32,
pub timestamp: u128,
pub hash: BlockHash,
pub prev_block_hash: BlockHash,
pub nonce: u64,
pub payload: String,
}
impl Debug for Block {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "Block[{}]:{} at: {} with: {}",
&self.index,
&hex::encode(&self.hash),
&self.timestamp,
&self.payload,
)
}
}
impl Block {
pub fn new(
index: u32, // These are all arguments, see we have not passed hash as its hard coded as vec![0;32] below
timestamp: u128,
prev_block_hash: BlockHash,
nonce: u64,
payload: String,
) -> Self { // Here self is the return type, look at the method systax in structs
Block {
index,
timestamp,
hash: vec![0; 32],
prev_block_hash,
nonce,
payload,
}
}
}
use blockchainlib::*;
fn main() {
let block = Block::new(0, 0, vec![0; 32], 0, "Genesis block".to_owned());
println!("{:?}", &block);
}
fn main() {
let block = Block::new(0, 0, vec![0; 32], 0, "Genesis block".to_owned());
println!("{:?}", &block);
}
Cargo.toml
[package]
name = "blockchain_learning"
version = "0.1.0"
authors = ["Amiya Behera <amiyatulu@gmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib] # Library is defined here which is lib.rs and the name is blockhainlib
name = "blockchainlib"
path = "src/lib.rs"
[[bin]] # Binary is defined here
name = "blockchain"
path = "src/main.rs"
[dependencies]
hex = "0.4.2"
name = "blockchain_learning"
version = "0.1.0"
authors = ["Amiya Behera <amiyatulu@gmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib] # Library is defined here which is lib.rs and the name is blockhainlib
name = "blockchainlib"
path = "src/lib.rs"
[[bin]] # Binary is defined here
name = "blockchain"
path = "src/main.rs"
[dependencies]
hex = "0.4.2"