Skip to content

Instantly share code, notes, and snippets.

@IanWhitney
Created February 29, 2016 03:06
Show Gist options
  • Save IanWhitney/b2ac83ae8195f5fc1a75 to your computer and use it in GitHub Desktop.
Save IanWhitney/b2ac83ae8195f5fc1a75 to your computer and use it in GitHub Desktop.
RNA Transcription example.
use std::fmt;
pub struct RibonucleicAcid {
strand: String
}
pub struct DeoxyribonucleicAcid {
strand: String
}
pub trait NucleicAcid {
fn new(strand: &str) -> Self;
}
impl NucleicAcid for RibonucleicAcid {
fn new(strand: &str) -> RibonucleicAcid {
RibonucleicAcid {
strand: strand.to_string()
}
}
}
impl NucleicAcid for DeoxyribonucleicAcid {
fn new(strand: &str) -> DeoxyribonucleicAcid {
DeoxyribonucleicAcid {
strand: strand.to_string()
}
}
}
impl RibonucleicAcid {
fn pair_of(base: char) -> char {
match base {
'C' => 'G',
'G' => 'C',
'A' => 'U',
'T' => 'A',
_ => base
}
}
}
impl DeoxyribonucleicAcid {
pub fn to_rna(&self) -> RibonucleicAcid {
let x: String = self.strand.chars().map(RibonucleicAcid::pair_of).collect();
RibonucleicAcid::new(&x)
}
}
impl PartialEq for RibonucleicAcid {
fn eq(&self, other: &RibonucleicAcid) -> bool {
self.strand == other.strand
}
}
impl fmt::Debug for RibonucleicAcid {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "RibonucleicAcid {{ strand: {} }}", self.strand)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment