field of struct is private when importing module - rust

I am trying to split my project into multiple files but I am having problems importing them into my main.rs as it says the Column's fields are private but I have declared the struct as public.
src/column.rs
pub struct Column {
name: String,
vec: Vec<i32>,
}
src/main.rs
pub mod column;
fn main() {
let col = column::Column{name:"a".to_string(), vec:vec![1;10]};
println!("Hello, world!");
}
cargo build
src/main.rs:4:15: 4:75 error: field `name` of struct `column::Column` is private
src/main.rs:4 let col = column::Column{name:"a".to_string(), vec:vec![1;10]};
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/main.rs:4:15: 4:75 error: field `vec` of struct `column::Column` is private
src/main.rs:4 let col = column::Column{name:"a".to_string(), vec:vec![1;10]};

Try labeling the fields as public:
pub struct Column {
pub name: String,
pub vec: Vec<i32>,
}
Labeling Column as pub means that other modules can use the struct itself, but not necessarily all of its members.

You've declared the struct as public, but not the fields. To make both fields public, the struct declaration should look as follows:
pub struct Column {
pub name: String,
pub vec: Vec<i32>,
}

Related

'use' keyword not working (calling from sibling file)

I'm trying to use a struct from a value.rs file from chunk.rs, but it is not working. This is my current file hierarchy.
src
|
|---value.rs
|---chunk.rs
|---other files
Here is my value.rs code:
pub enum Value {
}
pub struct ValueArray {
values: Vec<Value>,
}
pub impl Value {
pub fn new() -> Value {
Value {
values: Vec::new(),
}
}
pub fn write_chunk(&mut self, value: Value) {
self.code.push(value);
}
}
Here is my chunk.rs code:
use crate::value::ValueArray; // Error Here
pub enum OpCode {
OpReturn,
}
pub struct Chunk {
pub code: Vec<OpCode>,
constants: value::ValueArray,
}
impl Chunk {
pub fn new() -> Chunk {
Chunk {
code: Vec::new(),
constants: value::ValueArray::new(),
}
}
pub fn write_chunk(&mut self, byte: OpCode) {
self.code.push(byte);
}
}
This is my exact error message:
unresolved import `crate::value`
could not find `value` in the crate rootrustc(E0432)
chunk.rs(1, 12): could not find `value` in the crate root
I'm not sure why it isn't working since I've done something very similar in another sibling file. I'm new to Rust, so I appreciate all of your help. Thanks
You need to define the value module on the lib.rs file
pub mod value;
The syntax I'm using to include a file ("lib.rs") with naked structs/functions (no explicit module defined) is:
mod lib;
use crate::lib::*;
So you must be missing mod value;.

Is there a way to access inner struct fields of a tuple struct without writing the `.0`? [duplicate]

I have an object of a struct with one field from an external library, which is defined as: pub struct SomeId(pub i64);
Using println! to print the object shows this, for example: SomeId(123)
I created my own struct:
#[derive(Debug)]
pub struct Something {
pub id: i64,
}
And I'm trying to put value from external struct SomeId to field id in my struct Something:
let test = Something { id: ?? };
or extract value from struct SomeId:
let test: i64 = ??;
It's also possible to use struct destructuring to extract value from SomeId.
pub struct SomeId(pub i64);
#[derive(Debug)]
pub struct Something {
pub id: i64,
}
fn main() {
let some_id = SomeId(42);
let SomeId(id) = some_id;
let test = Something { id: id };
let test: i64 = id;
}
Link to more examples.
You should probably try
let test = Something { id: external_struct.0 };
or, to the second question,:
let test = external_struct.0;
These structs , of the form , struct structname(variables...) are called tuple structs and acts very similar to tuples in rust.
May be you are looking for something like the below?
pub struct SomeId(i32);
#[derive(Debug)]
pub struct Something {
pub id: i32,
}
fn main() {
let sid = SomeId(10);
let sth = Something { id: sid.0 };
println!("{:?}", sth);
}
Playground link

Rust - value of type cannot be built from `std::iter::Iterator<>`

I have struct from which I want to extract the data.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RunResult {
pub runs: Vec<RunDetails>,
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RunDetails {
pub details: String,
pub name: String,
pub id: String,
pub type: String,
}
I am getting the data which got inserted in above struct in the variable result which I am iterating below
let my_result:RunResult = result
.runs
.into_iter()
.filter(|line| line.details.contains(&"foo"))
.collect();
I am getting the error as
value of type `RunResult` cannot be built from `std::iter::Iterator<Item=RunDetails>`
I want to get the record where type=OK, details contains foo with highest id.
How can I achieve this?
Check out the documentation for collect here.
Your struct RunResult should implement FromIterator<RunDetails>.
Try adding this (untested by me):
impl FromIterator<RunDetails> for RunResult {
fn from_iter<I: IntoIterator<Item=RunDetails>>(iter: I) -> Self {
Self {
runs: Vec::from_iter(iter);
}
}
}

How can I declare a generic HashMap in a Rust struct?

The usual way to declare a HashMap in a Rust struct is as follows:
struct MyStruct {
query: HashMap<String, String>,
counter: u32,
}
How would I write the above code if I do not know what the HashMap would contain beforehand? I have tried the below code without success.
struct MyStruct {
query: HashMap<K, V>,
counter: u32,
}
You will need to add your generics to your struct declaration as well:
struct MyStruct<K,V> {
query: HashMap<K, V>,
counter: u32,
}
Have a look at Rust Book/Generic Data Types

How do I extract a value from a tuple struct?

I have an object of a struct with one field from an external library, which is defined as: pub struct SomeId(pub i64);
Using println! to print the object shows this, for example: SomeId(123)
I created my own struct:
#[derive(Debug)]
pub struct Something {
pub id: i64,
}
And I'm trying to put value from external struct SomeId to field id in my struct Something:
let test = Something { id: ?? };
or extract value from struct SomeId:
let test: i64 = ??;
It's also possible to use struct destructuring to extract value from SomeId.
pub struct SomeId(pub i64);
#[derive(Debug)]
pub struct Something {
pub id: i64,
}
fn main() {
let some_id = SomeId(42);
let SomeId(id) = some_id;
let test = Something { id: id };
let test: i64 = id;
}
Link to more examples.
You should probably try
let test = Something { id: external_struct.0 };
or, to the second question,:
let test = external_struct.0;
These structs , of the form , struct structname(variables...) are called tuple structs and acts very similar to tuples in rust.
May be you are looking for something like the below?
pub struct SomeId(i32);
#[derive(Debug)]
pub struct Something {
pub id: i32,
}
fn main() {
let sid = SomeId(10);
let sth = Something { id: sid.0 };
println!("{:?}", sth);
}
Playground link

Resources