Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions solutions/rust/rna-transcription/1/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "rna_transcription"
version = "0.1.0"
edition = "2024"

# Not all libraries from crates.io are available in Exercism's test runner.
# The full list of available libraries is here:
# https://github.com/exercism/rust-test-runner/blob/main/local-registry/Cargo.toml
[dependencies]
50 changes: 50 additions & 0 deletions solutions/rust/rna-transcription/1/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use std::collections::HashMap;

#[derive(Debug, PartialEq, Eq)]
pub struct Dna {
coding_strand:String
}

#[derive(Debug, PartialEq, Eq)]
pub struct Rna {
coding_strand:String
}

impl Dna {
pub fn new(dna: &str) -> Result<Dna, usize> {
let nucleotides = ['A', 'T', 'C', 'G'];
let mut seq = String::new();
for (index, ch) in dna.to_uppercase().chars().enumerate() {
if nucleotides.contains(&ch) {
seq.push(ch);
}else {
return Err(index)
}
}
Ok(Self { coding_strand: seq })
}

pub fn into_rna(self) -> Rna {
let pair = HashMap::from([('G','C'),('C','G'), ('T','A'), ('A','U')]);
let mut trans_seq = String::new();
for ch in self.coding_strand.chars() {
trans_seq.push(pair.get(&ch).unwrap().to_owned())
}
Rna { coding_strand: trans_seq }
}
}

impl Rna {
pub fn new(rna: &str) -> Result<Rna, usize> {
let nucleotides = ['A', 'C', 'G', 'U'];
let mut seq = String::new();
for (index, ch) in rna.to_uppercase().chars().enumerate() {
if nucleotides.contains(&ch) {
seq.push(ch);
}else {
return Err(index)
}
}
Ok(Self { coding_strand: seq })
}
}