Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b29efcd

Browse files
reviewing rust
0 parents commit b29efcd

File tree

7 files changed

+289
-0
lines changed

7 files changed

+289
-0
lines changed

‎.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target

‎Cargo.lock

Lines changed: 130 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "example"
3+
version = "0.1.0"
4+
edition = "2018"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
borsh = "0.9.1"
10+
borsh-derive = "0.9.1"

‎src/main.rs

Whitespace-only changes.

‎src/main1.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
2+
use borsh::{BorshDeserialize, BorshSerialize};
3+
4+
#[derive(BorshSerialize, BorshDeserialize, Debug)]
5+
pub struct Record {
6+
pub nft_address: String, // < 50
7+
pub owner: String, // < 50
8+
pub register_date: String // < 50
9+
}
10+
11+
fn main() {
12+
13+
14+
/*
15+
const RECORD_MAX_SIZE: usize = 44 + 44 + 12 + 4 * 3;
16+
17+
let mut account_info: Vec<u8> = Vec::new();
18+
let data1 = String::from("4h8pBJZDp4MSzXH675AgCSMCnxEDZnjkG5DW31ySCdyp");
19+
let data2 = String::from("Ch4vjb7joSG3RTQc24Z91VJyQXN5WqncvXoXpHEhqEoX");
20+
let data1_bytes: &[u8] = data1.as_bytes();
21+
22+
let new_owner_address = String::from("Ch4vjb7joSG3RTQc24Z91VJyQXN5WqncvXoXpHEhqEoX");
23+
let new_nft_address = String::from_utf8(data1_bytes.to_vec()).unwrap();
24+
25+
let new_record: Record = Record {
26+
nft_address: new_nft_address,
27+
owner: new_owner_address,
28+
register_date: String::from("ABCABCABCABC")
29+
};
30+
31+
let mut buffer: Vec<u8> = Vec::new();
32+
33+
new_record.serialize(&mut buffer).unwrap();
34+
account_info.append(&mut buffer);
35+
36+
new_record.serialize(&mut buffer).unwrap();
37+
account_info.append(&mut buffer);
38+
39+
println!("{:?}", account_info);
40+
41+
let dst: Vec<Vec<u8>> = account_info.chunks(RECORD_MAX_SIZE).map(|s| s.into()).collect();
42+
43+
println!("{} {}", dst.len(), account_info.len());
44+
let one_record : Record = Record::try_from_slice(dst[0].as_slice()).unwrap();
45+
46+
println!("{:?}", one_record);
47+
48+
let data: [u8; 5] = [4, 6, 7, 4, 3];
49+
let mut data_vec: Vec<u8> = vec![];
50+
data_vec.append(&mut data.to_vec());
51+
data_vec.append(&mut account_info);
52+
println!("{:?}", data_vec);
53+
*/
54+
55+
//let bytes: Vec<u8> = nftAddress.to_vec();
56+
//let ascii: Ascii = Ascii::from_bytes(bytes).unwrap();
57+
58+
//println!("{:?}", nftAddress.to_vec());
59+
60+
//let addr = String::from_utf8(nftAddress.to_vec()).unwrap();
61+
//println!("{}", addr);
62+
63+
64+
// let ascii: Ascii = Ascii::from_bytes(bytes).unwrap(); // We know these chosen bytes are ok. // This call is zero-cost: no allocation, copies, or scans.
65+
// let string = String::from(ascii);
66+
67+
}
68+

‎src/main2.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
3+
trait Show {
4+
fn show(&self) -> String;
5+
}
6+
7+
impl Show for i32 {
8+
fn show(&self) -> String {
9+
format!("four-byte signed {}", self)
10+
}
11+
}
12+
13+
impl Show for f64 {
14+
fn show(&self) -> String {
15+
format!("eight byte float {}", self)
16+
}
17+
}
18+
19+
fn main() {
20+
let answer = 42;
21+
let pi = 3.14;
22+
println!("{}", answer.show());
23+
println!("{}", pi.show());
24+
}

‎src/person.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
use std::fmt;
3+
4+
struct Person {
5+
first_name: String,
6+
last_name: String
7+
}
8+
9+
impl fmt::Debug for Person {
10+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
11+
write!(f, "I am {}!", self.full_name())
12+
}
13+
}
14+
15+
impl Person {
16+
fn new(first: &str, last: &str) -> Self {
17+
Person {
18+
first_name: first.to_string(),
19+
last_name: last.to_string()
20+
}
21+
}
22+
fn full_name(&self) -> String {
23+
format!("{} {}", self.first_name, self.last_name)
24+
}
25+
26+
fn copy(&self) -> Self {
27+
Self::new(&self.first_name, &self.last_name)
28+
}
29+
30+
fn set_first_name(&mut self, first: &str) {
31+
self.first_name = first.to_string();
32+
}
33+
34+
fn to_touple(self) -> (String, String) {
35+
(self.first_name, self.last_name)
36+
}
37+
}
38+
fn main() {
39+
let mut p = Person::new("john", "max");
40+
println!("{:?}", p);
41+
println!("{} {}", p.first_name, p.last_name);
42+
43+
let c = p.copy();
44+
println!("c is {:?}", c);
45+
p.first_name = "asdfasdf".to_string();
46+
println!("c is {:?}", c);
47+
println!("p is {:?}", p);
48+
49+
50+
let x = "asdfasdf";
51+
let y = x.to_string();
52+
println!("x is {}", x);
53+
54+
println!("{:?}", p.to_touple());
55+
}
56+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /