Rust Quickstart¶
Get started with MRRC in Rust in 5 minutes.
Add Dependency¶
Read Records¶
use mrrc::MarcReader;
use std::fs::File;
fn main() -> mrrc::Result<()> {
let file = File::open("records.mrc")?;
let mut reader = MarcReader::new(file);
while let Some(record) = reader.read_record()? {
if let Some(title) = record.title() {
println!("{}", title);
}
}
Ok(())
}
Access Fields¶
use mrrc::Record;
// Get a specific field by tag
if let Some(field) = record.field("245") {
if let Some(title) = field.subfield("a") {
println!("Title: {}", title);
}
}
// Get all fields with a tag
for field in record.fields_by_tag("650") {
if let Some(subject) = field.subfield("a") {
println!("Subject: {}", subject);
}
}
// Use convenience methods
println!("Title: {:?}", record.title());
println!("Author: {:?}", record.author());
println!("ISBNs: {:?}", record.isbns());
Create Records¶
use mrrc::{Record, Field, Leader};
// Create a new record with builder pattern
let record = Record::builder()
.leader(Leader::default())
.control_field("001", "123456789")
.field(
Field::builder("245", '1', '0')
.subfield('a', "My Book Title")
.subfield('c', "by Author Name")
.build()
)
.build();
Write Records¶
use mrrc::MarcWriter;
use std::fs::File;
fn main() -> mrrc::Result<()> {
let file = File::create("output.mrc")?;
let mut writer = MarcWriter::new(file);
writer.write_record(&record)?;
Ok(())
}
Convert Formats¶
use mrrc::formats::{to_json, to_xml, to_marcjson};
// To JSON
let json_str = to_json(&record)?;
// To XML
let xml_str = to_xml(&record)?;
// To MARCJSON (LOC standard)
let marcjson_str = to_marcjson(&record)?;
Error Handling¶
MRRC uses a custom Result type:
use mrrc::{Result, MarcError};
fn process_file(path: &str) -> Result<()> {
let file = File::open(path)?;
let mut reader = MarcReader::new(file);
while let Some(record) = reader.read_record()? {
// Process record
}
Ok(())
}
Next Steps¶
- Reading Records Tutorial - Detailed reading guide
- Writing Records Tutorial - Builder pattern and traits
- Concurrency Tutorial - Parallel processing with Rayon
- Rust API Reference - Full API documentation