logo
down
shadow

How do I access core traits like StrExt when using #![no_std]?


How do I access core traits like StrExt when using #![no_std]?

By : Benzi
Date : November 17 2020, 11:58 AM
Hope that helps I'm trying to write some Rust with #![no_std] set. I am trying to iterate over a str one character at a time; however none of the usual techniques seem to work. When I try to access the characters via a function provided by str, e.g. for c in s.char_indices() or for c in s.chars() I receive the error: , You need to import the trait to be able to call their methods, e.g.
code :
#![no_std]
extern crate core;

use core::str::StrExt;

fn foo(s: &str) {
    for c in s.char_indices() {}
}


Share : facebook icon twitter icon
Is it possible to use Box with no_std?

Is it possible to use Box with no_std?


By : StrikerCrash
Date : March 29 2020, 07:55 AM
should help you out You have to import the alloc crate:
code :
#![no_std]

extern crate alloc;

use alloc::boxed::Box;

fn main() {
    let _: Box<[u8]> = Box::new([0; 10]);
}
Why traits::access cannot be compiled (c++)?

Why traits::access cannot be compiled (c++)?


By : vicky kumar
Date : March 29 2020, 07:55 AM
it fixes the issue You have specialized traits::access without giving a generic definition, so add:
code :
namespace traits
{
    template <typename, int>
        struct access;
    /* ... */
}
Blanket implementation of core traits constrained by locally-defined public trait

Blanket implementation of core traits constrained by locally-defined public trait


By : Stephanie Houng
Date : March 29 2020, 07:55 AM
it should still fix some issue Consumers of your crate might implement GetIdentifier for TheirType and simultaneously implement Hash for TheirType.
Now you might say that's their problem, but imagine another crate with a trait Foo that also does impl Hash for T {}, and TheirType implementing Foo and GetIdentifier. Suddenly they can't implement either trait.
How do I create a struct of references to traits when one object might implement multiple of the traits?

How do I create a struct of references to traits when one object might implement multiple of the traits?


By : Guillermo Codas
Date : March 29 2020, 07:55 AM
this one helps. I have a struct which manages several sensors. I have a gyroscope, accelerometer, magnetometer, barometer, and thermometer. All of which are traits. ,
In C++ I would store pointers or references
code :
struct SensorManager {
    barometer: Rc<RefCell<Barometer>>,
    thermometer: Rc<RefCell<Thermometer>>,
    gyroscope: Rc<RefCell<Gyroscope>>,
}

impl SensorManager {
    fn new(
        barometer: Rc<RefCell<Barometer>>,
        thermometer: Rc<RefCell<Thermometer>>,
        gyroscope: Rc<RefCell<Gyroscope>>,
    ) -> Self {
        Self {
            barometer,
            thermometer,
            gyroscope,
        }
    }

    fn dump_info(&self) {
        let barometer = self.barometer.borrow();
        let thermometer = self.thermometer.borrow();
        let gyroscope = self.gyroscope.borrow();

        println!(
            "{}, {}, {}",
            barometer.get(),
            thermometer.get(),
            gyroscope.get()
        );
    }

    fn update(&self) {
        self.barometer.borrow_mut().set(42);
        self.thermometer.borrow_mut().set(42);
        self.gyroscope.borrow_mut().set(42);
    }
}

fn main() {
    let multi = Rc::new(RefCell::new(Multitudes));
    let gyro = Rc::new(RefCell::new(AutoGyro));

    let manager = SensorManager::new(multi.clone(), multi.clone(), gyro.clone());

    manager.dump_info();
    manager.update();
}
barometer: Barometer + Sized,
Can traits have properties & methods with private & protected visibility? Can traits have constructor, destructo

Can traits have properties & methods with private & protected visibility? Can traits have constructor, destructo


By : ramapuram
Date : March 29 2020, 07:55 AM
like below fixes the issue Traits can have properties and methods with private and protected visibility too. You can access them like they belong to class itself. There is no difference.
Traits can have a constructor and destructor but they are not for the trait itself, they are for the class which uses the trait.
code :
trait Singleton{
    //private const CONST1 = 'const1'; //FatalError
    private static $instance = null;
    private $prop = 5;

    private function __construct()
    {
        echo "my private construct<br/>";
    }

    public static function getInstance()
    {
        if(self::$instance === null)
            self::$instance = new static();
        return self::$instance;
    }

    public function setProp($value)
    {
        $this->prop = $value;
    }

    public function getProp()
    {
        return $this->prop;
    }
}

class A
{
    use Singleton;

    private $classProp = 5;

    public function randProp()
    {
        $this->prop = rand(0,100);
    }

    public function writeProp()
    {
        echo $this->prop . "<br/>";
    }
}

//$a1 = new A(); //Fatal Error too private constructor
$a1 = A::getInstance();
$a1->writeProp();
echo $a1->getProp() . "<br/>";
$a1->setProp(10);
$a1->writeProp();
$a1->randProp();
$a1->writeProp();
$a2 = A::getInstance();
$a2->writeProp();
$a2->randProp();
$a2->writeProp();
$a1->writeProp();
shadow
Privacy Policy - Terms - Contact Us © ourworld-yourmove.org