📢 Webサイト閉鎖と移転のお知らせ
このWebサイトは2026年9月に閉鎖いたします。
新しい記事は移転先で追加しております。(旧サイトでは記事を追加しておりません)

編集の要約なし
21行目: 21行目:
* 優先度管理
* 優先度管理
*: BinaryHeap<T>
*: BinaryHeap<T>
* ビット集合
*: BitSet (外部クレート)
<br><br>
<br><br>


251行目: 253行目:
  // 出力
  // 出力
  [0, 1, 2]
  [0, 1, 2]
</syntaxhighlight>
<br><br>
== BTreeMap::range() : 範囲検索 ==
BTreeMap<K, V>では、range()メソッドを使用して、指定した範囲のキーに対応する要素を効率的に取得できる。<br>
<br>
範囲検索が必要な場合に使用する。<br>
<syntaxhighlight lang="rust">
use std::collections::BTreeMap;
fn main() {
    let mut map = BTreeMap::new();
    map.insert(1, "one");
    map.insert(2, "two");
    map.insert(3, "three");
    map.insert(4, "four");
    map.insert(5, "five");
    // 2から4までの範囲を取得
    for (key, value) in map.range(2..=4) {
      println!("{}: {}", key, value);
    }
}
// 出力
2: two
3: three
4: four
  </syntaxhighlight>
  </syntaxhighlight>
<br><br>
<br><br>
283行目: 314行目:
  // 出力
  // 出力
  hello
  hello
</syntaxhighlight>
<br><br>
== 配列とスライス ==
==== [T; N] : 固定長配列 ====
固定長配列は、コンパイル時にサイズが決まっている配列型である。<br>
<br>
サイズが固定されている場合に使用し、スタック上に格納される。<br>
<syntaxhighlight lang="rust">
fn main() {
    let arr: [i32; 5] = [1, 2, 3, 4, 5];
    println!("{:?}", arr);
    println!("Length: {}", arr.len());
    // 同じ値で初期化
    let arr2 = [0; 10];
    println!("{:?}", arr2);
}
// 出力
[1, 2, 3, 4, 5]
Length: 5
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
</syntaxhighlight>
<br>
==== &[T] : スライス ====
スライスは、配列やベクタの一部への参照である。<br>
<br>
データをコピーせずに部分的にアクセスしたい場合に使用する。<br>
<syntaxhighlight lang="rust">
fn main() {
    let arr = [1, 2, 3, 4, 5];
    let slice = &arr[1..4];
    println!("{:?}", slice);
    let v = vec![10, 20, 30, 40, 50];
    let slice2 = &v[..3];
    println!("{:?}", slice2);
}
// 出力
[2, 3, 4]
[10, 20, 30]
  </syntaxhighlight>
  </syntaxhighlight>
<br><br>
<br><br>
342行目: 416行目:
  Result: 5
  Result: 5
  Error: division by zero
  Error: division by zero
</syntaxhighlight>
<br><br>
== Cell<T>とRefCell<T> : 内部可変性 ==
==== Cell<T> : Copyな型の内部可変性 ====
Cell<T>は、不変参照から値を変更できるようにする型である。<br>
<br>
Copyトレイトを実装する型(i32、bool型等)に対して使用する。<br>
<syntaxhighlight lang="rust">
use std::cell::Cell;
fn main() {
    let c = Cell::new(5);
    println!("{}", c.get());
    c.set(10);
    println!("{}", c.get());
}
// 出力
5
10
</syntaxhighlight>
<br>
==== RefCell<T> : 動的な借用チェック ====
RefCell<T>は、実行時に借用ルールをチェックする型である。<br>
<br>
コンパイル時に借用チェックができない場合に使用する。<br>
<syntaxhighlight lang="rust">
use std::cell::RefCell;
fn main() {
    let c = RefCell::new(vec![1, 2, 3]);
    // 不変借用
    {
      let borrowed = c.borrow();
      println!("{:?}", borrowed);
    }
    // 可変借用
    {
      let mut borrowed_mut = c.borrow_mut();
      borrowed_mut.push(4);
    }
    println!("{:?}", c.borrow());
}
// 出力
[1, 2, 3]
[1, 2, 3, 4]
</syntaxhighlight>
<br><br>
== Rc<T>とArc<T> : 参照カウント ==
==== Rc<T> : シングルスレッド用の参照カウント ====
Rc<T>は、複数の所有者を持つデータを表現する型である。<br>
<br>
単一スレッド内で複数の所有者が必要な場合に使用する。<br>
<syntaxhighlight lang="rust">
use std::rc::Rc;
fn main() {
    let a = Rc::new(5);
    let b = Rc::clone(&a);
    let c = Rc::clone(&a);
    println!("a: {}, count: {}", a, Rc::strong_count(&a));
    println!("b: {}, count: {}", b, Rc::strong_count(&b));
    println!("c: {}, count: {}", c, Rc::strong_count(&c));
}
// 出力
a: 5, count: 3
b: 5, count: 3
c: 5, count: 3
</syntaxhighlight>
<br>
==== Arc<T> : マルチスレッド用の参照カウント ====
Arc<T>は、スレッド間で共有できる参照カウント型である。<br>
<br>
マルチスレッド環境で複数の所有者が必要な場合に使用する。<br>
<syntaxhighlight lang="rust">
use std::sync::Arc;
use std::thread;
fn main() {
    let data = Arc::new(vec![1, 2, 3, 4, 5]);
    let handles: Vec<_> = (0..3)
      .map(|i| {
          let data = Arc::clone(&data);
          thread::spawn(move || {
            println!("Thread {}: {:?}", i, data);
          })
      })
      .collect();
    for handle in handles {
      handle.join().unwrap();
    }
}
// 出力
Thread 0: [1, 2, 3, 4, 5]
Thread 1: [1, 2, 3, 4, 5]
Thread 2: [1, 2, 3, 4, 5]
</syntaxhighlight>
<br><br>
== Box<T> : ヒープ割り当て ==
Box<T>は、値をヒープ上に格納するスマートポインタである。<br>
<br>
再帰的なデータ構造や、サイズが大きい値をヒープに格納したい場合に使用する。<br>
<syntaxhighlight lang="rust">
fn main() {
    let b = Box::new(5);
    println!("b = {}", b);
    // 大きなデータをヒープに格納
    let large_data = Box::new([0; 1000]);
    println!("Length: {}", large_data.len());
}
// 出力
b = 5
Length: 1000
</syntaxhighlight>
<br><br>
== Cow<T> : Clone on Write ==
Cow<T>は、必要になるまでクローンを遅延させる型である。<br>
<br>
読み取り専用の場合はクローンせず、変更が必要な場合のみクローンする。<br>
<syntaxhighlight lang="rust">
use std::borrow::Cow;
fn main() {
    let s: Cow<str> = Cow::Borrowed("hello");
    println!("{}", s);
    let mut s2: Cow<str> = Cow::Borrowed("hello");
    s2.to_mut().push_str(" world");
    println!("{}", s2);
}
// 出力
hello
hello world
  </syntaxhighlight>
  </syntaxhighlight>
<br><br>
<br><br>