Borsh v1.6.1 example
⚓ Rust 📅 2026-04-20 👤 surdeus 👁️ 4Here is the link for borsh v1.6.1 page (crates.io: Rust Package Registry). I tried to use the code example posted in the page and got several compilation errors. I added dependency in rust Cargo.toml file, as suggested, and the code:
[dependencies]
borsh = "1.6.1"
borsh-derive = "1.6.1"
#[derive(BorshSerialize, BorshDeserialize, PartialEq, Debug)]
struct A {
x: u64,
y: String,
}
fn main() {
let a = A {
x: 3301,
y: "liber primus".to_string(),
};
let encoded_a = to_vec(&a).unwrap();
println!("Encoded serialization : {encoded_a:?}");
let decoded_a = from_slice::<A>(&encoded_a).unwrap();
println!("Decoded serialization: {decoded_a:?}");
assert_eq!(a, decoded_a);
}
Some of the errors are posted below:
cannot find derive macro BorshSerialize in this scope
--> src\main.rs:3:10
|
3 | #[derive(BorshSerialize, BorshDeserialize, PartialEq, Debug)]
| ^^^^^^^^^^^^^^
|
note: BorshSerialize is imported here, but it is only a trait, without a derive macro
--> src\main.rs:1:13
|
1 | use borsh::{BorshSerialize, BorshDeserialize, from_slice, to_vec};
| ^^^^^^^^^^^^^^
error: cannot find derive macro BorshDeserialize in this scope
--> src\main.rs:3:26
|
3 | #[derive(BorshSerialize, BorshDeserialize, PartialEq, Debug)]
| ^^^^^^^^^^^^^^^^
|
note: BorshDeserialize is imported here, but it is only a trait, without a derive macro
--> src\main.rs:1:29
|
1 | use borsh::{BorshSerialize, BorshDeserialize, from_slice, to_vec};
| ^^^^^^^^^^^^^^^^
error[E0277]: the trait bound A: BorshSerialize is not satisfied
--> src\main.rs:14:28
|
14 | let encoded_a = to_vec(&a).unwrap();
| ------ ^^ unsatisfied trait bound
| |
| required by a bound introduced by this call
|
help: the trait BorshSerialize is not implemented for A
--> src\main.rs:4:1 and so on...
However, If I specifically import borsh_derive (se borsh_derive::{BorshSerialize, BorshDeserialize}
and add vector type (let encoded_a: Vec = to_vec(&a).unwrap(), the errors go awasy and the code is compiled.
2 posts - 2 participants
🏷️ Rust_feed